inputs = {}

local DEFAULT_SYSTEM_INPUT = {
	device = "keyboard",
	actions = {
		start = {device = "keyboard", code = 32},
		esc = {device = "keyboard", code = 27},
	}
}

local DEFAULT_INPUTS = {
	{
		device = "keyboard",
		actions = {
			left = {device = "keyboard", code = 65}, --WASD
			right = {device = "keyboard", code = 68},
			up = {device = "keyboard", code = 87},
			down = {device = "keyboard", code = 83},
			pickups = {device = "keyboard", code = 17},
			primary = {device = "mouse", code = 0},
			secondary = {device = "mouse", code = 1},
			start = {device = "keyboard", code = 32},
		},
	},
}

function inputs.init()
	local path = daisy.getUserFolderPath("inputs", "*.*")
	for i=1,32 do
		local default = DEFAULT_INPUTS[i]
		local inp = table.simpleLoad(path..i..".sfg") or default
		if inp then
			--printtable(inp.actions)
			local input = Input:new()
			input:load(inp)
			if default then
				for index, var in pairs(default.actions) do
					if not input:has_action(index) then
						input:set_action(var, index)
					end
				end
			end
			inputs[i] = input
		end
	end

	local inp = table.simpleLoad(path.."system.sfg") or DEFAULT_SYSTEM_INPUT
	if inp then
		local input = Input:new()
		input:load(inp)
		for index, var in pairs(DEFAULT_SYSTEM_INPUT.actions) do
			if not input:has_action(index) then
				input:set_action(var, index)
			end
		end
		inputs.system = input
	end
end

function inputs.save()
	local path = daisy.getUserFolderPath("inputs")
	for index, input in ipairs(inputs) do
		input:save(path.."/"..index..".sfg")
	end

	if inputs.system then
		inputs.system:save(path.."/".."system.sfg")
	end
end
