include("data/ingameUI/lua/popupWindow")
include("data/ingameUI/lua/resultList")
--include("dataInclude")
pauseState_curSel = 1

function pauseState_onEnterState()		

	if flow_gameMode == gameTypesIds["MULTIPLAYER"] then
		pauseState_options = {lng_PAUSEMENUBACK, lng_PAUSEMENUSOUND, lng_PAUSEMENUSHIFTS, lng_PAUSEMENUEXIT}
		pauseState_numOptions = 4
	elseif flow_gameMode == gameTypesIds["CHALLENGE"] then
		pauseState_options = {lng_PAUSEMENUBACKCHALLENGE, lng_PAUSEMENURETRY, lng_PAUSEMENUSOUND, lng_PAUSEMENUSHIFTS, lng_PAUSEMENUOBJECTIVES, lng_PAUSEMENUEXIT}
		pauseState_numOptions = 6
	elseif flow_gameMode == gameTypesIds["TOURNAMENT"] then
		pauseState_options = {lng_PAUSEMENUBACK, lng_PAUSEMENURETRYTOUR, lng_PAUSEMENUSOUND, lng_PAUSEMENUSHIFTS, lng_PAUSEMENUEXIT}
		pauseState_numOptions = 5
	else
		pauseState_options = {lng_PAUSEMENUBACK, lng_PAUSEMENURETRY, lng_PAUSEMENUSOUND, lng_PAUSEMENUSHIFTS, lng_PAUSEMENUEXIT}
		pauseState_numOptions = 5
	end

	if pauseState_curSel == 0 then
		pauseState_curSel = 1
	end	
		
	if popup_currentWindow.state == PopupStates["NONE"] then
		createWindowFrame("pauseState_afterWindowOpen", 1.8, 0)
	elseif popup_currentWindow.state == PopupStates["CLOSED"] then
		openPopupWindow("pauseState_afterWindowOpen", 1.8)
	else
		cfunction("debugPrintF", "Wrong popupstate in pauseState_onEnterState", popup_currentWindow.state)
	end
	
	cfunction("enableInput", 0)
	
end

function pauseState_onLeaveState()
end


function pauseState_onDownPressed()	
	pauseState_curSel = pauseState_curSel +1
	if pauseState_curSel ==pauseState_numOptions+1 then 
		pauseState_curSel =1
	end
	pauseState_setSelectionCursor()
	cfunction("startSoundSample", "move_sfx", 1, 100)	
end

function pauseState_onUpPressed()		
	pauseState_curSel = pauseState_curSel -1
	if pauseState_curSel ==0 then 
		pauseState_curSel = pauseState_numOptions
	end
	pauseState_setSelectionCursor()
	cfunction("startSoundSample", "move_sfx", 1, 100)	
end

function pauseState_setSelectionCursor()
	xText = ""
	for i = 1, pauseState_numOptions do
		xText = xText .. '\05'
		if pauseState_curSel == i then
			xText = xText .. "@  "
		end
		xText = xText .. pauseState_options[i]
		if pauseState_curSel == i then
			xText = xText .. "  @"
		end
		xText = xText .. "\n"

	end
	cfunction("setFontOverlayText", "menu_content", xText)
end

function pauseState_onSubmitPressed()	
	cfunction("showFontOverlay", "menu_content", 0, 100, 0, 100)	
	cfunction("startSoundSample", "select_sfx", 1, 100)
	closePopupWindow("pauseState_onAfterClosePopup")	
	cfunction("enableInput", 0)
end

