local this = {}
local _battleTpl = L_GameTpl:getBattleTpl()
local _battleFieldTpl = L_GameTpl:getBattleFieldTpl()

function this:initWorldServerData()
end

function this:doFsmBattleField(callback)
  local createBattleFieldData = C_EntityManager.CreateBattleFieldData()
  createBattleFieldData.createMonsterDataList = self.battleFieldDataEntity.monsterCreateDataList
  if self.battleFieldDataEntity.battleType ~= nil then
    createBattleFieldData.battleType = self.battleFieldDataEntity.battleType
  else
    createBattleFieldData.battleType = C_EBattlerType.None
  end
  C_BattleManager.CreateBattleFieldEntity(createBattleFieldData, function(entity)
    entity:SetAliveFsmInitActive()
    if callback then
      callback(entity)
    end
  end)
end

function this:get_battleWallCenterAndPos(configId)
  local battleOneTpl = _battleTpl:getTplById(configId)
  local _worldBattleZoneTpl = L_GameTpl:getWorldBattleZoneTpl()
  local zoneId = _battleTpl:getBattleZoneId(battleOneTpl)
  local zoneTpl = _worldBattleZoneTpl:getTplById(zoneId)
  if zoneTpl == nil then
    errorf("\231\173\150\229\136\146\230\178\161\230\156\137\233\133\141\231\189\174 battle_id:" .. configId .. "\229\175\185\229\186\148\231\154\132\230\136\152\229\156\186 BattleZone")
    return
  end
  local centerPos = _worldBattleZoneTpl:getCenterPos(zoneTpl)
  local boxRadius = _worldBattleZoneTpl:getBoxRadius(zoneTpl)
  return centerPos, boxRadius
end

function this:initServerMonsterData(config_battleId, battle_fomations, entry)
  local _battleFieldTpl = L_GameTpl:getBattleFieldTpl()
  local enemyList = {}
  local configEnemyList = _battleFieldTpl:getEnemyInfos(_battleFieldTpl:getTplById(config_battleId))
  for i, v in ipairs(configEnemyList) do
    local createEnemyData = C_EntityManager.CreateMonsterData()
    createEnemyData.guid = v.guid
    local enemyId = L_GameTpl:getEnemyPackTpl():getEnemyIdByPackId(v.id)
    local enemyLv = L_GameUtil.getLvByLvPackId(v.id)
    createEnemyData.configID = enemyId
    createEnemyData.lv = enemyLv
    createEnemyData.position = v.position
    createEnemyData.rotation = Unity.Quaternion.Euler(v.angle)
    createEnemyData.campType = CS.Lens.Gameplay.Modules.BigWorld.ECampType.Monster
    table.insert(enemyList, createEnemyData)
  end
  local battleFiledPosList = L_GameTpl:getBattleFieldTpl():getSlotInfoList(config_battleId)
  for i, v in ipairs(battle_fomations or {}) do
    if not math.isEmpty(v.id) and battleFiledPosList[i] ~= nil then
      local createEnemyData = C_EntityManager.CreateMonsterData()
      createEnemyData.guid = i
      createEnemyData.configID = v.id
      createEnemyData.lv = v.level
      local pos = battleFiledPosList[i].position
      local rot = battleFiledPosList[i].angle
      createEnemyData.position = pos
      createEnemyData.rotation = Unity.Quaternion.Euler(rot)
      createEnemyData.campType = CS.Lens.Gameplay.Modules.BigWorld.ECampType.Monster
      table.insert(enemyList, createEnemyData)
    end
    if not math.isEmpty(v.id) and battleFiledPosList[i] == nil then
      error("[Battle Error] Battle Filed Data Not Enough Check FiledId : [" .. config_battleId .. "] \232\129\148\231\179\187\231\173\150\229\136\146")
    end
  end
  return enemyList
end

function this:getBattleFieldDataEntity()
  return self.battleFieldDataEntity
end

function this:clearBattleFiledDataEntity()
  self.battleFieldDataEntity = nil
end

function this:getAllEnemy()
  local enemys = {}
  local battleFieldEntity = C_BattleManager.battleFieldEntity
  if battleFieldEntity then
    local allEnemy = battleFieldEntity.data.monsterEntityList
    for i = 0, allEnemy.Count - 1 do
      enemys[allEnemy[i].data.entityId] = allEnemy[i]
    end
  end
  return enemys
end

return this
