local this = class("heroTotalProperty", G_UIModuleBase)
local _heroLevelTpl = L_GameTpl:getHeroLevelTpl()
local talentRankTpl = L_GameTpl:getTalentRankTpl()
local battleInfoTpl = L_GameTpl:getBattleInfoTpl()
local attId = {
  L_Const.battleAtt.MAXHP,
  L_Const.battleAtt.ATK,
  L_Const.battleAtt.DEF,
  L_Const.battleAtt.MDEF
}

function this.bind()
  return {
    nation = "BSCommon/tex_icon_nation_01",
    heroName = "",
    currentLv = "",
    lvSlider = 0,
    lvUpBtn = true,
    heroPropertyItem = {
      moduleName = "pages/Pet/cellAttrItem"
    },
    moduleHeroShowStar = {
      moduleName = "pages/HeroPanel/moduleHeroShowStar"
    },
    professionIcon = "",
    nameDecalText = "",
    skillList = {
      moduleName = "pages/heroPanel/skill/cellHeroSkillIcon"
    },
    propetyPanel = {
      moduleName = "pages/propertyPanel/modulePropertyPanel"
    },
    levelBackgroundColor = C_Color.white,
    expValueText = "",
    professionImage = "",
    rankText = "",
    rankList = {}
  }
end

function this.methods()
  return {
    onClick_detailsBtn = function(self)
      local detailId = L_Const.propertyDetail.hero
      local isPreview = self.parent:isPreviewMode()
      if isPreview then
        detailId = L_Const.propertyDetail.heroPreview
      end
      self.modules.propetyPanel:initModule({
        isPreview = isPreview,
        heroData = self.parent:getHero(self:getHeroId()),
        detailId = detailId
      })
      self.modules.propetyPanel:setPanelActive(true)
    end,
    onClick_levelUpBtn = function(self)
      self.parent:onClickLevelUp()
    end,
    onClick_professionIcon = function(self)
      local heroId = self:getHeroId()
      local hero = self.parent:getHero(heroId)
      L_UI:open("pageElementalRestraint", {
        heroTplId = L_HeroStore:getHeroConfigId(hero),
        initTab = 1
      })
    end,
    skillList = {
      onClick = function(self, bind)
        local skillId = bind.skillId
        L_UI:open("pageSkillInfo", {
          isPreview = self.parent:isPreviewMode(),
          hero = self.parent:getHero(self:getHeroId()),
          skillId = skillId
        })
      end
    },
    onClick_TalentBtn = function(self)
      if self.parent:isPreviewMode() then
        return
      end
      local hero = self.parent:getHero(self:getHeroId())
      L_HeroManager:showHeroSelection(L_Const.heroModuleType.Talent, L_HeroStore:getHeroGuid(hero))
    end
  }
end

function this:getHeroLv()
  local hero = self.parent:getHero(self:getHeroId())
  return L_HeroStore:getHeroLevel(hero)
end

function this:getHeroId()
  return self.parent.heroGuid
end

function this:open()
  self.isFirstOpen = true
end

function this:refreshHeroProperty()
  if self.parent:isPreviewMode() and self.isFirstOpen then
    local heroId = self:getHeroId()
    local hero = self.parent:getHero(heroId)
    local lv = L_HeroStore:getHeroLevel(hero)
    if lv == 1 then
      self.bindComponents.heroPropertyAnimation:Play("anim_herodetail_init")
    end
    self.isFirstOpen = false
  end
  self:refreshHeroPropertyTotal()
  self:refreshHeroProfession()
end

function this:playTotalPreviewAnimation(isMax)
  if isMax then
    self.bindComponents.heroPropertyAnimation:Play("anim_herodetail_fresh100")
  else
    self.bindComponents.heroPropertyAnimation:Play("anim_herodetail_fresh1")
  end
end

function this:refreshHeroProfession()
  local heroTpl = L_GameTpl:getHeroTpl()
  local heroId = self:getHeroId()
  local hero = self.parent:getHero(heroId)
  local tpl = heroTpl:getTplById(L_HeroStore:getHeroConfigId(hero))
  local elementId = heroTpl:getElement(tpl)
  local elementTypeTpl = L_GameTpl:getElementTypeTpl()
  local position = heroTpl:getPosition(tpl)
  local professionTpl = L_GameTpl:getProfessionTpl()
  local professionIcon = professionTpl:getIcon(professionTpl:getTplById(position))
  self.bind.professionIcon = professionIcon
  self.bind.nameDecalText = heroTpl:getEnglishName(tpl)
  self.bind.professionImage = string.format("Atlas/ProfessionRectangle/tex_common_icon_bg_ty%s.png", elementId)
  local _, color = C_ColorUtility.TryParseHtmlString(elementTypeTpl:getProfessionRectangleColor(elementTypeTpl:getTplById(elementId)))
  color.a = 0.6
  self.bind.levelBackgroundColor = color
  local nationId = heroTpl:getNations(tpl)
  local groupsTpl = L_GameTpl:getGroupsTpl()
  local iconPath = groupsTpl:getIcon(groupsTpl:getTplById(nationId), L_Const.nationTextureIndex.nationLarge)
  if not string.isEmpty(iconPath) then
    self.bind.nation = iconPath
  end
end

