-- need to copy and paste this because the #include keyword stopped working
function KeyInit()
	setKey("savegame.mod.hardcore", false, true)
	setKey("savegame.mod.survival", false, true)
	setKey("savegame.mod.weapon", 1, false)
	setKey("savegame.mod.shield", false, true)
end

function setKey(key, default, boolean, infinite)

	boolean = boolean or false
	infinite = infinite or false
	
	if not HasKey(key) then
		if boolean then
			SetBool(key, default)
		else
			SetInt(key, default)
		end
	end
	if infinite then
		if not HasKey(key .. "_infinite") then
			SetBool(key .. "_infinite", false)
		end
	end
end

function init()
	KeyInit()
end


function draw()
	UiTranslate(UiCenter(), 40)
	UiAlign("center middle")

	--Draw buttons
	UiTranslate(0, 30)
	UiFont("regular.ttf", 26)
	
	local border = 90
	
	UiTranslate(0, border)
	DrawBoolButton("savegame.mod.hardcore", "Toggle the hardcore mod. If toggled, you one-hit enemies but they also one-hit you.")
	
	UiTranslate(0, border)
	DrawBoolButton("savegame.mod.survival", "Toggle the spawning of enemies around you. Toggled by default on the forest map.")

	UiTranslate(0, border)
	DrawIntButton("savegame.mod.weapon", "Select your weapon.")

	UiTranslate(0, border)
	DrawBoolButton("savegame.mod.shield", "Toggle the shield. With the shield you cannot do thrust anymore, but you can parry a succession of attacks instead.")
	
	UiTranslate(0, border)
	DrawBoolButton("START", "Cursed Forest")
	
	UiTranslate(0, 100)
	if UiTextButton("Close", 200, 40) then
		Menu()
	end
end

local function bool_to_int(value)
	return value == true and 1 or value == false and 0
end

local function int_to_bool(value)
	return value > 0
end

function DrawIntButton(key, text)

	local intToName = {}
	intToName[#intToName + 1] = "Sword"
	intToName[#intToName + 1] = "Axe"
	intToName[#intToName + 1] = "Spear"
	intToName[#intToName + 1] = "Hammer"

	UiPush()
		UiAlign("center middle")
		UiFont("regular.ttf", 22)
		UiColor(0.9,0.9,0.025,1)
		UiText(text, true)
		UiTranslate(0, 15)
		local textButton = intToName[GetInt(key)]
		UiColor(1,1,1,1)
		UiFont("regular.ttf", 26)
		if UiTextButton(textButton) then
			SetInt(key, (GetInt(key) % #intToName) + 1)
		end
	UiPop()
end

function DrawBoolButton(key, text)
	UiPush()
		UiAlign("center middle")
		UiFont("regular.ttf", 22)
		UiColor(0.9,0.9,0.025,1)
		UiText(text, true)
		UiTranslate(0, 15)
		local textButton = "Toggle"
		if GetBool(key) then
			UiColor(0,1,0,1)
		else
			UiColor(1,0,0,1)
		end
		if key == "START" then
			UiColor(1, 1, 1, 1)
			textButton = "Play"
		end
		UiFont("regular.ttf", 26)
		if UiTextButton(textButton) then
			if key == "START" then
				StartLevel("Cursed Forest", "MOD/mainmap.xml")
			else
				SetBool(key, not GetBool(key))
			end
		end
	UiPop()
end