local this = class("pageHeroSpiritSwitch", G_UIPageBase)

function this.bind()
  return {
    bgActive = false,
    boardActive = true,
    spiritSwitch = {
      moduleName = "pages/heroPanel/spirit/moduleHeroSpiritSwitch"
    }
  }
end

function this.method()
  return {}
end

function this:preOpen(options)
  self.options = options
  L_SpiritronStore:listenCallFunc(L_SpiritronStore.event.switchSpirit, self.refreshUI, self)
  self.bind.bgActive = not options.hideBg
  self.modules.spiritSwitch:initModule(options)
end

function this:update()
  if self.bind.boardActive ~= not L_UI:checkPageOpen("pageHeroSpiritUpgrade") then
    self.bind.boardActive = not L_UI:checkPageOpen("pageHeroSpiritUpgrade")
  end
end

function this:close(options)
  L_SpiritronStore:unListenCallFunc(L_SpiritronStore.event.switchSpirit, self.refreshUI)
end

function this:onPageOpen(pageName)
  if pageName == "pageHeroSpiritUpgrade" then
    self.bind.boardActive = false
  end
end

function this:onPageClose(pageName)
  if not L_UI:checkPageOpen("pageHeroSpiritUpgrade") and pageName ~= "pageCommonSwitchTip" then
    self.bind.boardActive = true
    self:refreshUI(self.options)
  end
end

function this:closeUI()
  L_UI:close(self.pageName)
end

function this:refreshUI(options)
  self.options = options
  self.modules.spiritSwitch:initModule(options)
end

function this:getModuleData()
  return {
    name = L_WordsTpl:getValue("ui_spirit_switch_top_name"),
    helpBtnBool = false,
    mainBtnBool = true,
    closeCurPage = function()
      L_UI:close(self.pageName)
    end,
    helpBtn = function()
    end,
    mainBtn = function()
      L_UI:backToRoot()
    end
  }
end

function this:check(options, callback)
  if L_BattleDataManager:checkPlayerBattle() then
    L_FlyMsgManager:showNormalMsg(L_BattleDataManager:getBattleBlockTips(L_Const.BattleBlockType.SpiritWearEntrance))
    callback(false)
    return
  end
  local spiritCount = L_SpiritronStore:getSpiritronListCount()
  if spiritCount == 0 then
    L_FlyMsgManager:showNormalMsg(L_WordsTpl:getValue("ui_spirit_empty"))
    callback(false)
    return
  end
  callback(true)
end

return this
