local this = class("moduleProperty", require("ui.pages.HeroPanel.moduleHeroBase"))

function this.bind()
  return {
    go_pet = false,
    isOn_hasPet = false,
    module_pixelIcon = {
      moduleName = "pages/pet/modulePetPixelIcon"
    },
    txt_petLevel = nil,
    txt_petName = nil,
    heroTotalProperty = {
      moduleName = "pages/heroPanel/property/heroTotalProperty"
    },
    heroModuleLevelUp = {
      moduleName = "pages/heroPanel/property/heroModuleLevelUp"
    },
    go_heroTotalProperty = true,
    go_heroModuleLevelUp = false,
    go_resetModBtn = false,
    txt_petFavorLv = "",
    go_petFavor = false
  }
end

function this.methods()
  return {
    onClick_resetMod = function(self)
      local canMod = L_GameUtil.checkCanMod()
      if canMod then
        L_UI:open("pageMOD")
      end
    end,
    onClick_pet = function(self)
      if L_BattleDataManager:checkPlayerBattle() then
        L_FlyMsgManager:showNormalMsg(L_BattleDataManager:getBattleBlockTips(L_Const.BattleBlockType.FormationSetting))
        return
      end
      if L_FormationStore:getIsTrailFormation(L_FormationManager:getCurFormationType()) or L_DungeonManager:getIsInDungeon() then
        L_FlyMsgManager:showNormalMsg(L_WordsTpl:getValue("notice_pageFormationMain_01"))
        return
      end
      L_UI:open("pageWearPet", {
        heroGuid = self.heroGuid
      })
    end,
    onClick_petFavor = function(self)
      if not math.isEmpty(self._petGuid) then
        L_PetStore:setCurSelectPetId(self._petGuid)
      end
      L_UI:open("pagePetBox")
    end
  }
end

function this:created(...)
  this.super.created(self, ...)
end

function this:open()
  self.isLevelUp = false
  self:refreshHeroProperty()
  self._ESyncHeroServerDataHandler = handler(self, self.onEvent_refreshHero)
  AzurWorld.heroMgr:RegisterEvent(C_EHeroEvent.ESyncHeroServerData, self._ESyncHeroServerDataHandler)
  L_PetStore:listenCallFunc(L_PetStore.event.refreshPet, self.onRefreshPet, self, nil, 0)
end

function this:close()
  AzurWorld.heroMgr:UnregisterEvent(C_EHeroEvent.ESyncHeroServerData, self._ESyncHeroServerDataHandler)
  L_PetStore:unListenCallFunc(L_PetStore.event.refreshPet, self.onRefreshPet)
end

function this:onEvent_refreshHero(enum, varList)
  local hero = varList:GetObject(0)
  if L_HeroStore:getIsPlayLevelAnim() then
    return
  end
  if L_HeroStore:getHeroGuid(hero) ~= self.heroGuid then
    return
  end
  self:refreshHeroProperty()
end

function this:setCommonTop(bool)
  self.parent:setCommonTop(bool)
end

function this:onClickLevelUp()
  if self:isPreviewMode() then
    return
  end
  self.parent:setDownBg(false)
  self.parent:refreshCanChangeHero(false)
  self.isLevelUp = true
  self.modules.heroModuleLevelUp:onOpenExp()
  self:refreshHeroProperty()
  self.parent:setCommonTopData(handler(self, self.onExitLevelUp), L_WordsTpl:getValue("ui_spirit_level_up"))
end

function this:onExitLevelUp()
  self.parent:setDownBg(true)
  self.parent:refreshCanChangeHero(true)
  self.isLevelUp = false
  self:refreshHeroProperty()
  self.modules.heroModuleLevelUp:onCloseExpBtn()
end

function this:refreshHeroProperty()
  self.heroGuid = self:getModeController():getSelectGuid()
  self.heroConfigId = self:getModeController():getSelectConfigId()
  local playAnim = false
  if self.bind.go_heroTotalProperty and self:checkIsLevelUp() then
    self.bindComponents.switchAnimator:SetTrigger("ToLevelUp")
    self.bind.go_heroModuleLevelUp = self:checkIsLevelUp()
    playAnim = true
  end
  if self.bind.go_heroModuleLevelUp and not self:checkIsLevelUp() then
    self.bindComponents.switchAnimator:SetTrigger("ToTotal")
    self.bind.go_heroTotalProperty = not self:checkIsLevelUp()
    playAnim = true
  end
  if playAnim then
    Timer.once(0.24, function()
      self.bind.go_heroTotalProperty = not self:checkIsLevelUp()
      self.bind.go_heroModuleLevelUp = self:checkIsLevelUp()
    end, self)
  else
    self.bind.go_heroTotalProperty = not self:checkIsLevelUp()
    self.bind.go_heroModuleLevelUp = self:checkIsLevelUp()
  end
  local unlockMod = L_SystemUnlockManager:check(L_SystemConst.enum.mod)
  self.bind.go_resetModBtn = self.heroGuid == L_HeroStore:getDefaultHeroGuid() and self.parent.options.pageType ~= L_Const.heroPageType.DetailMode and unlockMod
  if self:checkIsLevelUp() then
    self.modules.heroModuleLevelUp:refreshHeroProperty()
    self.modules.heroTotalProperty:skillListSet()
  else
    self.modules.heroTotalProperty:refreshHeroProperty()
  end
  self:initPetInfo()
end

function this:checkIsLevelUp()
  return self.isLevelUp
end

function this:initPage(params)
  this.super.initPage(self)
  self.isLevelUp = false
  self:refreshHeroProperty()
  if params and params.openLevelUp then
    self:onClickLevelUp()
  end
end

function this:playTotalPreviewAnimation(isMax)
  if self:isPreviewMode() then
    self.modules.heroTotalProperty:playTotalPreviewAnimation(isMax)
  end
end

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

function this:lvUpBtnAndAdvanceBtnObj(bool1)
  local isPreview = self:isPreviewMode()
  self.bind.lvUpBtn = bool1 and not isPreview
end

function this:initPetInfo()
  local formationSystemUnlock = L_SystemUnlockStore:getSystemIsOpen(L_SystemConst.enum.formation)
  self.bind.go_pet = not self:checkIsLevelUp() and not self:isPreviewMode() and formationSystemUnlock
  if self.bind.go_pet then
    local hero = L_HeroStore:getHero(self.heroGuid)
    self._petGuid = L_HeroStore:getHeroPetGuid(hero)
    local isEquipped = not math.isEmpty(self._petGuid)
    self.bind.isOn_hasPet = isEquipped
    if isEquipped then
      local pet = L_PetStore:getPetItem(self._petGuid)
      self.modules.module_pixelIcon:setGuid(self._petGuid, true)
      self.bind.txt_petLevel = tostring(pet.lv)
      self.bind.txt_petName = pet.pet_name
      local result = L_SystemUnlockStore:getSystemIsOpen(L_SystemConst.enum.petFavor)
      self.bind.go_petFavor = result
      if result then
        self.bind.txt_petFavorLv = tostring(pet:petFavorLv())
      end
    end
  end
end

function this:onRefreshPet()
  self:initPetInfo()
end

function this:playSwitchAnimator(name)
  if self.bindComponents.switchAnimator then
    self.bindComponents.switchAnimator:SetTrigger(name)
  end
end

return this