function this:refreshHeroPropertyTotal()
  self.bind.gold = false
  self.bind.expCount = ""
  local heroId = self:getHeroId()
  local hero = self.parent:getHero(heroId)
  local rank = L_HeroStore:getHeroRank(hero)
  self.bind.rankText = L_Const.Number2Roma[rank]
  self.bindComponents.rankLevel_graphicMaterialSetter.ApplyChildren = false
  self.bindComponents.rankLevel_graphicMaterialSetter.ApplyChildren = true
  local valueList = {}
  local property = L_HeroStore:getHeroPropertyMap(heroId)
  local propertyData = {}
  if self.parent:isPreviewMode() then
    local heroPreview = L_AttributeManager:parseHeroConfigAttByHero(hero, L_HeroStore:getHeroLevel(hero))
    valueList = heroPreview
  else
    for i, v in pairs(attId) do
      local id, value = L_AttributeManager:getHeroAttValue(property, v, false, heroId)
      valueList[v] = value
    end
  end
  for i, v in pairs(attId) do
    table.insert(propertyData, {
      attrIcon = battleInfoTpl:getIcon(battleInfoTpl:getTplById(v)),
      value = L_HeroManager:addComma(valueList[v]),
      name = battleInfoTpl:getName(battleInfoTpl:getTplById(v))
    })
  end
  self.bind.heroPropertyItem:clear()
  self.bind.heroPropertyItem:insert_array(propertyData)
  if self.parent:isPreviewMode() then
    self.bind.heroName = L_HeroManager:getHeroName(heroId, heroId)
  else
    self.bind.heroName = L_HeroManager:getHeroName(heroId)
  end
  self.modules.moduleHeroShowStar:refreshStar(L_HeroStore:getHeroStar(hero))
  local lv = L_HeroStore:getHeroLevel(hero)
  self.bind.currentLv = self:lvChange(lv)
  self.maxLv = _heroLevelTpl:getMaxLvByCondition()
  local tpl = _heroLevelTpl:getTplById(L_HeroStore:getHeroLevel(hero))
  local rankCfg = talentRankTpl:getMaxTpl(L_HeroStore:getHeroConfigId(hero))
  local maxRank = talentRankTpl:getRank(rankCfg)
  self.bind.expValueText = string.format("%d/%d", L_HeroStore:getHeroExp(hero), _heroLevelTpl:getExp(tpl))
  if self.parent:isPreviewMode() then
    if lv == _heroLevelTpl:getMaxLv() then
      self.bind.lvSlider = 1
      self.bind.expValueText = "MAX"
    else
      self.bind.lvSlider = 0
    end
  elseif lv == _heroLevelTpl:getMaxLv() then
    self.bind.lvSlider = 1
    self.bind.lvUpBtn = false
    self.bind.expValueText = "MAX"
  elseif lv == self.maxLv then
    self.bind.lvSlider = 0
    self.bind.lvUpBtn = true
    local previousTpl = _heroLevelTpl:getTplById(L_HeroStore:getHeroLevel(hero))
    self.bind.expValueText = string.format("%d/%d", 0, _heroLevelTpl:getExp(previousTpl))
  else
    self.bind.lvSlider = L_HeroStore:getHeroExp(hero) / _heroLevelTpl:getExp(tpl)
    self.bind.lvUpBtn = true
  end
  if self.bind.lvUpBtn == true then
    self.bind.lvUpBtn = not self.parent:isPreviewMode()
  end
  self:skillListSet()
  self:refreshRankList(hero)
end

function this:lvChange(lv)
  if lv < 10 then
    return string.format("0%d", lv)
  end
  return tostring(lv)
end

function this:sliderChange(value)
  return 0.1 + value * 0.8
end

function this:skillListSet()
  self.bind.skillList:clear()
  local hero = self.parent:getHero(self:getHeroId())
  local skillMap = L_HeroManager:getHeroSystemSkillList(hero)
  local skillTpl = L_GameTpl:getSkillTpl()
  local skillLevelTpl = L_GameTpl:getSkillLevelTpl()
  local list = {}
  local index = 0
  for i, skill in ipairs(skillMap) do
    if 5 < i then
      break
    end
    local skillId = skill.skillId
    local tpl = skillTpl:getTplById(skillId)
    local level = skill.skillLevel
    local type = skillTpl:getSkillDisplayType(tpl)
    local isMaxLevel = level >= skillLevelTpl:getMaxLevelById(skillId)
    local levelTxt = isMaxLevel and L_WordsTpl:getValue("menu_common_lvMax") or string.format("%s%s", L_WordsTpl:getValue("info_char_lv"), level)
    table.insert(list, {
      levelTxt = levelTxt,
      levelNum = level,
      iconPath = skillTpl:getIcon(tpl),
      borderImgColor = L_Const.skillBorderIcon[type],
      isLevel = true,
      isblankBg = true,
      levelText = levelTxt,
      heroId = self.heroGuid,
      skillId = skillId,
      index = index,
      isPreview = true
    })
    index = index + 1
  end
  self.bind.skillList:insert_array(list)
end

function this:skillListChoice(id)
  for i = 1, #self.bind.skillList do
    if self.bind.skillList:getValue(i, "skillId") == id then
      self.bind.skillList:change(i, {choice = true})
    else
      self.bind.skillList:change(i, {choice = false})
    end
  end
end

function this:refreshRankList(hero)
  local rankTpl = talentRankTpl:getHeroRankTpl(L_HeroStore:getHeroConfigId(hero), L_HeroStore:getHeroRank(hero))
  local itemsNum = #talentRankTpl:getRankBreakthroughItem(rankTpl)
  local runes = L_HeroStore:getHeroRuneIds(hero)
  local temp = {}
  local activateRuneNum = 0
  for i = 1, itemsNum do
    table.insert(temp, {})
    if not math.isEmpty(runes[i]) then
      activateRuneNum = activateRuneNum + 1
    end
  end
  self.bind.rankList:clear()
  self.bind.rankList:insert_array(temp)
  for i = 1, #self.bind.rankList do
    local cell = self.bind.rankList:getItemCls(i)
    cell.bindComponents.toggle_activateRank.isOn = i <= activateRuneNum
  end
end

return this
