local this = class("handleCenter")

function this:ctor()
  L_SceneManager:addListener(L_SceneManager.event.switchSceneStart, self.onEvent_switchSceneStartHandle, self, nil, 0)
  L_SceneManager:addListener(L_SceneManager.event.loadSceneEnd, self.onEvent_loadSceneEndHandle, self, nil, 0)
  L_SceneManager:addListener(L_SceneManager.event.switchSceneEnd, self.onEvent_switchSceneEndHandle, self, nil, 0)
  L_SceneManager:addListener(L_SceneManager.event.unloadSceneStart, self.onEvent_unloadSceneStartHandle, self, nil, 0)
  L_SceneManager:addListener(L_SceneManager.event.suspendCurScene, self.onEvent_suspendCurSceneHandle, self, nil, 0)
  L_SceneManager:addListener(L_SceneManager.event.resumeCurSceneEnd, self.onEvent_resumeCurSceneEndHandle, self, nil, 0)
  L_PlayerManager:addListener(L_PlayerManager.event.setMainControl, self.onEvent_setMainControlHandle, self, nil, 0)
  L_PlayerStore:listenCallFunc(L_PlayerStore.event.refreshModAvatar, self.onEvent_refreshModAvator, self, nil, 0)
  L_PlayerStore:listenCallFunc(L_PlayerStore.event.refreshPlayerMount, self.onEvent_refreshMount, self, nil, 0)
  L_PlayerStore:listenCallFunc(L_PlayerStore.event.refreshPlayerStatus, self.onEvent_refreshPlayerStatus, self, nil, 0)
  L_SettingStore:listenCallFunc(L_SettingStore.event.refreshSceneQuality, self.onEvent_refreshSceneQuality, self, nil, 0)
  L_UI:addListener(L_UI.pageEvent.closed, self.onEvent_closedPage, self)
  self.lastModule = nil
  self.updateTimer = L_GameTimer.new(self.update, L_Const.TIME_WORLDSCALE, -1, self)
end

function this:update()
  if not self:getCurPoxyIsActive() then
    return
  end
  L_PlayerManager:onUpdate(L_Const.TIME_WORLDSCALE)
end

function this:dispose()
  self:destroyCurProxy()
  L_CameraManager:setClearFlag(2)
end

function this:refreshPrxoy(module)
  self:initCurProxy(module)
end

function this:initCurProxy(module)
  if module == L_Const.worldModule.home then
    C_ExternalManager.DispatchEvent(C_EExternalEventType.PetHomeEnter)
  else
    C_ExternalManager.DispatchEvent(C_EExternalEventType.PetHomeExit)
  end
  self:destroyCurProxy(module)
  self.lastModule = module
  local cfg = L_Const.worldData[module]
  L_UI:openRoot(cfg.ui, nil)
  L_UIManager:closeAllUnStackPages()
end

function this:destroyCurProxy(nextModule)
  if self.lastModule == nil then
    return
  end
  local curCfg = L_Const.worldData[self.lastModule]
  if nextModule then
    local nextCfg = L_Const.worldData[nextModule]
    if curCfg.ui ~= nextCfg.ui and curCfg.exitClose then
      L_UI:close(curCfg.ui)
    end
  elseif curCfg.exitClose then
    L_UI:close(curCfg.ui)
  end
  self.updateTimer:stop()
  L_ResPool:forceGarbage()
end

function this:getCurModule()
  return self.lastModule
end

function this:getCurPoxyIsActive()
  return AzurWorldInstance.WorldStarted
end

function this:onEvent_switchSceneStartHandle(sceneId)
  local camController = L_CameraManager:switchToWorld()
  camController:initVirCamera()
  L_CameraManager:setClearFlag(1)
end

function this:onEvent_loadSceneEndHandle(sceneId)
end

function this:onEvent_unloadSceneStartHandle()
  L_PlayerManager:dispose()
  L_EntityManager:dispose()
