local this = class("battleFieldEventManager", G_EventManagerBase)
table.merge(this, require(L_R.battle .. "battleFieldEventManager_interim"))
table.merge(this, require(L_R.battle .. "battleFieldEventManager_local"))

function this:ctor()
  this.super.ctor(self)
end

function this:init()
  self:ctor_interim()
  self:ctor_local()
  self.onEvent_battleEnterHandle = self.onEvent_battleEnterHandle or handler(self, self.onEvent_battleEnter)
  C_ExternalManager.AddEvent(C_EExternalEventType.BattleEnter, self.onEvent_battleEnterHandle)
  self.onEvent_loadPageControlHandle = self.onEvent_loadPageControlHandle or handler(self, self.onEvent_loadPageControl)
  C_ExternalManager.AddEvent(C_EExternalEventType.LoadPageControl, self.onEvent_loadPageControlHandle)
  self.onEvent_transPosHandle = self.onEvent_transPosHandle or handler(self, self.onEvent_transPos)
  C_ExternalManager.AddEvent(C_EExternalEventType.TransPosToBattlePos, self.onEvent_transPosHandle)
  self.onEvent_battleStartHandle = self.onEvent_battleStartHandle or handler(self, self.onEvent_batterStart)
  C_ExternalManager.AddEvent(C_EExternalEventType.BattleStart, self.onEvent_battleStartHandle)
  self.onEvent_revertPosHandle = self.onEvent_revertPosHandle or handler(self, self.onEvent_revertPos)
  C_ExternalManager.AddEvent(C_EExternalEventType.RevertPos, self.onEvent_revertPosHandle)
  self.onEvent_recreateWorldTeamHandle = self.onEvent_recreateWorldTeamHandle or handler(self, self.onEvent_recreateWorldTeam)
  C_ExternalManager.AddEvent(C_EExternalEventType.RecreateWorldTeam, self.onEvent_recreateWorldTeamHandle)
  self.onEvent_serverFinishBattleHandle = self.onEvent_serverFinishBattleHandle or handler(self, self.onEvent_serverFinishBattle)
  C_ExternalManager.AddEvent(C_EExternalEventType.ServerFinishBattle, self.onEvent_serverFinishBattleHandle)
  self.onEvent_revertPlayerHandle = self.onEvent_revertPlayerHandle or handler(self, self.onEvent_revertPlayer)
  C_ExternalManager.AddEvent(C_EExternalEventType.RevertPlayer, self.onEvent_revertPlayerHandle)
  self.onEvent_battleExitHandle = self.onEvent_battleExitHandle or handler(self, self.onEvent_battleExit)
  C_ExternalManager.AddEvent(C_EExternalEventType.BattleExit, self.onEvent_battleExitHandle)
  self.onEvent_pageControlBattleHandle = self.onEvent_pageControlBattleHandle or handler(self, self.onEvent_pageControlBattle)
  C_ExternalManager.AddEvent(C_EExternalEventType.PageControlBattle, self.onEvent_pageControlBattleHandle)
  self.onEvent_createMonsterFinishHandle = self.onEvent_createMonsterFinishHandle or handler(self, self.onEvent_createMonsterFinish)
  C_ExternalManager.AddEvent(C_EExternalEventType.CreateMonsterFinish, self.onEvent_createMonsterFinishHandle)
  self.onEvent_battleFieldCreateMonsterHandle = self.onEvent_battleFieldCreateMonsterHandle or handler(self, self.onEvent_battleFieldCreateMonster)
  C_ExternalManager.AddEvent(C_EExternalEventType.BattleFieldCreateMonster, self.onEvent_battleFieldCreateMonsterHandle)
  self.onEvent_operatePlayerMountStatusHandle = self.onEvent_operatePlayerMountStatusHandle or handler(self, self.onEvent_operatePlayerMountStatus)
  C_ExternalManager.AddEvent(C_EExternalEventType.OperatePlayerMountStatus, self.onEvent_operatePlayerMountStatusHandle)
  self.onEvent_hideBossHPBarHandle = self.onEvent_hideBossHPBarHandle or handler(self, self.onEvent_hideBossHPBar)
  C_ExternalManager.AddEvent(C_EExternalEventType.HideBossHPBar, self.onEvent_hideBossHPBarHandle)
  self.entityRemoveHandle = self.entityRemoveHandle or handler(self, self.onEvent_EntityRemove)
  C_EntityManager.AddEntityOnRemoveHandler(self.entityRemoveHandle)
  self.onEvent_tmpPauseInputHandle = self.onEvent_tmpPauseInputHandle or handler(self, self.onEvent_tmpPauseInput)
  C_ExternalManager.AddEvent(C_EExternalEventType.TmpPauseInput, self.onEvent_tmpPauseInputHandle)
  self.onEvent_MonsterInBattleStatusChangeHandle = self.onEvent_MonsterInBattleStatusChangeHandle or handler(self, self.onEvent_MonsterInBattleStatusChange)
  C_ExternalManager.AddEvent(C_EExternalEventType.MonsterInBattleStatusChange, self.onEvent_MonsterInBattleStatusChangeHandle)
