------------------------------------------------------------------------------
-- Imports
------------------------------------------------------------------------------

local love = require 'love'
local magiclines = require 'src.utils.mgclines'


------------------------------------------------------------------------------
-- Constants
------------------------------------------------------------------------------

local SETTINGS_PATH = 'settings.ini'


------------------------------------------------------------------------------
-- Module definitions
------------------------------------------------------------------------------

local settings = {
    permadeath=true,
    forward='w',
    backward='s',
    stepleft='a',
    stepright='d',
    turnleft='q',
    turnright='e',
    musicvol=75,
    soundvol=100,
}


------------------------------------------------------------------------------
-- Private module methods
------------------------------------------------------------------------------

function settings:_parse_settings_str(s)
    for line in magiclines(s) do
        local k, v = string.match(line, '(%w+)=(.+)')

        if k and v then
            if self[k] ~= nil then
                if v == 'true' then
                    if type(self[k]) ~= 'boolean' then error(string.format('Invalid value for %s in settings', k)) end
                    self[k] = true
                elseif v == 'false' then
                    if type(self[k]) ~= 'boolean' then error(string.format('Invalid value for %s in settings', k)) end
                    self[k] = false
                elseif tonumber(v) then
                    if type(self[k]) ~= 'number' then error(string.format('Invalid value for %s in settings', k)) end
                    self[k] = tonumber(v)
                else
                    self[k] = v
                end
            else
                error(string.format('Unknown settings key "%s"', k))
            end
        end
    end
end


function settings:_get_settings_str()
    local s = '[settings]\n'

    for k, v in pairs(self) do
        if type(v) ~= 'function' then
            s = s .. string.format('%s=%s\n', k, v)
        end
    end

    return s
end


function settings:_open_or_create()
    if not love.filesystem.exists(SETTINGS_PATH) then
        local setts = self:_get_settings_str()
        love.filesystem.write(SETTINGS_PATH, setts)
        return setts
    else
        if not love.filesystem.isFile(SETTINGS_PATH) then
            error ("Settings path exists and isn't a file")
        else
            return love.filesystem.read(SETTINGS_PATH)
        end
    end
end


------------------------------------------------------------------------------
-- Public module methods
------------------------------------------------------------------------------

function settings:load_settings()
    local s = self:_open_or_create()
    self:_parse_settings_str(s)
end


function settings:save_settings()
    if not love.filesystem.exists(SETTINGS_PATH) or love.filesystem.isFile(SETTINGS_PATH) then
        love.filesystem.write(SETTINGS_PATH, self:_get_settings_str())
    else
        error("Settings path exists and isn't a file")
    end
end


------------------------------------------------------------------------------
-- Module return
------------------------------------------------------------------------------

return settings