end

function this:onEvent_switchSceneEndHandle(sceneId)
  local curSceneId = AzurWorldInstance.CurWorldId
  if curSceneId ~= sceneId then
    return
  end
  local controlEntity = L_PlayerManager:getMainControlEntity()
  local operatorSendSystem = L_PlayerManager:getOperatorSendSystem()
  if operatorSendSystem then
    operatorSendSystem:syncPosition()
  end
  C_SmartTriggerEvent:Dispatch(C_SmartTriggerType.SceneLoaded)
  local csEntity = controlEntity:getCsEntity()
  csEntity.data:SetAnimatorSpeed(1)
end

function this:onEvent_closedPage(pageName)
  if pageName == "loading" then
    Timer.once(1, function()
      C_VoiceEvent:Dispatch(C_VoiceType.EVoiceGreetingMorning)
      C_VoiceEvent:Dispatch(C_VoiceType.EVoiceGreetingAfternoon)
      C_VoiceEvent:Dispatch(C_VoiceType.EVoiceGreetingEvening)
    end)
  end
end

function this:onEvent_suspendCurSceneHandle(sceneId)
  local curSceneId = AzurWorldInstance.CurWorldId
  if curSceneId ~= sceneId then
    return
  end
end

function this:onEvent_resumeCurSceneEndHandle(sceneId)
end

function this:onEvent_setMainControlHandle(main, sou)
  if AzurWorld.photoMgr and AzurWorld.photoMgr.unitAdapter then
    return
  end
  local getCameraMode = L_CameraManager:getCameraMode()
  if getCameraMode == L_Const.cameraMode.npcEntrustTask then
    return
  end
  L_CameraManager:bindEntity(main, true)
end

function this:onEvent_refreshModAvator()
  local mainHero = L_PlayerManager:getMainHeroEntity()
  if mainHero == nil then
    return
  end
  L_CameraManager:refreshVirCameraOffset(mainHero)
end

function this:onEvent_refreshMount()
  if not self:getCurPoxyIsActive() then
    return
  end
  AzurWorld.playerMgr.myPlayerUnitAdapter:SwitchToMountMainControl()
end

function this:onEvent_refreshPlayerStatus(status)
  if not self:getCurPoxyIsActive() and not L_BattleStore:getIsInBattle() then
    return
  end
  local curStatus = status.cur
  local nextStatus = status.next
  local move = status.move
  local reason = status.reason
  if reason == L_Const.WorldMapChangeStatusReason.WMCSR_DIE and curStatus == L_Const.WorldMapPlayerStatusType.WMPST_MOUNT then
    return
  end
  if curStatus == L_Const.WorldMapPlayerStatusType.WMPST_MOUNT then
    if not C_MountUtil.CheckSceneCanMount() then
      return
    end
    if move ~= nil and move.move_status == L_Const.serverMoveType.transfer then
      AzurWorld.mountMgr:TempDispatchLeaveMount(true)
    end
  elseif curStatus == L_Const.WorldMapPlayerStatusType.WMPST_FISH then
    L_FishingManager:switchOutFishingMode()
  end
  if nextStatus == L_Const.WorldMapPlayerStatusType.WMPST_NORMAL then
    AzurWorld.formationMgr:TempDispatchSwitchToFormation()
  elseif nextStatus == L_Const.WorldMapPlayerStatusType.WMPST_MOUNT then
    AzurWorld.mountMgr:TempDispatchRideMount()
  elseif nextStatus == L_Const.WorldMapPlayerStatusType.WMPST_FISH then
    L_FishingManager:switchToFishingMode()
  end
end

function this:onEvent_refreshSceneQuality()
  if not self:getCurPoxyIsActive() or self.isQualityRefresh then
    return
  end
  self.isQualityRefresh = true
end

function this:onEvent_formationRefresh()
  L_PlayerManager:refreshUsingFormation()
end

return this
