local this = class("pageSpiritronAdvance", G_UIPageBase)
local _battleInfoTpl = L_GameTpl:getBattleInfoTpl()
local spiritronRankTpl = L_GameTpl:getSpiritronRankTpl()

function this.bind()
  return {
    txt_name = "",
    curLv = "",
    advanceLv = "",
    lvRequire = "",
    txt_cost = "0",
    txt_skill = "",
    star = false,
    leftW = C_Vector2(0, 0),
    rightW = C_Vector2(0, 0),
    scrollList_property = {
      moduleName = "pages/spiritron/cellSpiritronProp"
    },
    module_currency = {
      moduleName = "modulePages/moduleCurrency"
    },
    advancePropItem = {
      moduleName = "modulePages/cellIcon"
    },
    moduleCommonTop = {
      moduleName = "modulePages/moduleCommonTop"
    },
    spiritronCardItem = {
      moduleName = "pages/Spiritron/cellSpiritronCardItem"
    },
    advanceStar = {
      moduleName = "pages/HeroPanel/hero/heroStarItem"
    }
  }
end

function this.methods()
  return {
    onClick_rank = function(self)
      if not self.boolGold then
        L_HeroManager:heroHintInfo(1)
        return
      end
      if not self.boolProp then
        L_HeroManager:heroHintInfo(3)
        return
      end
      L_SpiritronStore:req_rankUpSpiritron(self.spiritronGuid, function()
        L_FlyMsgManager:showNormalMsg(L_WordsTpl:getValue("notice_spiritron_breaksuccess"))
        L_UI:close(self.pageName)
      end)
    end
  }
end

function this:preOpen(options)
  this.super.preOpen(self, options)
  local data = {
    name = "\231\170\129\231\160\180",
    mainBtnBool = false,
    closeCurPage = function()
      L_UI:close(self.pageName)
    end
  }
  self.modules.moduleCommonTop:initModule(data)
  self.spiritronGuid = options.weaponGuid
  local tpl_sys = L_GameTpl:getSystemUnlockTpl():getTplById(L_SystemConst.enum.spiritronAdvance)
  self.modules.module_currency:refreshRes(tpl_sys)
  self:initCostInfo()
  self:initSpiritronInfo()
end

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

function this:initCostInfo()
  local serverData = L_SpiritronStore:getSpiritronItem(self.spiritronGuid)
  local rankData = spiritronRankTpl:getChildren(serverData.spiritronId)
  self.curRank = serverData.rank
  self.boolProp = true
  self.boolGold = true
  local itemList = rankData[self.curRank].rankUpItem
  local data = {}
  for i = 1, #itemList do
    local itemId = itemList[i][1]
    local num = itemList[i][2]
    local currNum = L_BagStore:getItemNum(itemId)
    if currNum < itemList[i][2] then
      self.boolProp = false
      currNum = L_GameUtil.fillColor(currNum, L_Const.colorHtml.red001)
    end
    table.insert(data, {
      itemNumTxt = tostring(currNum) .. "/" .. num,
      itemType = L_Const.resType.commonItem,
      itemId = itemId
    })
  end
  self.bind.advancePropItem:clear()
  self.bind.advancePropItem:insert_array(data)
  if L_PlayerStore:getCurrencyNum(L_Const.currencyType.gold) >= rankData[self.curRank].rankUpCoin then
    self.bind.txt_cost = tostring(rankData[self.curRank].rankUpCoin)
  else
    self.bind.txt_cost = L_GameUtil.fillColor(rankData[self.curRank].rankUpCoin, L_Const.colorHtml.red01)
    self.boolGold = false
  end
end

function this:initSpiritronInfo()
  local serverData = L_SpiritronStore:getSpiritronItem(self.spiritronGuid)
  local rankData = spiritronRankTpl:getChildren(serverData.spiritronId)
  local item = L_ItemManager:parseSpiritronItem(serverData.spiritronId)
  self.curLv = serverData.spiritronLv
  self.modules.spiritronCardItem:setId(serverData.spiritronId)
  self.bind.txt_name = item.name
  self.bind.curLv = string.format("Lv.%s/%s", serverData.spiritronLv, rankData[self.curRank].rankLevelLimit)
  self.bind.advanceLv = string.format("Lv.%s/%s", serverData.spiritronLv, rankData[self.curRank + 1].rankLevelLimit)
  self.bind.lvRequire = "Lv." .. serverData.spiritronLv
  local curPropertyList = L_SpiritronManager:getConfigAttList(serverData.spiritronId, serverData.spiritronLv, self.curRank)
  local upPropertyList = rankData[self.curRank + 1].rankUpAttribute
  local proList = {}
  for k, v in pairs(curPropertyList) do
    local valueTxt = _battleInfoTpl:getShowPropertyTxt_spiritron(k, v)
    if upPropertyList[k] and 0 < upPropertyList[k] then
      local upShowValue = _battleInfoTpl:getShowPropertyTxt_spiritron(k, upPropertyList[k])
      valueTxt = valueTxt .. L_GameUtil.fillColor(" +" .. upShowValue, L_Const.colorHtml.blue001)
    end
    table.insert(proList, {id = k, valueShowText = valueTxt})
  end
  L_SpiritronManager:sortAttrId(proList)
  for i, data in ipairs(proList) do
    data.bg = i % 2 == 1
  end
  self.bind.scrollList_property:clear()
  self.bind.scrollList_property:insert_array(proList)
  self.bind.txt_skill = L_SpiritronManager:getSkillDescByGuid(self.spiritronGuid)
  local showStars = serverData.star
  local weight = 20 * showStars
  self.bind.leftW = C_Vector2(270 - weight, 4)
  self.bind.rightW = C_Vector2(270 - weight, 4)
  self.bind.star = true
  self.bind.advanceStar:clear()
  for i = 1, showStars do
    self.bind.advanceStar:insert(i, {})
  end
end

return this
