local this = class("moduleSpiritProperty", G_UIModuleBase)
local battleInfoTpl = L_GameTpl:getBattleInfoTpl()
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 {
    propertyList = {
      moduleName = "pages/pet/cellAttrItem"
    }
  }
end

function this.methods()
  return {}
end

function this:initModule(spiritGuid, heroGuid, isPreviewMode)
  self.isPreviewMode = isPreviewMode
  self.spiritGuid = spiritGuid
  self.heroGuid = heroGuid
  local isEmpty = math.isEmpty(spiritGuid)
  if isEmpty then
    self:setEmpty()
  else
    self:setInfo()
  end
end

function this:setEmpty()
  local attributes = {
    5,
    1,
    3,
    4
  }
  local proList = {}
  for k, v in pairs(attributes) do
    local tpl_battleInfo = battleInfoTpl:getTplById(v)
    local strName = L_WordsTpl:getValue(attrBaseName[v])
    table.insert(proList, {
      id = v,
      value = "--",
      name = string.isEmpty(strName) and battleInfoTpl:getName(tpl_battleInfo) or strName,
      attrIcon = battleInfoTpl:getIcon(tpl_battleInfo)
    })
  end
  self.bind.propertyList:clear()
  self.bind.propertyList:insert_array(proList)
end

function this:setInfo()
  self:setPropertyList()
end

function this:setPropertyList()
  local serverData = L_SpiritronManager:getSpiritronItem(self.spiritGuid, self.isPreviewMode)
  if not serverData then
    errorf("\230\178\161\230\156\137\231\129\181\229\173\144guid" .. self.spiritGuid)
    return
  end
  local propertyList = L_SpiritronManager:getAttListByGuidByItem(serverData)
  local proList = {}
  for k, v in pairs(propertyList) do
    local tpl_battleInfo = battleInfoTpl:getTplById(k)
    local strName = L_WordsTpl:getValue(attrBaseName[k])
    table.insert(proList, {
      id = k,
      value = battleInfoTpl:getShowPropertyTxt_spiritron(k, v),
      name = string.isEmpty(strName) and battleInfoTpl:getName(tpl_battleInfo) or strName,
      attrIcon = battleInfoTpl:getIcon(tpl_battleInfo)
    })
  end
  L_SpiritronManager:sortAttrId(proList)
  self.bind.propertyList:clear()
  self.bind.propertyList:insert_array(proList)
end

function this:setCampareLevel(spiritGuid, heroGuid, isPreviewMode, afterLevel, rank)
  self.isPreviewMode = isPreviewMode
  self.spiritGuid = spiritGuid
  self.heroGuid = heroGuid
  local proList = {}
  local currPropertyList = L_SpiritronManager:getAttListByGuid(spiritGuid)
  local currSpData = L_SpiritronStore:getSpiritronItem(spiritGuid)
  rank = rank or currSpData.rank
  local afterPropertyList = L_SpiritronManager:getConfigAttList(currSpData.spiritronId, afterLevel, rank)
  for k, v in pairs(afterPropertyList) do
    local addNum = v - currPropertyList[k]
    local value = battleInfoTpl:getShowTxtComma(k, currPropertyList[k])
    local addValueText = value
    local showAddBg = false
    if 0 < addNum then
      showAddBg = true
      addValueText = battleInfoTpl:getShowTxtComma(k, v)
      goto lbl_46
      goto lbl_82
      ::lbl_46::
      local tpl_battleInfo = battleInfoTpl:getTplById(k)
      local strName = L_WordsTpl:getValue(attrBaseName[k])
      table.insert(proList, {
        id = k,
        name = string.isEmpty(strName) and battleInfoTpl:getName(tpl_battleInfo) or strName,
        attrIcon = battleInfoTpl:getIcon(tpl_battleInfo),
        value = value,
        addValueText = addValueText,
        showAddBg = showAddBg
      })
    end
    ::lbl_82::
  end
  L_SpiritronManager:sortAttrId(proList)
  self.bind.propertyList:clear()
  self.bind.propertyList:insert_array(proList)
end

return this
