BattleUtils = {}

local datasutils = require "game.utils.datas"

local DIR = "battles"

function BattleUtils.getBaseDirectory(lua)
  return datasutils.concatDataFolder(DIR, lua)
end

function BattleUtils.listCategories()
  return love.filesystem.getDirectoryItems(BattleUtils.getBaseDirectory(false))
end

function BattleUtils.getCategoryDirectory(directory, lua)
  local baseDir = BattleUtils.getBaseDirectory(lua)
  return datasutils.concatFolder(baseDir, directory, lua)
end

function BattleUtils.getDirectoryLuaStyle(directory)
  return DIR .. "." .. directory
end

function BattleUtils.getBattleFromCategory(directory)
  local folder = BattleUtils.getCategoryDirectory(directory, false)
  return datasutils.luaFileListToModuleList(love.filesystem.getDirectoryItems(folder))
end

function BattleUtils.getBattleData(directory, battlename)
  local categoryDir = BattleUtils.getCategoryDirectory(directory, true)
  return datasutils.require(categoryDir, battlename)
end

-- ENNEMY FUNCTIONS

function BattleUtils.getEnnemyTypeStructure(type)
  local typeList = require "game.utils.battle.arguments"
  return typeList[type]
end

function BattleUtils.ennemyTypeExists(type)
  return (BattleUtils.getEnnemyTypeStructure(type) ~= nil)
end

function BattleUtils.validateEnnemyType(ennemyBaseData)
  local structure = BattleUtils.getEnnemyTypeStructure(ennemyBaseData[1])
  if (structure == nil) then
    return false
  else
    return ((#structure + 1) == #ennemyBaseData)
  end
end

function BattleUtils.getEnnemyData(ennemyBaseData)
  local ennemyData = {}
  ennemyData.type = ennemyBaseData[1]

  if (BattleUtils.validateEnnemyType(ennemyBaseData)) then
    local structure = BattleUtils.getEnnemyTypeStructure(ennemyData.type)

    for i, argumentName in ipairs(structure) do
      local argumentContent = ennemyBaseData[i + 1]
      ennemyData[argumentName] = argumentContent
    end

    return ennemyData
  else
    error("Le type d'ennemy " .. ennemyData.type .. " à un nbr d'argument incorrect")
  end


end

return BattleUtils
