local this = class("moduleSpiritBreak", G_UIModuleBase)

function this.bind()
  return {
    beforeRomanNumActive = true,
    beforeRomanNum = "",
    afterRomanNum = "",
    beforeLevelText = "",
    afterLevelText = "",
    moduleSpiritProperty = {
      moduleName = "pages/heroPanel/spirit/moduleSpiritProperty"
    },
    costList = L_Const.ModuleInfo.CellIconSquare,
    unlockDesActive = false,
    breakBtnActive = true,
    unlockDes = "",
    breakBtn = L_Const.ModuleInfo.ButtonSingle
  }
end

function this.methods()
  return {
    onClick_breakBtn = function(self)
      if self.lackGlod then
        L_HeroManager:heroHintInfo(1)
        return
      end
      if self.lackMaterial then
        L_HeroManager:heroHintInfo(3)
        return
      end
      local currData = L_SpiritronStore:getSpiritronItem(self.spiritGuid)
      local curRank = currData.rank
      L_SpiritronStore:req_rankUpSpiritron(self.spiritGuid, function()
        local spData = L_SpiritronStore:getSpiritronItem(self.spiritGuid)
        L_UI:open("pageSpiritBreakSuccess", {
          spiritGuid = self.spiritGuid,
          curRank = curRank,
          afterRank = spData.rank
        })
      end)
    end
  }
end

function this:initModule(data)
  self.heroGuid = data.heroGuid
  self.spiritGuid = data.spiritGuid
  self.isPreview = data.isPreview
  self:refreshUI()
end

function this:refreshUI()
  self.lackMaterial = false
  local spData = L_SpiritronStore:getSpiritronItem(self.spiritGuid)
  local item = L_ItemTplManager:getSpiritronItem(spData.spiritronId)
  local showRank = spData.rank - 1
  self.bind.beforeRomanNumActive = 0 < showRank
  if self.bind.beforeRomanNumActive then
    self.bind.beforeRomanNum = "BSHero/tex_herodetail_dec_jinjie" .. showRank
  end
  self.bind.afterRomanNum = "BSHero/tex_herodetail_dec_jinjie" .. showRank + 1
  local currentMaxLevel = L_SpiritronManager:getMaxLevel(self.spiritGuid)
  local afterMaxLevel = L_SpiritronManager:getMaxLevelByRank(self.spiritGuid, spData.rank + 1)
  self.bind.beforeLevelText = L_WordsTpl:getValue("ui_spirit_level_show", {
    [0] = spData.spiritronLv,
    [1] = currentMaxLevel
  })
  self.bind.afterLevelText = L_WordsTpl:getValue("ui_spirit_level_show", {
    [0] = spData.spiritronLv,
    [1] = afterMaxLevel
  })
  self.modules.moduleSpiritProperty:setCampareLevel(self.spiritGuid, self.heroGuid, self.isPreview, spData.spiritronLv, spData.rank + 1)
  local spiritronRankTpl = L_GameTpl:getSpiritronRankTpl()
  local rankTpl = spiritronRankTpl:getTplByIdAndRank(spData.spiritronId, spData.rank)
  local itemList = spiritronRankTpl:getRankUpItem(rankTpl)
  local needCost = spiritronRankTpl:getRankUpCoin(rankTpl)
  local data = {}
  for i = 1, #itemList do
    local itemId = itemList[i][1]
    local num = itemList[i][2]
    local currNum = L_BagStore:getItemNum(itemId)
    if num > currNum then
      self.lackMaterial = true
      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.costList:clear()
  self.bind.costList:insert_array(data)
  self.lackGlod = needCost > L_PlayerStore:getCurrencyNum(L_Const.currencyType.gold)
  self.modules.breakBtn:setData(L_WordsTpl:getValue("ui_spirit_break"), nil, {
    itemType = L_Const.resType.currency,
    itemId = L_Const.currencyType.gold,
    txtNum = needCost
  })
  local condition = spiritronRankTpl:getCondition(rankTpl)
  local bComplete = true
  if not table.isEmpty(condition) then
    bComplete = L_ConditionManager:singleIsComplete(condition)
  end
  self.bind.unlockDesActive = not bComplete
  self.bind.breakBtnActive = bComplete
  if not bComplete then
    self.bind.unlockDes = L_ConditionManager:getSingleDesc(condition)
  end
  printf("rank", spData.rank)
end

return this
