local this = {}

function this:sendBattleEvent(eventName, ...)
  self:sendEvent(eventName, ...)
end

function this:entityDyingFunction(CsEntity)
  local entityType = CsEntity.data.entityType
  if entityType == CS.Lens.Gameplay.Modules.BigWorld.EEntityType.Hero then
    local entity = L_PlayerManager:getPlayerUnit():getEntityByCEntityId(CsEntity.data.entityId)
    if entity ~= nil and self:isAllHeroDead(entity:getId()) then
      self:afterHeroAllDying()
    end
  end
end

function this:entityDeadFunction(CsEntity)
  local entityType = CsEntity.data.entityType
  if entityType == CS.Lens.Gameplay.Modules.BigWorld.EEntityType.Hero then
    local entity = L_PlayerManager:getPlayerUnit():getEntityByCEntityId(CsEntity.data.entityId)
    if entity == nil then
      return
    end
    local id = entity:getId()
    self:onPlayerBattleDead(id)
  end
end

function this:onPlayerBattleDead(id)
  if L_PlayerManager:getMainHeroEntity():getId() ~= id then
    return
  end
  local heroIndex = L_FormationManager:getNextAliveHeroIndex(id)
  if heroIndex == nil then
    C_DriveManager.DispatchDrive(C_EExternalDrive.HeroAllDead)
    self:afterHeroAllDead()
    L_GuideManager:stopAllGuide(true)
    return
  end
  L_FormationManager:switchMyPlayerMainControl(heroIndex - 1, true)
end

function this:isAllHeroDead(lastDeadId)
  local heroDic = L_PlayerManager:getPlayerUnit():getAllHero()
  printf("guide isAllHeroDead", table.count(heroDic))
  for i, v in pairs(heroDic) do
    printf("guide isAllHeroDead", i .. "   " .. lastDeadId)
    if i ~= lastDeadId and not C_EntityManager.IsEntityDead(v:getCsEntityId()) and C_MyFloatUtility.GetFloat(v:getCsEntity().data.aliveProperty.hp) > 0 then
      return false
    end
  end
  return true
end

function this:afterHeroAllDying()
  L_UI:backToRoot()
  print("afterHeroAllDying backToRoot")
  L_FormationManager:setReviveHeroState("dying")
end

function this:afterHeroAllDead()
  L_FormationManager:setReviveHeroState("dead")
  local playModule = L_WorldManager:getCurModule()
  if playModule == L_Const.worldModule.dungeon or playModule == L_Const.worldModule.multiDungeon or playModule == L_Const.worldModule.interim then
    if not L_FormationManager:checkCanReviveHeros() then
      return
    end
    local isInBattle = C_BattleManager:IsInBattle()
    if not isInBattle then
      L_FormationManager:reviveHeros()
    else
      L_FormationManager:setReviveHeroState("waitBattleState")
      self._onBattleFieldStateChange = self._onBattleFieldStateChange or handler(self, self.onBattleFieldStateChange)
      C_ExternalManager.RemoveEvent(C_EExternalEventType.BattleFieldStateChange, self._onBattleFieldStateChange)
      C_ExternalManager.AddEvent(C_EExternalEventType.BattleFieldStateChange, self._onBattleFieldStateChange)
    end
  else
    L_FormationManager:setReviveHeroState(nil)
    L_UI:open("pageBattleFailed")
  end
end

function this:onBattleFieldStateChange(args)
  local isInBattle = C_BattleManager:IsInBattle()
  if not isInBattle then
    C_ExternalManager.RemoveEvent(C_EExternalEventType.BattleFieldStateChange, self._onBattleFieldStateChange)
    L_FormationManager:reviveHeros()
  end
end

return this
