local this = class("pageSpiritBreakSuccess", G_UIPageBase)
local battleInfoTpl = L_GameTpl:getBattleInfoTpl()
local spiritronTpl = L_GameTpl:getSpiritronTpl()
local attrBaseName = {
  [L_Const.battleAtt.MAXHP] = "info_attr_basic_hp",
  [L_Const.battleAtt.ATK] = "info_attr_basic_atk",
  [L_Const.battleAtt.DEF] = "info_attr_basic_def",
  [L_Const.battleAtt.MDEF] = "info_attr_basic_mdef"
}

function this.bind()
  return {
    txt_spiritRank = "",
    advanceSucceedCurLv = "",
    advanceSucceedNextLv = "",
    advancePropertyList = {
      moduleName = "pages/HeroPanel/hero/heroProperty"
    },
    icon = ""
  }
end

function this.methods()
  return {
    onClick_closeAdvance = function(self)
      if self.callback then
        self.callback()
      end
      L_UI:close(self.pageName)
    end
  }
end

function this:preOpen(options)
  self.spiritGuid = options.spiritGuid
  self.curRank = options.curRank
  self.afterRank = options.afterRank
  self:InitRankUpPreview(self.curRank)
  self:setBreakView()
end

function this:close()
end

function this:setBreakView()
  local spData = L_SpiritronStore:getSpiritronItem(self.spiritGuid)
  local conf = L_ItemTplManager:getItemConfig(L_Const.resType.spiritron, spData.spiritronId)
  local cfg = spiritronTpl:getTplById(spData.spiritronId)
  self.bind.icon = spiritronTpl:getAvatarTexture(cfg, 1)
  local currentMaxLevel = L_SpiritronManager:getMaxLevelByRank(self.spiritGuid, self.curRank)
  local afterMaxLevel = L_SpiritronManager:getMaxLevelByRank(self.spiritGuid, self.afterRank)
  self.bind.advanceSucceedCurLv = tostring(currentMaxLevel)
  self.bind.advanceSucceedNextLv = tostring(afterMaxLevel)
  local curPropertyList = L_SpiritronManager:getConfigAttList(spData.spiritronId, spData.spiritronLv, self.curRank)
  local afterPropertyList = L_SpiritronManager:getConfigAttList(spData.spiritronId, spData.spiritronLv, self.afterRank)
  local propertyData = {}
  for k, v in pairs(afterPropertyList) do
    local addNum = v - curPropertyList[k]
    local currStr = battleInfoTpl:getShowPropertyTxt_spiritron(k, curPropertyList[k])
    local nextStr = battleInfoTpl:getShowTxtComma(k, afterPropertyList[k])
    local tpl_battleInfo = battleInfoTpl:getTplById(k)
    local strName = L_WordsTpl:getValue(attrBaseName[k])
    local name = string.isEmpty(strName) and battleInfoTpl:getName(tpl_battleInfo) or strName
    local attIcon = battleInfoTpl:getIcon(tpl_battleInfo)
    if not (addNum <= 0) then
      table.insert(propertyData, {
        toggle_state = false,
        curAttr = currStr,
        nextAttr = nextStr,
        name = name,
        icon = attIcon,
        active_icon = true,
        id = k
      })
    end
  end
  L_SpiritronManager:sortAttrId(propertyData)
  for i = 1, #propertyData do
    local bgBool = false
    if 0 < math.floor(i % 2) then
      bgBool = true
    end
    propertyData[i].toggle_state = not bgBool
  end
  self.bind.advancePropertyList:clear()
  self.bind.advancePropertyList:insert_array(propertyData)
end

function this:InitRankUpPreview(afterRank)
  for i = 1, #L_Const.Number2Roma do
    if i == afterRank then
      self.bind.txt_spiritRank = L_Const.Number2Roma[i]
    end
  end
end

return this
