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

function this.bind()
  return {
    previewActive = true,
    spiritInfo = {
      moduleName = "pages/heroPanel/spirit/moduleSpiritInfo"
    },
    bgActive = false,
    spiritNullActive = true,
    bottomActive = false,
    lockActive = false,
    unlockActive = false
  }
end

function this.methods()
  return {
    onClick_addSpiritron = function(self)
      L_SpiritronManager:openSpiritSwitch({
        spiritGuid = self.spiritGuid,
        heroGuid = self.heroGuid
      })
    end,
    onValueChanged_togLevelPreview = function(self, value)
      self:toggleLevelPreview(value)
    end
  }
end

function this:open(options)
  L_SpiritronStore:listenCallFunc(L_SpiritronStore.event.switchSpirit, self.initPage, self)
  L_UI:addListener(L_UI.pageEvent.preOpen, self.onPageOpen, self)
  L_UI:addListener(L_UI.pageEvent.closed, self.onPageClose, self)
  L_TopBarManager:setTopBarBackName(L_WordsTpl:getValue("ui_moduleHeroSpirit"))
end

function this:close()
  L_SpiritronStore:unListenCallFunc(L_SpiritronStore.event.switchSpirit, self.initPage, self)
  L_UI:removeListener(L_UI.pageEvent.preOpen, self.onPageOpen, self)
  L_UI:removeListener(L_UI.pageEvent.closed, self.onPageClose, self)
end

function this:onPageOpen(pageName)
  if pageName == "pageHeroSpiritUpgrade" or pageName == "pageHeroSpiritSwitch" or pageName == "pageSpiritronInfo" or pageName == "pageSpiritronInfoLow" then
    self.bind.previewActive = false
    self.parent:setDownBg(false)
    self.parent:setCommonTop(false)
  end
end

function this:onPageClose(pageName)
  if not L_UI:checkPageOpen("pageHeroSpiritUpgrade") and not L_UI:checkPageOpen("pageHeroSpiritSwitch") and not L_UI:checkPageOpen("pageSpiritronInfo") and not L_UI:checkPageOpen("pageSpiritronInfoLow") then
    self.bind.previewActive = true
    self.parent:setDownBg(true)
    self.parent:setCommonTop(true)
  end
end

function this:initPage()
  self.heroGuid = self:getModeController():getSelectGuid()
  self.spiritGuid = self:getCurrHeroSpiritronGuid()
  self.isPreview = self:isPreviewMode()
  self.modules.spiritInfo:initModule({
    heroGuid = self.heroGuid,
    spiritGuid = self.spiritGuid,
    isPreview = self.isPreview
  })
  self.modules.spiritInfo:setUpBtnActive(true)
  if self.isPreview then
    self.bindComponents.reddotNormal.gameObject:SetActive(false)
    self:initlockActive()
  else
    L_ReddotManager:registerReddot(self.bindComponents.reddotNormal, string.format(L_ReddotManager.DotDef.HeroSpirit, self.heroGuid))
  end
  self.bind.spiritNullActive = math.isEmpty(self.spiritGuid)
  self.bind.bgActive = self.isPreview
  self.bind.bottomActive = self.isPreview
end

function this:getCurrHeroSpiritronGuid()
  if self:isPreviewMode() then
    local guid = self:getModeController()._spiritronId * 10
    if self:getModeController():getIsMaxLevel() then
      guid = guid + 1
    end
    return guid
  else
    return L_HeroStore:getHeroSpiritronGuid(self:getHero(self.heroGuid))
  end
end

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

function this:getShowHeroAssets()
  return false
end

function this:toggleLevelPreview(isMax)
  self.isPreview = self:isPreviewMode()
  self:getModeController():setisMaxLevel(isMax)
  if self.isPreview then
    self.modules.spiritInfo:playPreviewMaxAnimation(isMax)
  end
  self.spiritGuid = self:getCurrHeroSpiritronGuid()
  self.heroGuid = self:getModeController():getSelectGuid()
  FrameScheduler.add(function()
    self.modules.spiritInfo:refreshLevel({
      spiritGuid = self.spiritGuid,
      heroGuid = self.heroGuid,
      isPreview = self.isPreview
    })
  end, 1)
end

function this:initlockActive()
  local spiritConfigId = self:getModeController()._spiritronId
  if not L_SpiritronStore:getHasSpiritron(spiritConfigId) then
    self.bind.lockActive = true
  else
    self.bind.unlockActive = true
  end
end

return this
