DataUtils = {}

local DATADIR = "datas"
local GAMEDATADIR = "gamedata"

function DataUtils.isLuaFile(name)
  local extension = name:sub(#name - 4, #name)
  return (extension == ".lua")
end

function DataUtils.luaFileToModule(luaFile)
  if (not DataUtils.isLuaFile(luaFile)) then
    return luaFile:sub(1, #luaFile - 4)
  else
    return luaFile
  end
end

function DataUtils.luaFileListToModuleList(luaFileList)
  local moduleList = {}
  for i,luaFile in ipairs(luaFileList) do
    table.insert(moduleList, DataUtils.luaFileToModule(luaFile))
  end
  return moduleList
end

function DataUtils.require(parent, filename)
  return require(DataUtils.concatFolder(parent, filename, true))
end

function DataUtils.copy(filepath)
  local orig = require(filepath)
  local orig_type = type(orig)
  local copy
  if orig_type == 'table' then
      copy = {}
      for orig_key, orig_value in pairs(orig) do
          copy[orig_key] = orig_value
      end
  else -- number, string, boolean, etc
      copy = orig
  end
  return copy
end


function DataUtils.copyDataset(parent, filename)
  local orig = DataUtils.require(parent, filename)
  local orig_type = type(orig)
  local copy
  if orig_type == 'table' then
      copy = {}
      for orig_key, orig_value in pairs(orig) do
          copy[orig_key] = orig_value
      end
  else -- number, string, boolean, etc
      copy = orig
  end
  return copy
end

function DataUtils.concatDataFolder(folder, lua)
  return DataUtils.concatFolder(DataUtils.getGameDataDir(lua), folder, lua)
end

function DataUtils.getGameDataDir(lua)
  return DataUtils.concatFolder(DATADIR, GAMEDATADIR, lua)
end

function DataUtils.concatFolder(parent, filename, lua)
  return parent .. DataUtils.sep(lua) .. filename
end

function DataUtils.sep(lua)
  if (lua) then
    return "."
  else
    return "/"
  end
end

return DataUtils
