local this = class("pageAutoRuneConfirm", G_UIPageBase)
local talentRuneTpl = L_GameTpl:getTalentRuneTpl()
local battleInfoTpl = L_GameTpl:getBattleInfoTpl()

function this.bind()
  return {
    module_commonTipLarge = L_Const.ModuleInfo.CommonTipLarge,
    moduleCurrency = {
      moduleName = "modulePages/moduleCurrency"
    },
    sortingOrder_page = nil,
    runeList = {
      moduleName = "pages/HeroPanel/talent/cellAutoRune"
    },
    equipBtn = L_Const.ModuleInfo.ButtonSingle,
    attributList = {
      moduleName = "pages/heroPanel/hero/heroProperty"
    },
    skillList = {
      moduleName = "pages/heroPanel/talent/cellSkillAddItem"
    },
    attributListActive = true,
    skillListActive = true
  }
end

function this.methods()
  return {
    module_commonTipLarge = {
      onClick_close = function(self)
        if self._params.cancelCallback then
          self._params.cancelCallback()
        end
        L_UI:close(self.pageName)
      end,
      onClick_cancel = function(self)
        if self._params.cancelCallback then
          self._params.cancelCallback()
        end
        L_UI:close(self.pageName)
      end,
      onClick_confirm = function(self)
        if self._params.confirmCallback then
          self._params.confirmCallback()
        end
        L_UI:close(self.pageName)
      end
    }
  }
end

function this:preOpen(options)
  this.super.preOpen(self, options)
  self.selectedIndex = 1
  self:refreshPage(options)
end

function this:refreshPage(params)
  self._params = params or {}
  self.modules.module_commonTipLarge:initModule(params)
  if not math.isEmpty(params.overrideSortingOrder) then
    self.bind.sortingOrder_page = params.overrideSortingOrder
  end
  local data = {}
  self.runeData = {}
  for _, runeId in ipairs(self._params.runes) do
    table.insert(self.runeData, {
      runeId = runeId,
      heroGuid = self._params.heroGuid,
      isEquiped = false
    })
    table.insert(data, {runeId = runeId})
  end
  self.bind.runeList:clear()
  self.bind.runeList:insert_array(data)
  self:initEquipBtn()
  self:refreshDetail()
  local tpl_sys = L_GameTpl:getSystemUnlockTpl():getTplById(L_SystemConst.enum.heroTalent)
  self.modules.moduleCurrency:refreshRes(tpl_sys)
end

function this:onClickRune(runeId)
  for k, v in ipairs(self.runeData) do
    if v.runeId == runeId then
      self.selectedIndex = k
      break
    end
  end
  self:refreshDetail()
end

function this:refreshDetail()
  self:initAttributeList()
  self:initSkillList()
end

function this:initAttributeList()
  local attributes = {}
  local runeIds = {}
  for _, v in ipairs(self.runeData) do
    table.insert(runeIds, v.runeId)
  end
  L_HeroManager:addRuneAttributes(runeIds, attributes)
  local data = {}
  for k, v in pairs(attributes) do
    local name = battleInfoTpl:getName(battleInfoTpl:getTplById(k))
    if not string.isEmpty(L_Const.attrBaseName[k]) then
      name = L_WordsTpl:getValue(L_Const.attrBaseName[k])
    end
    table.insert(data, {
      active_icon = true,
      icon = battleInfoTpl:getIcon(battleInfoTpl:getTplById(k)),
      nextAttr = L_WordsTpl:getValue("ui_hero_rune_atttribute_add", {
        [0] = battleInfoTpl:getShowTxtComma(k, v)
      }),
      name = name,
      id = k
    })
  end
  table.sort(data, function(a, b)
    return a.id < b.id
  end)
  for i, v in ipairs(data) do
    v.toggle_state = i % 2 ~= 0
  end
  self.bind.attributList:clear()
  self.bind.attributList:insert_array(data)
  self.bind.attributListActive = 0 < #data
end

function this:initSkillList()
  local skillData = {}
  local heroGuid = self._params.heroGuid
  local hero = L_HeroStore:getHero(heroGuid)
  local skillList = L_HeroManager:getHeroSystemSkillList(hero)
  for _, v in ipairs(self.runeData) do
    local runeId = v.runeId
    local runeTpl = talentRuneTpl:getTplById(runeId)
    local skills = talentRuneTpl:getRuneSkill(runeTpl)
    for _, skill in ipairs(skills) do
      local slot = skill[1] + 1
      if not skillData[slot] then
        skillData[slot] = {
          skillId = skillList[slot].skillId,
          addLevel = skill[2],
          heroGuid = heroGuid,
          slot = slot
        }
      else
        skillData[slot].addLevel = skill[2] + skillData[slot].addLevel
      end
    end
  end
  local data = {}
  for _, skill in pairs(skillData) do
    table.insert(data, skill)
  end
  table.sort(data, function(a, b)
    return a.slot < b.slot
  end)
  self.bind.skillList:clear()
  self.bind.skillList:insert_array(data)
  self.bind.skillListActive = 0 < #data
end

function this:initEquipBtn()
  local needCost = 0
  for _, rune in ipairs(self.runeData) do
    if not rune.isEquiped then
      local runeTpl = talentRuneTpl:getTplById(rune.runeId)
      local itemId = talentRuneTpl:getRuneItem(runeTpl)
      if 0 < L_BagStore:getItemNum(itemId) then
        local cost = talentRuneTpl:getEquipRuneThroughCoin(runeTpl)
        needCost = needCost + cost
      end
    end
  end
  self.modules.equipBtn:setData(L_WordsTpl:getValue("ui_hero_rune_quick_equip"), handler(self, self.dealEquipBtn), {
    itemType = L_Const.resType.currency,
    itemId = L_Const.currencyType.gold,
    txtNum = needCost
  })
end

function this:dealEquipBtn()
  if self._params.confirmCallback then
    self._params.confirmCallback()
  end
  L_UI:close(self.pageName)
end

function this:close()
  this.super.close(self)
  if self._params.pageClosedCallback then
    self._params.pageClosedCallback()
  end
end

function this:escHandle()
  if self._params.closeCallbackTemp then
    self._params.closeCallbackTemp()
  end
  L_UI:close(self.pageName)
end

return this
