local this = class("pageSkillInfo", G_UIPageBase)
local skillLevelTpl = L_GameTpl:getSkillLevelTpl()
local talentRankTpl = L_GameTpl:getTalentRankTpl()
local talentRuneTpl = L_GameTpl:getTalentRuneTpl()
local _skillTpl = L_GameTpl:getSkillTpl()
local _skillsubLogicTpl = L_GameTpl:getSkillsubLogicTpl()

function this.bind()
  return {
    txt_skillTagName = nil,
    img_icon = nil,
    txt_name = nil,
    txt_level = nil,
    list_att = {
      moduleName = "pages/HeroPanel/talent/cellTalentAttrItem"
    },
    active_btnUpgrade = false,
    txt_desc = nil,
    active_subDesc = false,
    txt_subDesc = nil,
    module_commonTipLarge = L_Const.ModuleInfo.CommonTipLarge
  }
end

function this.methods()
  return {
    onClick_jumpBtn = function(self)
      if self._canLevelUp then
        local hero = self._heroItemInfo
        L_HeroManager:showHeroSelection(L_Const.heroModuleType.Talent, L_HeroStore:getHeroGuid(hero), {
          jumpRank = self._jumpRank,
          jumpRuneId = self._jumpRuneId
        })
        L_UI:close(self.pageName)
      end
    end,
    module_commonTipLarge = {
      onClick_close = function(self)
        L_UI:close(self.pageName)
      end,
      onClick_bg = function(self)
        L_UI:close(self.pageName)
      end
    }
  }
end

function this:preOpen(options)
  options = options or {}
  self._heroItemInfo = options.hero
  self._skillId = options.skillId
  self._isPreview = options.isPreview
  self:initCommonTipModule()
  self:initSkillInfo()
end

function this:close(options)
  this.super.close(self, options)
end

function this:initCommonTipModule()
  local moduleTip = self.modules.module_commonTipLarge
  moduleTip:initModule({
    hideConfirm = true,
    hideCancel = true,
    txtTitle = L_WordsTpl:getValue("ui_pageskillinfo_describe")
  })
end

function this:initSkillInfo()
  self:getCanLevelUp()
  local tpl_skill = _skillTpl:getTplById(self._skillId)
  self.bind.img_icon = _skillTpl:getIcon(tpl_skill)
  self.bind.txt_name = _skillTpl:getName(tpl_skill)
  local skill_list = L_HeroManager:getHeroSystemSkillList(self._heroItemInfo)
  local skill, index = table.ipairsFind(skill_list, function(value)
    return value.skillId == self._skillId
  end)
  skill = skill or {}
  local skillLevel = skill.skillLevel or 1
  local isUnlock = not math.isEmpty(index)
  local isMaxLevel = false
  local levelTxt = L_WordsTpl:getValue("menu_common_lock")
  if isUnlock then
    local tpl_skillLevel = skillLevelTpl:getTplByIdAndLevel(self._skillId, skillLevel)
    local tpl_skillLevelNext = skillLevelTpl:getTplByIdAndLevel(self._skillId, skillLevel + 1)
    if table.isEmpty(tpl_skillLevelNext) and not skillLevelTpl:getCanUp(tpl_skillLevel) then
      isMaxLevel = true
      levelTxt = L_WordsTpl:getValue("menu_common_lvMax")
    else
      levelTxt = string.format("%s%s", L_WordsTpl:getValue("info_char_lv"), skillLevel)
    end
  end
  self.bind.active_btnUpgrade = self._canLevelUp and not isMaxLevel and isUnlock
  self.bind.txt_level = levelTxt
  local skillType = _skillTpl:getSkillDisplayType(tpl_skill)
  self.bind.txt_skillTagName = L_WordsTpl:getValue(string.format("menu_skill_type%s", skillType + 1))
  local tpl_skillLevel = skillLevelTpl:getTplByIdAndLevel(self._skillId, skillLevel)
  local subId = skillLevelTpl:getSubSkillId(tpl_skillLevel)
  local tpl_skillSub = _skillsubLogicTpl:getTplById(subId)
  local temp = {}
  table.insert(temp, {
    attrIcon = "UI/Atlas/Skill/tex_icon_skillstatus_lq.png",
    name = L_WordsTpl:getValue("ui_pageskillinfo_cd"),
    value = L_WordsTpl:getValue("menu_skill_coolDownTime", {
      [0] = _skillsubLogicTpl:getCoolDown(tpl_skillSub) / 1000
    }),
    bgEnable = true
  })
  table.insert(temp, {
    attrIcon = "UI/Atlas/Skill/tex_icon_skillstatus_nlxh.png",
    name = L_WordsTpl:getValue("ui_pageskillinfo_energy_consumption"),
    value = tostring(_skillsubLogicTpl:getSpCost(tpl_skillSub)),
    bgEnable = false
  })
  self.bind.list_att:clear()
  self.bind.list_att:insert_array(temp)
  self.bind.txt_desc = L_GameUtil.getSkillDesc(self._skillId, skillLevel)
  local specialDes = skillLevelTpl:getSkillSpecialDesc(tpl_skillLevel)
  self.bind.active_subDesc = not string.isEmpty(specialDes)
  self.bind.txt_subDesc = specialDes
end

function this:getCanLevelUp()
  if self._isPreview or self._heroItemInfo == nil then
    self._canLevelUp = false
    return
  end
  local sysSkills = L_HeroManager:getHeroSystemSkillList(self._heroItemInfo)
  local curRank = L_HeroStore:getHeroRank(self._heroItemInfo)
  local maxRank = talentRankTpl:getHeroMaxVisibleRankNum(L_HeroStore:getHeroConfigId(self._heroItemInfo))
  local runes = L_HeroStore:getHeroRuneIds(self._heroItemInfo)
  for rank = curRank, maxRank do
    local rankTpl = talentRankTpl:getHeroRankTpl(L_HeroStore:getHeroConfigId(self._heroItemInfo), rank)
    local rankBreakItems = talentRankTpl:getRankBreakthroughItem(rankTpl)
    for k, id in ipairs(rankBreakItems) do
      local runeTpl = talentRuneTpl:getTplById(id)
      local isEquiped = false
      if rank == curRank then
        isEquiped = not math.isEmpty(runes[k])
      end
      if not isEquiped then
        local runeSkills = talentRuneTpl:getRuneSkill(runeTpl)
        for _, skill in ipairs(runeSkills) do
          local skillId = sysSkills[skill[1] + 1].skillId
          if self._skillId == skillId then
            self._canLevelUp = true
            self._jumpRank = rank
            self._jumpRuneId = id
            break
          end
        end
      end
      if self._canLevelUp then
        break
      end
    end
    if self._canLevelUp then
      break
    end
  end
end

return this
