local this = class("pageHomeCenter", G_UIPageBase)
local PageConfig = require("ui.pages.home.homeCenter.pageHomeCenterConfig")

function this.bind()
  return {
    toggleModule = {
      type = "toggleModule",
      moduleHomeCenterPetManage = {
        moduleName = "pages/home/homeCenter/moduleHomeCenterPetManage/moduleHomeCenterPetManage",
        assetName = "UI/Pages/Home/HomeCenter/ModuleHomeCenterPetManage/moduleHomeCenterPetManage"
      },
      moduleHomeCenterBuildingManage = {
        moduleName = "pages/home/homeCenter/moduleHomeCenterBuildingManage/moduleHomeCenterBuildingManage",
        assetName = "UI/Pages/Home/HomeCenter/ModuleHomeCenterBuildingManage/moduleHomeCenterBuildingManage"
      }
    },
    toggleModuleName = "",
    active_changeTo = true,
    toggle_changeTo = true
  }
end

function this.methods()
  return {
    onClick_closeBtn = function(self)
      self:escHandle()
    end,
    onClick_btn_toggleModuleChangePart = function(self)
      self:onClickToggleModuleChangePartBtn()
    end
  }
end

function this:preOpen(options)
  this.super.preOpen(self, options)
  self:initData()
  self:initSystemUnlock()
  self:initPage()
  L_UI:addListener(L_UI.pageEvent.hided, self.onPreHide, self)
  self._isOpen = true
  self.openTime = L_TimeUtil.getServerTime()
  self:reportLog(true)
end

function this:close()
  self:disposeSystemUnlock()
  L_TimerManager:stopTimer(self, "toggleModuleHide")
  L_UI:removeListener(L_UI.pageEvent.hided, self.onPreHide, self)
  self.closeTime = L_TimeUtil.getServerTime()
  self:reportLog()
end

function this:hideChangeButton(animBind, force)
  local anim = self.bindComponents[animBind]
  if force then
    L_TimerManager:stopTimer(self, "toggleModuleHide")
  end
  local hasTimer = self._timer and self._timer.running
  if anim.gameObject.activeSelf and not hasTimer then
    anim:Play("anim_homecenter_change_hide")
    self._timer = L_TimerManager:newOrResetTimer(self, "toggleModuleHide", function()
      anim.gameObject:SetActive(false)
    end, 0.18)
  end
end

function this:onPreHide(pageName)
  if pageName == "pageHomeCenter" then
    if self.bind.toggleModuleName == "moduleHomeCenterPetManage" then
      self:hideChangeButton("changeToTagAnim")
    else
      self:hideChangeButton("changeToPetAnim")
    end
  end
end

function this:hide()
  L_TimerManager:stopTimer(self, "toggleModuleHide")
end

function this:show()
  if self._isOpen then
    self._isOpen = nil
    return
  end
  if self.bind.toggleModuleName == "moduleHomeCenterPetManage" then
    self.bindComponents.changeToPetAnim.gameObject:SetActive(false)
  else
    self.bindComponents.changeToTagAnim.gameObject:SetActive(false)
  end
  self:refreshToggleModuleChangePart()
end

function this:initData()
end

function this:initPage()
  self:changeToggleModule("moduleHomeCenterPetManage")
  self:refreshToggleModuleChangePart()
  self.curToggleModule:showAnim("anim_homecenter_pet_in")
end

function this:changeToggleModule(toggleModuleName)
  self.bind.toggleModuleName = toggleModuleName
  self.curToggleModule = self.modules.toggleModule[toggleModuleName]
  self.curToggleModule:refreshData()
  self.curToggleModule:refreshUI()
  self.curToggleModule.bindComponents.rectTrans:SetAsLastSibling()
end

function this:refreshToggleModuleChangePart()
  local petAnim = self.bindComponents.changeToPetAnim
  local tagAnim = self.bindComponents.changeToTagAnim
  if self.bind.toggleModuleName == "moduleHomeCenterPetManage" then
    tagAnim.gameObject:SetActive(true)
    tagAnim:Stop()
    tagAnim:Play("anim_homecenter_change_show")
    self:hideChangeButton("changeToPetAnim", true)
    self.bind.toggle_changeTo = false
  else
    petAnim.gameObject:SetActive(true)
    petAnim:Stop()
    petAnim:Play("anim_homecenter_change_show")
    self:hideChangeButton("changeToTagAnim", true)
    self.bind.toggle_changeTo = true
  end
end

function this:onClickToggleModuleChangePartBtn()
  local targetToggleModuleName = self.bind.toggleModuleName == "moduleHomeCenterPetManage" and "moduleHomeCenterBuildingManage" or "moduleHomeCenterPetManage"
  self:changeToggleModule(targetToggleModuleName)
  self:refreshToggleModuleChangePart()
end

function this:initSystemUnlock()
  L_SystemUnlockStore:listenCallFunc(L_SystemUnlockStore.event.updateSystem, self.refreshSystemUnlock, self)
  self:refreshSystemUnlock()
end

function this:refreshSystemUnlock(id)
  if id ~= nil and id ~= L_SystemConst.enum.homeCenterProduction and id ~= L_SystemConst.enum.homeCenterCrop and id ~= L_SystemConst.enum.homeCenterCollection then
    return
  end
  if L_SystemUnlockManager:check(L_SystemConst.enum.homeCenterProduction) or L_SystemUnlockManager:check(L_SystemConst.enum.homeCenterCrop) or L_SystemUnlockManager:check(L_SystemConst.enum.homeCenterCollection) then
    self.bind.active_changeTo = true
  else
    self.bind.active_changeTo = false
  end
end

function this:disposeSystemUnlock()
  L_SystemUnlockStore:unListenCallFunc(L_SystemUnlockStore.event.updateSystem, self.refreshSystemUnlock, self)
end

function this:check(options, callback)
end

function this:escHandle()
  if self.curToggleModule and self.curToggleModule.escHandle then
    if self.curToggleModule:escHandle() then
      self.curToggleModule:homeCenterEscHandleCallback(self.pageName)
    end
    return
  end
  L_UI:close(self.pageName)
end

function this:reportLog(isOpen)
  local log = {}
  if isOpen then
    log.act_type = 1
  else
    log.act_type = 2
    log.duration = self.closeTime - self.openTime
  end
  L_GameUtil.logEvent(L_Const.logEventName.client_home_center, log)
end

return this
