local this = class("moduleTalentContent", G_UIModuleBase)
local talentRuneTpl = L_GameTpl:getTalentRuneTpl()
local battleInfoTpl = L_GameTpl:getBattleInfoTpl()
local talentRankTpl = L_GameTpl:getTalentRankTpl()

function this.bind()
  return {
    runeNameText = "",
    attributeList = {
      moduleName = "pages/HeroPanel/hero/heroProperty"
    },
    skillDesList = {
      moduleName = "pages/HeroPanel/talent/cellSkillAddItem"
    },
    equipBtn = L_Const.ModuleInfo.ButtonSingle,
    holdNumTxt = "",
    equipBtnActive = true
  }
end

function this.methods()
  return {
    onClick_closeBtn = function(self)
      L_UI:close("pageHeroTalentInfo")
    end,
    onClick_showInfo = function(self)
      L_ItemTplManager:showInfoTip(L_Const.resType.commonItem, self.itemId)
    end
  }
end

function this:initPage(options)
  self.options = options
  local runeTpl = talentRuneTpl:getTplById(self.options.runeId)
  self.itemId = talentRuneTpl:getRuneItem(runeTpl)
  local item = L_ItemTplManager:getCommonItem(self.itemId)
  self.bind.runeNameText = item.name
  self.bind.holdNumTxt = L_WordsTpl:getValue("ui_hero_rune_stock", {
    [0] = L_BagStore:getItemNum(self.itemId)
  })
  self.isLackRune = 0 >= L_BagStore:getItemNum(self.itemId)
  local attributes = talentRuneTpl:getRuneAttribute(runeTpl)
  local runeSkills = talentRuneTpl:getRuneSkill(runeTpl)
  self:initAttributeList(attributes)
  self:initRuneSkillList(runeSkills)
  local needCost = talentRuneTpl:getEquipRuneThroughCoin(runeTpl)
  self.isLackGold = needCost > L_PlayerStore:getCurrencyNum(L_Const.currencyType.gold)
  self.modules.equipBtn:setData(L_WordsTpl:getValue("ui_hero_rune_equip"), handler(self, self.dealClickEquipBtn), {
    itemType = L_Const.resType.currency,
    itemId = L_Const.currencyType.gold,
    txtNum = needCost
  })
  self.bind.equipBtnActive = options.canEquip
end

function this:dealClickEquipBtn()
  if self.isBreak then
    return
  end
  if L_BattleDataManager:checkPlayerBattle() then
    L_FlyMsgManager:showNormalMsg(L_BattleDataManager:getBattleBlockTips(L_Const.BattleBlockType.HeroRuneEquip))
    return
  end
  if self.isLackGold and self.isLackRune then
    L_FlyMsgManager:showNormalMsg(L_WordsTpl:getValue("ui_hero_rune_lack_resource"))
    return
  end
  if self.isLackGold then
    L_FlyMsgManager:showNormalMsg(L_WordsTpl:getValue("ui_hero_rune_lack_gold"))
    return
  end
  if self.isLackRune then
    L_FlyMsgManager:showNormalMsg(L_WordsTpl:getValue("ui_hero_rune_lack_equip"))
    return
  end
  self.isBreak = true
  AzurWorld.heroMgr:ReqHeroRuneEquip(self.options.heroGuid, {
    self.options.slotId
  }, function(errCode, rspData)
    if L_UI:checkPageOpen("pageHeroTalentInfo") then
      L_UI:close("pageHeroTalentInfo")
    end
    Timer.once(2, function()
      self.isBreak = false
    end)
    L_AudioUtil.playSound("Play_SFX_System_UI_StarGift_Equip")
  end)
end

function this:initAttributeList(attributes)
  local propertyData = {}
  for i, v in pairs(attributes) do
    local strName = L_WordsTpl:getValue(L_Const.attrBaseName[i])
    table.insert(propertyData, {
      active_icon = true,
      icon = battleInfoTpl:getIcon(battleInfoTpl:getTplById(i)),
      nextAttr = L_WordsTpl:getValue("ui_hero_rune_atttribute_add", {
        [0] = battleInfoTpl:getShowTxtComma(i, v)
      }),
      name = string.isEmpty(strName) and battleInfoTpl:getName(battleInfoTpl:getTplById(i)) or strName,
      id = i
    })
  end
  table.sort(propertyData, function(a, b)
    return a.id < b.id
  end)
  for i, v in ipairs(propertyData) do
    v.toggle_state = i % 2 == 0
  end
  self.bind.attributeList:clear()
  self.bind.attributeList:insert_array(propertyData)
end

function this:initRuneSkillList(runeSkills)
  self.bind.skillDesList:clear()
  local hero = L_HeroStore:getHero(self.options.heroGuid)
  local heroConfigId = L_HeroStore:getHeroConfigId(hero)
  local sysSkills = L_HeroManager:getHeroSystemSkillList(hero)
  local skillData = {}
  for _, skill in ipairs(runeSkills) do
    local level = self:getSlotLevel(heroConfigId, self.options.currentRank, skill[1])
    table.insert(skillData, {
      canEquip = self.options.canEquip,
      skillId = sysSkills[skill[1] + 1].skillId,
      currentLevel = level,
      addLevel = skill[2],
      heroGuid = self.options.heroGuid
    })
  end
  self.bind.skillDesList:insert_array(skillData)
end

function this:getSlotLevel(heroConifgId, currentRank, slot)
  local level = 1
  for rank = 1, currentRank - 1 do
    local rankTpl = talentRankTpl:getHeroRankTpl(heroConifgId, rank)
    local rankBreakItems = talentRankTpl:getRankBreakthroughItem(rankTpl)
    for k, runeId in ipairs(rankBreakItems) do
      local runeTpl = talentRuneTpl:getTplById(runeId)
      local runeSkills = talentRuneTpl:getRuneSkill(runeTpl)
      for _, skill in ipairs(runeSkills) do
        if skill[1] == slot then
          level = level + skill[2]
        end
      end
    end
  end
  return level
end

return this
