local this = class("kiBoDuelSceneMgr", G_EventManagerBase)

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

function this:OpenKiboDuelUI()
  L_UI:openRoot("pagePetDuel", {directlyOpen = true})
end

function this:StartKiboDuel()
  L_KiBoDuelManager:addListener(L_KiBoDuelManager.event.restartKiBoDuel, self.restartKiBoDuel, self)
  local duelId = L_PetDuelStore:getCurChallengeLevelId() or 1001
  self:dealEnterKiBoDuel(duelId, function()
  end)
end

function this:ExitKiboDuel()
  L_KiBoDuelManager:onUnloadKiBoDuel()
  L_KiBoDuelManager:removeListener(L_KiBoDuelManager.event.restartKiBoDuel, self.restartKiBoDuel, self)
  L_UI:openRoot("pageMainCity")
  C_KiBoDuelSceneManager.DisposePlayerUnitAdapter()
  AzurWorld.playerMgr.myPlayerUnitAdapter.pause = false
  AzurWorld.playerMgr.myPlayerUnitAdapter:ResumePlayerUnit(nil, true)
end

function this:dealEnterKiBoDuel(duelId, callback)
  C_KiBoDuelSceneManager.CreatePlayerUnitAdapter()
  AzurWorld.playerMgr.myPlayerUnitAdapter.pause = true
  C_KiBoDuelSceneManager.GetUnitAdapter():SwitchToFormationMainControl()
end

function this:restartKiBoDuel()
  L_KiBoDuelManager:onUnloadKiBoDuel()
  C_KiBoDuelLuaWrapper.RestartDuel()
  local duelId = L_PetDuelStore:getCurChallengeLevelId() or 1001
  self:dealEnterKiBoDuel(duelId)
  L_PetDuelStore:req_CSProtoKiboDuelStart(duelId)
end

return this