end

function this:dispose()
  C_ExternalManager.RemoveEvent(C_EExternalEventType.BattleEnter, self.onEvent_battleEnterHandle)
  C_ExternalManager.RemoveEvent(C_EExternalEventType.LoadPageControl, self.onEvent_loadPageControlHandle)
  C_ExternalManager.RemoveEvent(C_EExternalEventType.TransPosToBattlePos, self.onEvent_transPosHandle)
  C_ExternalManager.RemoveEvent(C_EExternalEventType.BattleStart, self.onEvent_battleStartHandle)
  C_ExternalManager.RemoveEvent(C_EExternalEventType.RevertPos, self.onEvent_revertPosHandle)
  C_ExternalManager.RemoveEvent(C_EExternalEventType.RecreateWorldTeam, self.onEvent_recreateWorldTeamHandle)
  C_ExternalManager.RemoveEvent(C_EExternalEventType.ServerFinishBattle, self.onEvent_serverFinishBattleHandle)
  C_ExternalManager.RemoveEvent(C_EExternalEventType.RevertPlayer, self.onEvent_revertPlayerHandle)
  C_ExternalManager.RemoveEvent(C_EExternalEventType.BattleExit, self.onEvent_battleExitHandle)
  C_ExternalManager.RemoveEvent(C_EExternalEventType.PageControlBattle, self.onEvent_pageControlBattleHandle)
  C_ExternalManager.RemoveEvent(C_EExternalEventType.CreateMonsterFinish, self.onEvent_createMonsterFinishHandle)
  C_ExternalManager.RemoveEvent(C_EExternalEventType.BattleFieldCreateMonster, self.onEvent_battleFieldCreateMonsterHandle)
  C_ExternalManager.RemoveEvent(C_EExternalEventType.OperatePlayerMountStatus, self.onEvent_operatePlayerMountStatusHandle)
  C_ExternalManager.RemoveEvent(C_EExternalEventType.HideBossHPBar, self.onEvent_hideBossHPBarHandle)
  C_EntityManager.RemoveEntityOnRemoveHandler(self.entityRemoveHandle)
  C_ExternalManager.RemoveEvent(C_EExternalEventType.TmpPauseInput, self.onEvent_tmpPauseInputHandle)
  C_ExternalManager.RemoveEvent(C_EExternalEventType.MonsterInBattleStatusChange, self.onEvent_MonsterInBattleStatusChangeHandle)
end

