Savegame = Object:extend()

local binser = require "libs.binser"

require "modules.savegame.pigs"

function Savegame:new(filename)
  self:reset()
end

function Savegame:init(filename)
  self:reset()
  self.name = filename
  self.slot = 1 -- le slot 0 signifie qu'on ne peut pas sauvegarder.
end

function Savegame:reload()
  self:read(self.name)
end

function Savegame:getSaveFile(saveslot, absolute)
  local dir = ""
  if absolute then
    dir = love.filesystem.getSaveDirectory() .. "/"
    if not love.filesystem.exists(dir) then
      love.filesystem.createDirectory( "" )
    end
  end

  local filepath = dir .. "save" .. saveslot .. ".save"

  return filepath
end

function Savegame:read()
  if self.slot > 0 then
    filepath = self:getSaveFile(self.slot, true)
    if love.filesystem.exists("save" .. self.slot .. ".save") then
      local loadedDatas = binser.readFile(filepath)
      self.data = loadedDatas[1]
    else
    end
  end
end

function Savegame:write()
  if self.slot > 0 then
    filepath = self:getSaveFile(self.slot, true)
    binser.writeFile(filepath, self.data)
  end
end

function Savegame:reset()
  self.name = ""
  self.data = {}
  self.slot = 1
  self:resetPigs()
end

return Savegame