function pauseState_onAfterClosePopup()	
	-- difference because MP has one option less
	if flow_gameMode == gameTypesIds["MULTIPLAYER"] then
		if pauseState_curSel == 1 then
			destroyPopup("pauseState_onAfterDestroyPopup", PopupOptions["FADETONORMAL"])
		elseif pauseState_curSel == 3 then				
			cfunction("transitionToState", "controlScreen")				
		elseif pauseState_curSel == 2 then
			cfunction("transitionToState", "soundAndMusicScreen")
		else
			destroyPopup("pauseState_onAfterDestroyPopup", PopupOptions["FADETOLOADING"])
			cfunction("callCPPFunction", "fadeSound", "0", 0)
		end	
	elseif flow_gameMode == gameTypesIds["CHALLENGE"] then
		if pauseState_curSel == 1 then
			destroyPopup("pauseState_onAfterDestroyPopup", PopupOptions["FADETONORMAL"])
		elseif pauseState_curSel == 4 then				
			cfunction("transitionToState", "controlScreen")				
		elseif pauseState_curSel == 5 then				
			cfunction("transitionToState", "challengeObjectives")				
		elseif pauseState_curSel == 3 then
			cfunction("transitionToState", "soundAndMusicScreen")
		else
			destroyPopup("pauseState_onAfterDestroyPopup", PopupOptions["FADETOLOADING"])
			cfunction("callCPPFunction", "fadeSound", "0", 0)
		end	
	else
		if pauseState_curSel == 1 then
			destroyPopup("pauseState_onAfterDestroyPopup", PopupOptions["FADETONORMAL"])
		elseif pauseState_curSel == 4 then				
			cfunction("transitionToState", "controlScreen")				
		elseif pauseState_curSel == 3 then
			cfunction("transitionToState", "soundAndMusicScreen")
		else
			destroyPopup("pauseState_onAfterDestroyPopup", PopupOptions["FADETOLOADING"])
			cfunction("callCPPFunction", "fadeSound", "0", 0)
		end	
	end
end

function pauseState_onAfterDestroyPopup()
	if pauseState_curSel == 1 then
		cfunction("callCPPFunction", "setSoundVolume", "", soundAndMusicScreen_soundVol*11.1)
		cfunction("callCPPFunction", "setMusicVolume", "", soundAndMusicScreen_musicVol*11.1)		
	
	end
	
	if flow_gameMode == gameTypesIds["MULTIPLAYER"] then	
		if pauseState_curSel == 1 then
			cfunction("exitScriptSystem", UIReturnCodes["MULTIPLAYER_RETURNGAME"])
		elseif pauseState_curSel == 4 then
			cfunction("exitScriptSystem",UIReturnCodes["MULTIPLAYER_EXIT"])
		else
			cfunction("debugPrintF", "Wrong pauseState_curSel in pauseState_onAfterDestroyPopup", pauseState_curSel)	
		end
	elseif flow_gameMode == gameTypesIds["CHALLENGE"] then			
		if pauseState_curSel == 1 then
			cfunction("exitScriptSystem", UIReturnCodes["CHALLENGE_RETURNGAME"])			
		elseif pauseState_curSel == 2 then
			cfunction("exitScriptSystem",UIReturnCodes["CHALLENGE_RESTART"])
		elseif pauseState_curSel == 6 then
			cfunction("exitScriptSystem",UIReturnCodes["CHALLENGE_EXIT"])
		else
			cfunction("debugPrintF", "Wrong pauseState_curSel in pauseState_onAfterDestroyPopup", pauseState_curSel)	
		end	
	elseif flow_gameMode == gameTypesIds["ARCADE"] then
		if pauseState_curSel == 1 then
			cfunction("exitScriptSystem", UIReturnCodes["ARCADE_RETURNGAME"])
		elseif pauseState_curSel == 2 then
			cfunction("exitScriptSystem",UIReturnCodes["ARCADE_RESTART"])
		elseif pauseState_curSel == 5 then
			cfunction("exitScriptSystem",UIReturnCodes["ARCADE_EXIT"])
		else
			cfunction("debugPrintF", "Wrong pauseState_curSel in pauseState_onAfterDestroyPopup", pauseState_curSel)	
		end	
	elseif flow_gameMode == gameTypesIds["TIMETRIAL"] then
		if pauseState_curSel == 1 then
			cfunction("exitScriptSystem", UIReturnCodes["TIMETRIAL_RETURNGAME"])
		elseif pauseState_curSel == 2 then
			cfunction("exitScriptSystem",UIReturnCodes["TIMETRIAL_RESTART"])
		elseif pauseState_curSel == 5 then
			cfunction("exitScriptSystem",UIReturnCodes["TIMETRIAL_EXIT"])
		else
			cfunction("debugPrintF", "Wrong pauseState_curSel in pauseState_onAfterDestroyPopup", pauseState_curSel)	
		end
	elseif flow_gameMode == gameTypesIds["TUTORIAL"] then
		if pauseState_curSel == 1 then
			cfunction("exitScriptSystem", UIReturnCodes["TUTORIAL_RETURNGAME"])
		elseif pauseState_curSel == 2 then
			cfunction("exitScriptSystem",UIReturnCodes["TUTORIAL_RESTART"])
		elseif pauseState_curSel == 5 then
			cfunction("exitScriptSystem",UIReturnCodes["TUTORIAL_EXIT"])
		else
			cfunction("debugPrintF", "Wrong pauseState_curSel in pauseState_onAfterDestroyPopup", pauseState_curSel)	
		end	
	elseif flow_gameMode == gameTypesIds["TOURNAMENT"] then
		if pauseState_curSel == 1 then
			cfunction("exitScriptSystem", UIReturnCodes["TOURNAMENT_RETURNGAME"])
		elseif pauseState_curSel == 2 then
			cfunction("exitScriptSystem",UIReturnCodes["TOURNAMENT_RESTART"])
		elseif pauseState_curSel == 5 then
			cfunction("exitScriptSystem",UIReturnCodes["TOURNAMENT_EXIT"])
		else
			cfunction("debugPrintF", "Wrong pauseState_curSel in pauseState_onAfterDestroyPopup", pauseState_curSel)	
		end
	else
		cfunction("debugPrintF", "Wrong gameMode in pauseState_onAfterDestroyPopup", flow_gameMode)
	end		
	
