--TODO: make menus skinnable
function mainMenu(originalBGImage, modal)
	local ovImg=Image.load("images/menus/main_menu.png")
	
	local mainMenuItems={}
	mainMenuItems[1]="New game"
	mainMenuItems[2]="New neutral game"
	mainMenuItems[3]="Game preferences"
	mainMenuItems[4]="Skin preferences"
	mainMenuItems[5]="Exit"
	mainMenuItems[6]="Help" --controls & rules
	mainMenuItems[7]="Credits"
	mainMenuItems[8]="Scores"
	mainMenuItems["unavailable"]={}
	mainMenuItems["unavailable"][3]=true
	mainMenuItems["unavailable"][6]=true
	mainMenuItems["unavailable"][7]=true
	
	--if not modal then
	--	mainMenuItems[7]="Back"
	--end
	
	local currentSelection=1
	local nbOptions=tableLength(mainMenuItems)
	

	--saves the background we receive, to propagate it properly to sub menus
	--so instead of working with the image we receive, we'll work on a local copy
	--this only saves a blit for the moment
	--TODO is it needed?
	--local bgImage=Image.createEmpty(480, 272)
	--bgImage:blit(0, 0, originalBGImage, true)
	--bgImage:blit(0, 0, menuImages["background_overlay"], true)
	
	
	
	while true do
		updateInputs(Controls.read())
		
		if inputFirstPress("down") or inputIsRepeating("down") then
			currentSelection=math.mod(currentSelection, nbOptions)+1
		elseif inputFirstPress("up") or inputIsRepeating("up") then
			currentSelection=currentSelection-1
			if currentSelection==0 then
				currentSelection=nbOptions
			end
		end
		
		if inputFirstPress("r") then
			currentSelection=nbOptions
		end
		if inputFirstPress("l") then
			currentSelection=1
		end
		
		--selection of an item
		if inputFirstPress("cross") then
			if currentSelection==5 then --exit
				drawMainMenuScene(screen, originalBGImage, mainMenuItems, nbOptions, currentSelection, ovImg)
				if yesNoMenu("Are you sure\nyou want to exit?")=="yes" then
					return "exit"
				end
			elseif currentSelection==4 then --skin preferences
				local skinchoice=skinMenu(originalBGImage)
				if skinchoice=="quitMenu" then
					return "quitMenu"
				--elseif == cancel : nothing
				
				end
			--elseif currentSelection==7 then --back
			--	return "cancel"
			elseif currentSelection==1 then --new game
				return "newGame"
			elseif currentSelection==2 then --new neutral game
				return "newNeutralGame"
			elseif currentSelection==8 then --scores
				if showHighScores(originalBGImage)=="quitMenu" then
					return "quitMenu"
				end
			end
		end
		
		if inputLongPress("l") and inputLongPress("r") then
			if testMenu(originalBGImage)=="exit" then
				return "exit"
			end
		end
				
		if inputFirstPress("start") then
			if not modal then
				return "quitMenu"
			end
		end
		
		if inputFirstPress("square") then
			if not modal then
				return "cancel"
			end
		end
		
		drawMainMenuScene(screen, originalBGImage, mainMenuItems, nbOptions, currentSelection, ovImg)
	end
end

function drawMainMenuScene(target, originalBGImage, mainMenuItems, nbOptions, currentSelection, ovImg)
	local x=138+10
	local y=74+10
	
	target:blit(0, 0, originalBGImage, true)
	target:blit(0, 0, ovImg, true)
	
	local i=0
	for i=1,nbOptions do
		if i==currentSelection then
			target:blit(x-10, y-1+(i-1)*15, menuImages["main_selector"], true)
		end
		if mainMenuItems["unavailable"][i] then
			target:print(x, y+(i-1)*15, mainMenuItems[i], grey)
		else
			target:print(x, y+(i-1)*15, mainMenuItems[i], white)
		end
	end
	
	screen.flip()
	screen.waitVblankStart()
	
end














