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

function this.bind()
  return {
    module_commonTipSmall = L_Const.ModuleInfo.CommonTipSmall,
    moduleCurrency = {
      moduleName = "modulePages/moduleCurrency"
    },
    sortingOrder_page = nil,
    beforeLevelLimit = "",
    curLevelLimit = "",
    propList = {
      moduleName = "pages/HeroPanel/hero/heroProperty"
    },
    breakBtn = L_Const.ModuleInfo.ButtonSingle
  }
end

function this.methods()
  return {
    module_commonTipSmall = {
      onClick_close = function(self)
        L_UI:close(self.pageName)
      end,
      onClick_cancel = function(self)
        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_commonTipSmall:initModule(params)
  if not math.isEmpty(params.overrideSortingOrder) then
    self.bind.sortingOrder_page = params.overrideSortingOrder
  end
  local hero = L_HeroStore:getHero(params.heroGuid)
  local configId = L_HeroStore:getHeroConfigId(hero)
  local currentRank = L_HeroStore:getHeroRank(hero)
  local nextRank = currentRank + 1
  local currentTpl = talentRankTpl:getHeroRankTpl(configId, currentRank)
  local nextTpl = talentRankTpl:getHeroRankTpl(configId, nextRank)
  self.bind.beforeLevelLimit = tostring(talentRankTpl:getRankLevelLimit(currentTpl))
  self.bind.curLevelLimit = tostring(talentRankTpl:getRankLevelLimit(nextTpl))
  self.modules.breakBtn:setData(L_WordsTpl:getValue("ui_hero_rune_level_up"), function()
    if self._params.confirmCallback then
      self._params.confirmCallback()
    end
    L_UI:close(self.pageName)
  end, params.breakBtnData)
  self:initPropList(currentTpl)
  local tpl_sys = L_GameTpl:getSystemUnlockTpl():getTplById(L_SystemConst.enum.heroTalent)
  self.modules.moduleCurrency:refreshRes(tpl_sys)
end

function this:initPropList(rankTpl)
  local data = {}
  local attributes = talentRankTpl:getAttribute(rankTpl)
  local property = L_HeroStore:getHeroPropertyMap(self._params.heroGuid)
  for k, v in pairs(attributes) do
    local curValue = property[k] or 0
    if curValue < 0 then
      curValue = 0
    end
    local nextValue = curValue + v
    local tpl = battleInfoTpl:getTplById(k)
    table.insert(data, {
      toggle_state = true,
      curAttr = battleInfoTpl:getShowTxtComma(k, curValue),
      nextAttr = battleInfoTpl:getShowTxtComma(k, nextValue),
      name = battleInfoTpl:getName(tpl),
      icon = battleInfoTpl:getIcon(tpl),
      active_icon = true
    })
  end
  self.bind.propList:clear()
  self.bind.propList:insert_array(data)
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