function this:onEvent_MonsterInBattleStatusChange(args)
  local monsterCsEntity = C_EntityManager.GetEntity(args.intArg)
  if not monsterCsEntity then
    return
  end
  local entityScene = monsterCsEntity:GetMonsterScene()
  if entityScene == CS.Lens.Gameplay.Modules.BigWorld.EEntityScene.Battle then
    L_EntityManager:registerCSEntityTriggerStack(monsterCsEntity)
    return
  end
  L_EntityManager:unRegisterCSEntityTriggerStack(monsterCsEntity)
end

function this:onEvent_operatePlayerMountStatus(args)
  L_BattleDataManager:setIsCanMount(args.intArg == 0)
  if args.intArg == 0 then
    return
  end
  if args.intArg == 1 then
    if L_PlayerStore:playerInMountStatus() then
      AzurWorld.mountMgr:ReqLeaveMount(true)
      if L_PlayerManager:isInMounting() then
      else
        AzurWorld.playerMgr.myPlayerUnitAdapter:SwitchOutMountMainControl(nil, true)
      end
    end
  elseif args.intArg == 2 and L_PlayerStore:playerInMountStatus() then
    AzurWorld.mountMgr:ReqLeaveMount(true)
    AzurWorld.playerMgr.myPlayerUnitAdapter:SwitchOutMountMainControl(nil, true)
  end
end

function this:onEvent_hideBossHPBar(args)
  L_BattleDataManager:setShowBossHpNode(args.boolArg)
end

function this:onEvent_pageControlBattle(args)
end

function this:onEvent_battleEnter()
  L_BattleManager:sendEvent(L_BattleManager.event.battleEnter)
end

function this:onEvent_loadPageControl(args)
  if args.isOpen then
    L_LoadingManager:openLoading()
  elseif args.pageType == 2 then
    L_LoadingManager:shotDownLoading()
  else
    L_LoadingManager:shotDownLoading()
  end
end

function this:onEvent_transPos(args)
  L_PlayerManager:transfer(args.pos, args.rot, function()
    C_DriveManager.DispatchDrive(C_EExternalDrive.TransPosEnd)
  end, false, true)
end

function this:onEvent_batterStart()
  L_BattleManager:sendBattleEvent(L_BattleManager.event.battleStart)
end

function this:onEvent_recreateWorldTeam()
  if not L_PlayerManager:getMainControlEntity() then
    C_DriveManager.DispatchDrive(C_EExternalDrive.TransPosEnd)
    return
  end
  AzurWorld.playerMgr.myPlayerUnitAdapter:ResumePlayerUnit()
end

function this:onEvent_revertPos(args)
  L_PlayerManager:transfer(args.pos, nil, function()
    C_DriveManager.DispatchDrive(C_EExternalDrive.TransPosEnd)
  end)
end

function this:onEvent_serverFinishBattle(args)
  L_BattleManager:sendEvent(L_BattleManager.event.sendBattleFinish)
  if args.finishType == C_EBattlerServerFinishType.TimeOut then
  elseif args.finishType == C_EBattlerServerFinishType.ForceOut then
  elseif args.finishType == C_EBattlerServerFinishType.Win then
  elseif args.finishType == C_EBattlerServerFinishType.Defeat then
  end
end

function this:onEvent_revertPlayer()
  C_DriveManager.DispatchDrive(C_EExternalDrive.RevertPlayerEnd)
end

function this:onEvent_battleExit(args)
  L_BattleStore:exitBattle()
  L_BattleManager:sendBattleEvent(L_BattleManager.event.exitBattle, args)
  L_BattleDataManager:onExitBattle(args.finishType)
end

function this:onEvent_createMonsterFinish(args)
  L_BattleManager:sendBattleEvent(L_BattleManager.event.createMonsterFinish, args)
end

function this:onEvent_battleFieldCreateMonster(args)
  L_EntityManager:registerCSEntityTriggerStack(args.summon)
end

function this:onEvent_EntityRemove(entity)
  L_EntityManager:unRegisterCSEntityTriggerStack(entity)
end

function this:onEvent_tmpPauseInput(args)
  L_UIManager:tmpPauseInput(args.pause)
end

return this
