local TalentDataManager = BaseClass("TalentDataManager")
local TalentData = require("DataCenter.TalentManager.TalentData")
local __init = function(self)
  self.allTalents = {}
  self.resetNum = 0
  self.refreshTalentCount = 0
  self.goldResetCount = 0
  self.talentOptions = {}
  self.specialShowTalentId = nil
end
local __delete = function(self)
  self.allTalents = nil
  self.resetNum = nil
  self.goldResetCount = nil
  self.talentOptions = nil
  self.specialShowTalentId = nil
  self.refreshTalentCount = nil
end
local InitTalent = function(self, message)
  if message.baseTalent ~= nil then
    local talent = message.baseTalent
    if talent.talentInfo ~= nil then
      self:UpdateTalentInfo(talent.talentInfo)
    end
    if talent.talents ~= nil then
      table.walk(talent.talents, function(_, v)
        self:UpdateOneTalentData(v)
      end)
    end
  end
  EventManager:GetInstance():Broadcast(EventId.TalentDataChange)
end
local UpdateOneTalentData = function(self, data)
  if data ~= nil then
    self.allTalents[data.talentId] = 1
  end
end
local GetMaxOpenedTalentByGroup = function(self, group)
  local lv = 0
  local id = 0
  local order = 0
  table.walk(self.allTalents, function(k, v)
    local template = DataCenter.TalentTemplateManager:GetTemplate(k)
    if template == nil or template.group ~= group then
      return
    end
    if id == 0 then
      lv = template.lv
      id = template.id
      order = template.order
    end
    if template and template.lv > lv then
      lv = template.lv
      id = template.id
      order = template.order
    end
  end)
  return lv, id, order
end
local GetMinLvTemplateByGroup = function(self, group)
  local templates = DataCenter.TalentTemplateManager:GetAllTemplate()
  for k, v in pairs(templates) do
    if v.group == group and v.lv == 1 then
      return v
    end
  end
  return nil
end
local IsTalentOpen = function(self, talentId)
  return self.allTalents[toInt(talentId)] ~= nil
end
local UpdateTalentInfo = function(self, info)
  self.talentOptions = {}
  self.resetNum = info.resetNum
  self.goldResetCount = info.goldResetCount or 0
  self.refreshTalentCount = info.refreshTalentCount or 0
  self.lastResetTime = info.lastResetTime or 0
  self.extraPoint = info.extraPoint or 0
  local optionStr = info.talentOption
  if not string.IsNullOrEmpty(optionStr) then
    local tempVec = string.split(optionStr, "|")
    local count = table.count(tempVec)
    for i = count, 1, -1 do
      local configK = DataCenter.TalentTemplateManager:GetTemplate(toInt(tempVec[i]))
      if configK == nil then
        table.remove(tempVec, i)
      end
    end
    table.sort(tempVec, function(k, v)
      local configK = DataCenter.TalentTemplateManager:GetTemplate(toInt(k))
      local configV = DataCenter.TalentTemplateManager:GetTemplate(toInt(v))
      return configK.order < configV.order
    end)
    if 0 < table.count(tempVec) then
      table.insert(self.talentOptions, tempVec[2] or tempVec[1])
      table.insert(self.talentOptions, tempVec[1])
      table.insert(self.talentOptions, tempVec[3] or tempVec[1])
    end
  end
end
local TalentChooseHandler = function(self, message)
  if message.errorCode ~= nil then
    if message.errorCode ~= SeverErrorCode then
      UIUtil.ShowTips(Localization:GetString(message.errorCode))
    end
    return
  end
  if message.talentInfo ~= nil then
    self:UpdateTalentInfo(message.talentInfo)
  end
  if message.talentId ~= nil then
    self:UpdateOneTalentData(message)
  end
  EventManager:GetInstance():Broadcast(EventId.TalentDataChange)
end
local TalentPushHandler = function(self, message)
  if message.talentInfo ~= nil then
    self:UpdateTalentInfo(message.talentInfo)
  end
  EventManager:GetInstance():Broadcast(EventId.TalentDataChange)
end
local TalentResetHandler = function(self, message)
  if message.errorCode ~= nil then
    if message.errorCode ~= SeverErrorCode then
      UIUtil.ShowTips(Localization:GetString(message.errorCode))
    end
    return
  end
  if message.gold ~= nil then
    LuaEntry.Player.gold = message.gold
    EventManager:GetInstance():Broadcast(EventId.UpdateGold)
  end
  self.allTalents = {}
  self.talentOptions = {}
  if message.talentInfo ~= nil then
    self:UpdateTalentInfo(message.talentInfo)
  end
  EventManager:GetInstance():Broadcast(EventId.TalentDataChange)
