local AccountListManager = BaseClass("AccountListManager")
local AccountInfo = require("DataCenter.AccountData.AccountInfo")
local MaxLimit = 50
local Sdk = CS.GameEntry.Sdk
local Setting = CS.GameEntry.Setting

function AccountListManager:__init()
  self.m_accountInfos = nil
  self:LoadAcountInfos()
end

function AccountListManager:LoadAcountInfos()
  local accountCache = Sdk:GetDataFromNative("PM_getDatFromFile", "account.txt")
  local historyAccount = ""
  if not string.IsNullOrEmpty(accountCache) then
    historyAccount = accountCache
  else
    historyAccount = Setting:GetString(SettingKeys.ACCOUNT_LIST_DEBUG, "")
  end
  self.m_accountInfos = {}
  local accList = string.string2array_s(historyAccount, "|", "#")
  for k, v in ipairs(accList) do
    if not table.IsNullOrEmpty(v) and not (#v < 4) then
      local accountInfo = AccountInfo.New()
      accountInfo.serverid = toInt(v[1])
      accountInfo.gameUid = v[2]
      accountInfo.nickname = v[3]
      accountInfo.level = toInt(v[4])
      if 5 <= #v then
        accountInfo.newLevel = toInt(v[5])
      end
      if 8 <= #v then
        accountInfo.ip = v[6]
        accountInfo.port = toInt(v[7])
        accountInfo.zone = v[8]
      end
      if 9 <= #v then
        accountInfo.accessToken = v[9]
      end
      if 10 <= #v then
        accountInfo.urlEnv = v[10]
      end
      table.insert(self.m_accountInfos, accountInfo)
    end
  end
end

function AccountListManager:AddAcountInfo(accountInfo)
  local queryIndex = self:GetAcountInfoIndexByUidAndURLEnv(accountInfo.serverid, accountInfo.gameUid, accountInfo.urlEnv)
  if queryIndex ~= -1 then
    table.remove(self.m_accountInfos, queryIndex)
  end
  table.insert(self.m_accountInfos, accountInfo)
  self:Save()
end

function AccountListManager:DeleteAcountInfo(serverId, uid, urlEnv)
  local index = self:GetAcountInfoIndexByUidAndURLEnv(serverId, uid, urlEnv)
  if index == -1 then
    return
  end
  table.remove(self.m_accountInfos, index)
  self:Save()
end

function AccountListManager:GetAcountInfoIndexByUidAndURLEnv(serverId, uid, urlEnv)
  local index = -1
  if string.IsNullOrEmpty(uid) then
    return index
  end
  for k, v in pairs(self.m_accountInfos) do
    if v.serverid == serverId and v.gameUid == uid and v.urlEnv == urlEnv then
      index = k
      break
    end
  end
  return index
end

function AccountListManager:Save()
  local count = #self.m_accountInfos
  local beginIndex = count > MaxLimit and count - MaxLimit or 0
  beginIndex = beginIndex + 1
  local tbl = {}
  for i = beginIndex, count do
    local acountInfo = self.m_accountInfos[i]
    if not string.IsNullOrEmpty(acountInfo.gameUid) then
      local newStr = string.format("%s|%s|%s|%s|%s|%s|%s|%s|%s|%s", acountInfo.serverid, acountInfo.gameUid, acountInfo.nickname or "", acountInfo.level or 0, acountInfo.newLevel or 0, acountInfo.ip, acountInfo.port, acountInfo.zone, acountInfo.accessToken or "", acountInfo.urlEnv or "")
      table.insert(tbl, newStr)
    end
  end
  local str = table.concat(tbl, "#")
  Setting:SetString(SettingKeys.ACCOUNT_LIST_DEBUG, str)
  Sdk:saveDataToSdcard(str, "account.txt")
end

function AccountListManager:UpdatePlayerMainLv(serverId, uid, level, urlEnv)
  local index = self:GetAcountInfoIndexByUidAndURLEnv(serverId, uid, urlEnv)
  if index == -1 then
    return
  end
  self.m_accountInfos[index].newLevel = level
  self:Save()
end

function AccountListManager:UpdatePlayerName(serverId, uid, name, urlEnv)
  local index = self:GetAcountInfoIndexByUid(serverId, uid, urlEnv)
  if index == -1 then
    return
  end
  self.m_accountInfos[index].nickname = name
  self:Save()
end

function AccountListManager:GetAccountInfos()
  local filteredAccountList = {}
  for k, v in ipairs(self.m_accountInfos) do
    if v.urlEnv == tostring(CS.NetworkURLConfig.URLGroupType) then
      table.insert(filteredAccountList, v)
    end
  end
  return filteredAccountList
end

return AccountListManager
