local this = class("moduleHeroStar", require("ui.pages.HeroPanel.moduleHeroBase"))
local _heroBreakTpl = L_GameTpl:getHeroBreakTpl()
local _skillTpl = L_GameTpl:getSkillTpl()
local _wordsTpl = L_GameTpl:getWordsTpl()
local _skillLevelTpl = L_GameTpl:getSkillLevelTpl()
local pos = {
  {x = -322.2, y = -3},
  {x = 293, y = -5.9},
  {x = 63.3, y = 83.1},
  {x = -167, y = 163},
  {x = 195.3, y = 249},
  {x = 4.2, y = 380.6}
}

function this.bind()
  return {
    cellHeroStarItem = {
      moduleName = "pages/HeroPanel/hero/cellHeroStarItem"
    },
    starBreak = false,
    skillDetails = false,
    skillDescribe = "",
    breakBtn = false,
    starNum = "",
    heroHalf = "",
    heroHalfPos = L_Vector3.new(0, 0),
    heroIcon = "",
    heroIconBg = "",
    heroIconFrame = ""
  }
end

function this.methods()
  return {
    cellHeroStarItem = {
      showSkill = function(self, bool, skillId, index)
        self.skillIndex = index
        self:skillDetails(bool, skillId)
      end
    },
    onClick_breakBtn = function(self)
      if self:isPreviewMode() then
        return
      end
      if self.skillIndex - self.heroStar == 1 then
      else
        L_FlyMsgManager:showNormalMsg(_wordsTpl:getTplById("notice_hero_lifeUnlock"))
        return
      end
      if self.upStar then
      else
        L_FlyMsgManager:showNormalMsg(_wordsTpl:getTplById("notice_common_lackItem"))
        return
      end
      if L_BattleDataManager:checkPlayerBattle() then
        L_FlyMsgManager:showNormalMsg(L_BattleDataManager:getBattleBlockTips(L_Const.BattleBlockType.HeroStarUpgrade))
        return
      end
      AzurWorld.heroMgr:ReqStarUpHero(self.heroGuid, function()
        L_PlayerManager:playHeroVoice(self.heroConfigId, L_Const.cvType.StarUp)
        self.bind.breakBtn = false
        L_UI:open("pageHeroStarAdvanceSuccess", {
          heroId = self.heroGuid,
          describe = self.bind.skillDescribe
        })
      end)
    end
  }
end

function this:open(...)
  self._ESyncHeroServerDataHandler = handler(self, self.refreshHeroStar)
  AzurWorld.heroMgr:RegisterEvent(C_EHeroEvent.ESyncHeroServerData, self._ESyncHeroServerDataHandler)
end

function this:close()
  AzurWorld.heroMgr:UnregisterEvent(C_EHeroEvent.ESyncHeroServerData, self._ESyncHeroServerDataHandler)
end

function this:initPage()
  this.super.initPage(self)
  self:upStarSet()
end

function this:upStarSet()
  self.heroGuid = self:getModeController():getSelectGuid()
  self.heroConfigId = self:getModeController():getSelectConfigId()
  self.skillId = 0
  local skillData = _heroBreakTpl:getBreakSkillList(_heroBreakTpl:getTplById(self.heroConfigId))
  local skillDataTemp = {}
  self.bind.cellHeroStarItem:clear()
  for i = 1, #skillData do
    local skillId = skillData[i]
    local skillDetail = {
      skillId = skillId,
      index = i,
      isPreview = self:isPreviewMode(),
      heroId = self.heroGuid,
      toggle = false,
      pos = pos[i],
      icon = "BSHeroStar/tex_herorankup_icon_0" .. i
    }
    table.insert(skillDataTemp, skillDetail)
  end
  self.bind.skillDetails = false
  self.bind.starBreak = false
  self.bind.cellHeroStarItem:insert_array(skillDataTemp)
  self:refreshHeroStar()
end

function this:refreshHeroStar()
  self.heroGuid = self:getModeController():getSelectGuid()
  self.heroConfigId = self:getModeController():getSelectConfigId()
  local hero = self:getHero(self.heroGuid)
  self.heroStar = L_HeroStore:getHeroStar(hero)
  for i = 1, #self.bind.cellHeroStarItem do
    local bool = false
    if i > self.heroStar then
      bool = true
    end
    self.bind.cellHeroStarItem:change(i, {unLock = bool})
  end
end

function this:skillDetails(bool, skillId)
  self.bind.skillDetails = true
  self.bind.skillDescribe = string.format("%s%s", _skillTpl:getName(_skillTpl:getTplById(skillId)), L_GameUtil.fillColor(_skillLevelTpl:getSkillDescribe(_skillLevelTpl:getTplByIdAndLevel(skillId, 1)), L_Const.colorHtml.grey004))
  if self:isPreviewMode() then
    bool = true
  end
  if bool then
    self.bind.breakBtn = false
  else
    self.bind.breakBtn = true
    for k, v in pairs(_heroBreakTpl:getBreakItemCost(_heroBreakTpl:getTplById(self.heroConfigId))) do
      for i = 1, #v do
        if 1 <= L_BagStore:getItemNum(v[2]) then
          self.bind.starNum = string.format("%s/%s", L_BagStore:getItemNum(v[2]), v[3])
          self.upStar = true
        else
          self.bind.starNum = string.format("<color=#fc2b2c>%s</color>/%s", L_BagStore:getItemNum(v[2]), v[3])
          self.upStar = false
        end
      end
    end
    local hero = self:getHero(self.heroGuid)
    local heroItem = L_ItemManager:parseHeroItem(L_HeroStore:getHeroConfigId(hero))
    local heroIconAvatar = L_GameUtil.getAvatarTexturePath(L_HeroStore:getHeroConfigId(hero), L_Const.resourceIndexOfAvatarTextureInHeroTpl.small)
    if self.heroGuid == L_HeroStore:getDefaultHeroGuid() then
      L_PhotoManager:bindPhotoEntity(self.bindComponents.heroIcon.gameObject, L_PlayerStore:getPlayerModHeadImgName())
    else
      self.bind.heroIcon = heroIconAvatar
    end
    self.bind.heroIconBg = heroItem.headBg
    self.bind.heroIconFrame = heroItem.heroFrame
  end
end

function this:getTimelineState()
  return L_Const.heroTimelineState.Skill
end

return this