end



function pauseState_afterWindowOpen()
	cfunction("showFontOverlay", "menu_content", 1, 100, 0, 136-((pauseState_numOptions*0.5)*14))	
	cfunction("enableInput", 1)
	pauseState_setSelectionCursor()
end

function pauseState_afterWindowClose()
	if flow_gameMode == gameTypesIds["MULTIPLAYER"] then
		if pauseState_curSel == 1 then
			cfunction("fadeOverlay", "frameTop", 100,0,frameFadeInOutTime)	
			cfunction("fadeOverlay", "frameBottom", 100,0,frameFadeInOutTime)	
			cfunction("fadeOverlay", "menuBgBlack", 50,0,frameFadeInOutTime)			
			cfunction("setNumberTimer", frameFadeInOutTime, "pauseState_goBackToGame", UIReturnCodes["MULTIPLAYER_RETURNGAME"])	
		elseif pauseState_curSel == 3 then				
			cfunction("transitionToState", "controlScreen")				
		elseif pauseState_curSel == 2 then
			cfunction("transitionToState", "soundAndMusicScreen")
		else
			cfunction("showBitmapOverlay", "menuBgBlack", 0,0,0,0)			
			cfunction("showBitmapOverlay", "blackPix1", 1, 0, 0,0)
			cfunction("scaleBitmapOverlay", "blackPix1", 1, 1, 480, 272,0)
			cfunction("fadeOverlay", "blackPix1", 50, 100, 1000)
			cfunction("setTimer", 1000, "pauseState_afterWait")	
			cfunction("setTimer", 800, "pauseState_afterWaitLoading")	
			cfunction("callCPPFunction", "fadeSound", "0", 0)
		end	
	elseif flow_gameMode == gameTypesIds["CHALLENGE"] then
		if pauseState_curSel == 1 then
			cfunction("fadeOverlay", "frameTop", 100,0,frameFadeInOutTime)	
			cfunction("fadeOverlay", "frameBottom", 100,0,frameFadeInOutTime)	
			cfunction("fadeOverlay", "menuBgBlack", 50,0,frameFadeInOutTime)		
			cfunction("setNumberTimer", frameFadeInOutTime, "pauseState_goBackToGame", UIReturnCodes["CHALLENGE_RETURNGAME"])			
		elseif pauseState_curSel == 4 then				
			cfunction("transitionToState", "controlScreen")				
		elseif pauseState_curSel == 5 then				
			cfunction("transitionToState", "challengeObjectives")				
		elseif pauseState_curSel == 3 then
			cfunction("transitionToState", "soundAndMusicScreen")
		else
			cfunction("showBitmapOverlay", "menuBgBlack", 0,0,0,0)			
			cfunction("showBitmapOverlay", "blackPix1", 1, 0, 0,0)
			cfunction("scaleBitmapOverlay", "blackPix1", 1, 1, 480, 272,0)
			cfunction("fadeOverlay", "blackPix1", 50, 100, 1000)
			cfunction("setTimer", 1000, "pauseState_afterWait")	
			cfunction("setTimer", 800, "pauseState_afterWaitLoading")	
			cfunction("callCPPFunction", "fadeSound", "0", 0)
		end
	else
		if pauseState_curSel == 1 then
			cfunction("fadeOverlay", "frameTop", 100,0,frameFadeInOutTime)	
			cfunction("fadeOverlay", "frameBottom", 100,0,frameFadeInOutTime)	
			cfunction("fadeOverlay", "menuBgBlack", 50,0,frameFadeInOutTime)		
			
			if flow_gameMode == gameTypesIds["ARCADE"] then
				cfunction("setNumberTimer", frameFadeInOutTime, "pauseState_goBackToGame", UIReturnCodes["ARCADE_RETURNGAME"])
			elseif flow_gameMode == gameTypesIds["TOURNAMENT"] then
				cfunction("setNumberTimer", frameFadeInOutTime, "pauseState_goBackToGame", UIReturnCodes["TOURNAMENT_RETURNGAME"])
			else
				cfunction("debugPrintF", "Wrong gameMode in pauseState_afterWindowClose", flow_gameMode)
			end
			
		elseif pauseState_curSel == 4 then				
			cfunction("transitionToState", "controlScreen")				
		elseif pauseState_curSel == 3 then
			cfunction("transitionToState", "soundAndMusicScreen")
		else
			cfunction("showBitmapOverlay", "menuBgBlack", 0,0,0,0)			
			cfunction("showBitmapOverlay", "blackPix1", 1, 0, 0,0)
			cfunction("scaleBitmapOverlay", "blackPix1", 1, 1, 480, 272,0)
			cfunction("fadeOverlay", "blackPix1", 50, 100, 1000)
			cfunction("setTimer", 1000, "pauseState_afterWait")	
			cfunction("setTimer", 800, "pauseState_afterWaitLoading")	
			cfunction("callCPPFunction", "fadeSound", "0", 0)
		end	
	end