end
local TalentChooseResetHandler = function(self, message)
  if message.errorCode ~= nil then
    if message.errorCode ~= SeverErrorCode then
      UIUtil.ShowTips(Localization:GetString(message.errorCode))
    end
    return
  end
  if message.talentInfo ~= nil then
    self:UpdateTalentInfo(message.talentInfo)
  end
  EventManager:GetInstance():Broadcast(EventId.TalentDataChange)
end
local GetTalentOptions = function(self)
  return self.talentOptions
end
local GetResetNum = function(self)
  return self.resetNum
end
local GetAllChooseTalent = function(self)
  return table.keys(self.allTalents)
end
local IsSystemOpen = function(self)
  local configOpenState = LuaEntry.DataConfig:CheckSwitch("base_talent_switch")
  if not configOpenState then
    return false
  end
  local openLv = LuaEntry.DataConfig:TryGetNum("base_talent", "k1")
  local mainLv = DataCenter.BuildManager.MainLv
  return openLv <= mainLv
end
local HasTalentToChoose = function(self)
  return self.talentOptions ~= nil and table.count(self.talentOptions) > 0
end
local TalentReset = function(self, resetType)
  SFSNetwork.SendMessage(MsgDefines.ResetBaseTalent, resetType)
end
local TalentChoose = function(self, talentId)
  SFSNetwork.SendMessage(MsgDefines.ChooseBaseTalent, toInt(talentId))
end
local TalentChooseReset = function(self)
  SFSNetwork.SendMessage(MsgDefines.RefreshBaseTalent)
end
local GetResetInfo = function(self)
  return self.resetNum, self.goldResetCount
end
local GetChooseNum = function(self)
  local currentNum = self.allTalents ~= nil and table.count(self.allTalents) or 0
  local openLv = LuaEntry.DataConfig:TryGetNum("base_talent", "k1")
  local mainLv = DataCenter.BuildManager.MainLv
  local total = self.extraPoint + mainLv + 1 - openLv
  return currentNum, total
end
local SetSpecialShowTalent = function(self, talentId)
  self.specialShowTalentId = talentId
end
local LeftRefreshTalentCount = function(self)
  local totalTime = Mathf.Round(LuaEntry.Effect:GetGameEffect(EffectDefine.TALENT_REFRESH_TIME))
  if totalTime - self.refreshTalentCount > 0 then
    return totalTime - self.refreshTalentCount
  end
  return 0
end
TalentDataManager.__init = __init
TalentDataManager.__delete = __delete
TalentDataManager.InitTalent = InitTalent
TalentDataManager.UpdateOneTalentData = UpdateOneTalentData
TalentDataManager.UpdateTalentInfo = UpdateTalentInfo
TalentDataManager.TalentChooseHandler = TalentChooseHandler
TalentDataManager.TalentChoose = TalentChoose
TalentDataManager.TalentPushHandler = TalentPushHandler
TalentDataManager.TalentResetHandler = TalentResetHandler
TalentDataManager.TalentReset = TalentReset
TalentDataManager.GetTalentOptions = GetTalentOptions
TalentDataManager.TalentChooseReset = TalentChooseReset
TalentDataManager.TalentChooseResetHandler = TalentChooseResetHandler
TalentDataManager.GetResetNum = GetResetNum
TalentDataManager.GetAllChooseTalent = GetAllChooseTalent
TalentDataManager.IsSystemOpen = IsSystemOpen
TalentDataManager.HasTalentToChoose = HasTalentToChoose
TalentDataManager.GetMaxOpenedTalentByGroup = GetMaxOpenedTalentByGroup
TalentDataManager.IsTalentOpen = IsTalentOpen
TalentDataManager.GetMinLvTemplateByGroup = GetMinLvTemplateByGroup
TalentDataManager.GetResetInfo = GetResetInfo
TalentDataManager.GetChooseNum = GetChooseNum
TalentDataManager.SetSpecialShowTalent = SetSpecialShowTalent
TalentDataManager.LeftRefreshTalentCount = LeftRefreshTalentCount
return TalentDataManager
