-- salvo i settaggi
function saveSettings()
  file = io.open("settings.ini", "w")
  -- controllo che il file sia aperto
  if file then
    file:write("options.difficulty = " .. options["difficulty"] .. "\n")
    file:write("options.background = " .. options["background"] .. "\n")
    file:write("options.music = " .. options["music"] .. "\n")
    file:close()
		return true
  else
		return false
	end
end

-- gestione "caricamento"
function loading(perc)
	x = 360
	y = 260
	screen:clear()
	screen:print(x, y, "loading: "..perc.."%", whiteColor)
	screen.flip()
	screen.waitVblankStart(10)
end

function curveAnim()
	-- linea animata - TEST -
	l1X = (math.sin(time/100) * 12) - 12
	--l1Y = math.abs((math.sin(time/512) * 6) + 180)
	time = time + 1
	if time >= 10000 then
		time = 0
	end
	screen:blit(l1X, l1Y, linea01, true)
end

-- funzione per il reset delle variabili
function resetVar()
	-- variabili del gioco
	score = 0
	timer = Timer.new()
	speed = 1 * options["difficulty"]
	currentBT = randCalc()
	nextBT = randCalc()
	lives = 3
	quadTimerCount = 0
end

-- funzione per il calcolo del random in base alla difficoltà scelta
function randCalc()
	if options["difficulty"] == 1 then
		rndVal = math.floor(math.random(1, 4))
	elseif options["difficulty"] == 2 then
		rndVal = math.floor(math.random(1, 8))
	elseif options["difficulty"] == 3 then
		rndVal = math.floor(math.random(1, 10))
	end
	return(rndVal)
end

-- funzione per la visualizzazione dei tasti
function showBT(current)
	-- seleziono l'ombra adatta
	if current <= 4 then
		screen:blit(73, 83, tastoOmbraTonda, true)
	end
	screen:blit(70, 80, tasti[current], true)
end

-- funzione per selezionare/visualizzare il background+animazioni varie
function displayBkg()
	screen:blit(0, 0, background3[options["background"]], false)
	curveAnim()
end

-- funzione per visualizzare i dati sullo schermo
function displayDataFunc()
		-- elementi vari ed info
		screen:print(10, 6, appName.." v"..appVer, whiteColor)
		screen:print(270, 100, "Score: "..score, whiteColor)
		screen:print(270, 140, "Lives: "..lives, whiteColor)
		screen:print(270, 160, "Timer: "..timer:time(), whiteColor)
		screen:print(270, 180, "Random: "..currentBT, whiteColor)
		screen:print(270, 200, "Press Select to exit", whiteColor)
end

-- funzione per la gestione del tasto indovinato
function correctFunc()
	soundCorrect:play()
	if options["difficulty"] == 1 then
		scoreAdd = math.ceil((maxTime - timer:time()) / 4)
	elseif options["difficulty"] == 2 then
		scoreAdd = math.ceil(((maxTime * 1.5) - timer:time()) / 4)
	elseif options["difficulty"] == 3 then
		scoreAdd = math.ceil(((maxTime * 2) - timer:time()) / 4)
	end

	score = score + scoreAdd
	timer:reset(0)

	-- visualizzo i dati sullo schermo
	screen:clear()
	displayBkg()
	displayDataFunc()
	screen:print(100, 140, "CORRECT!!!", whiteColor)
	screen:print(80, 160, "score + "..scoreAdd, whiteColor)
	screen.flip()
	screen.waitVblankStart(60/options["difficulty"])

	currentBT = nextBT
	nextBT = randCalc()
end	

-- funzione error
function errorFunc()
	soundError:play()
	currentBT = nextBT
	nextBT = randCalc()

	-- visualizzo i dati sullo schermo
	screen:clear()
	displayBkg()
	displayDataFunc()
	screen:print(100, 140, "WRONG!!!", redColor)

	screen.flip()
	screen.waitVblankStart(60/options["difficulty"])
	if lives > 1 then
		lives = lives - 1
		timer:reset(0)
		return(true)
	else
		if score > topScore then topScore = score end

		-- visualizzo i dati sullo schermo
		screen:clear()
		screen:blit(0, 0, background3[options["background"]], false)
		curveAnim()
		screen:blit(75, 90, quadEndGame, true)
		screen:print(10, 6, appName.." v"..appVer, whiteColor)
		screen:print(180, 130, "TOP Score: "..topScore, whiteColor)
		screen:print(180, 150, "Your Score: "..score, whiteColor)
		screen:print(146, 240, "Press START to play again", whiteColor)
		screen:print(136, 260, "Press X to quit to main menu", whiteColor)

	  -- se il punteggio è superiore al topScore salvo nel file
	  if score > topScore then
	  	file = io.open("scores.ini", "w")
 			-- controllo che il file sia aperto
 			if file then
	   		file:write("topScore = " .. topScore .. "\n")
   			file:write("name = \"" .. userName .. "\"\n")
   			file:close()
				screen:print(300, 6, ":: Score saved...", whiteColor)
 			else
				screen:print(300, 6, ":: Erro while saving score...", whiteColor)
			end
		end
		
		screen.flip()
		screen.waitVblankStart()
		oldErrorPad = Controls.read()
		while true  do
			errorPad = Controls.read()
			if errorPad ~= oldErrorPad then
				if errorPad:start() then
					resetVar()
					return(true)
				elseif errorPad:cross() then
					return(false)
				end
			end
			oldErrorPad = errorPad
		end
	end
end