end

function pauseState_goBackToGame(xReturnCode)
	cfunction("showBitmapOverlay", "frameTop", 0,100,112,104)	
	cfunction("showBitmapOverlay", "frameBottom", 0,100,112,136)	
	cfunction("showBitmapOverlay", "menuBgBlack", 0,0,0,0)
	frameState = 0
	
	cfunction("callCPPFunction", "setSoundVolume", "", soundAndMusicScreen_soundVol*11.1)
	cfunction("callCPPFunction", "setMusicVolume", "", soundAndMusicScreen_musicVol*11.1)
	
	cfunction("exitScriptSystem", xReturnCode)
end



function pauseState_setSoundVolume(f)	
	soundAndMusicScreen_soundVol = myAbs(f*11.1)	
end
function pauseState_setMusicVolume(f)
	soundAndMusicScreen_musicVol = myAbs(f*11.1)
end

function myAbs(f)
	if f < 1 then
		return 0
	elseif f < 2 then
		return 1
	elseif f < 3 then
		return 2
	elseif f < 4 then
		return 3
	elseif f < 5 then
		return 4
	elseif f < 6 then
		return 5
	elseif f < 7 then
		return 6
	elseif f < 8 then
		return 7
	elseif f < 9 then
		return 8
	else
		return 9
	end
end
