#include "missions.lua"
#include "starthub.lua"
-- Custom heist mission implementation
-- This script requires heist.lua in the level to work properly

function init()
	state = ""

	missionId = GetString("game.levelid")

	savedScore = false
	highScore = false
	score = 0
	timeLeft = 0

	endTimer = 0
	endScreen = 0

	endScreenScale = 0
	endScreenHeight = 300
end


function tick(dt)
	--Read the current level state. This is set by heist.lua
	state = GetString("level.state")
	
	--If level is completed we need to deal with score
	if state == "win" and not savedScore then
		--Get the score from registry. These values are set by heist.lua
		score = GetInt("level.clearedprimary") + GetInt("level.clearedsecondary")
		timeLeft = GetFloat("level.timeleft")
		missionTime = GetFloat("level.missiontime")

		--Save the new score if it's better than the current highscore
		local oldScore = GetInt("savegame.mod."..missionId..".score")
		local oldTimeLeft = GetFloat("savegame.mod."..missionId..".timeleft")
		
		if score > oldScore or (score == oldScore and timeLeft > oldTimeLeft) then
			highScore = true
			SetInt("savegame.mod."..missionId..".score", score)
			SetFloat("savegame.mod."..missionId..".timeleft", timeLeft)
			SetFloat("savegame.mod."..missionId..".missiontime", missionTime)
		end
		--Do this only once
		savedScore = true
	end
	
	--If level is no longer in default state we need to handle win and lose
	if state ~= "" then
		
		--Wait two seconds to allow the escape vehicle to drive off
		endTimer = endTimer + dt	
		if endTimer > 2 then	
			if endScreen == 0 then
				SetValue("endScreen", 1, "cosine", 0.25)
			end
		end
	end
end


function draw()
	--End screen
	if endScreen > 0 then
		--Set gEndScreenScale to ease in the end screen
		SetValue("endScreenScale", 1, "easeout", 0.5)
		--Call to draw the endscreen
		drawEndScreen(endScreenScale, state)					
	end	
end


function drawEndScreen(f, state)
	if f > 0 then
		UiPush()
			--This will bring up the mouse pointer and accept input
			UiMakeInteractive()

			--here we use the f parameter that is sent to the function to make the side bar slide in
			UiTranslate(-300+300*f, 0)

			--Dialog
			UiAlign("top left")
			UiColor(0, 0, 0, 0.7*f)
			UiRect(400, UiHeight())
			UiWindow(400, UiHeight())
			UiColor(1,1,1)
			UiPush()
				UiTranslate(0, 50)
				if state == "win" then
					--If state is win we display the mission complete screen, with score and time left
					UiPush()
						UiTranslate(UiCenter(), 0)
						UiAlign("center top")
						UiFont("bold.ttf", 44)
						UiScale(2)
						UiText("ÈÀÄÀÎÉÅ")
						UiTranslate(0, 35)
						UiScale(0.7)
						UiText("ÂÞÐÏÌÎÅÎÏ")
					UiPop()

					UiTranslate(0, 0)

					UiPush()
						UiTranslate(UiCenter(), 150)
						UiAlign("center")
						UiFont("bold.ttf", 32)
						if highScore then
							UiText("Îòåºí ôéëùï¼öãö "..score)
						else
							UiText("òýîòå "..score)
						end

						UiTranslate(0, 120)	
						UiFont("regular.ttf", 22)

						if GetInt("level.clearedprimary") == 0 then
							_, h = UiText("Ùéïì " .. GetInt("level.clearedsecondary"))
							UiTranslate(0, h)
						else
							_, h = UiText("Ïõñòåñºé üéïì " .. GetInt("level.clearedprimary"))
							UiTranslate(0, h)

							if gArtMissions[missionId].secondary > 0 then
								_, h = UiText("Âöòôìýñºé üéïì " .. GetInt("level.clearedsecondary"))
								UiTranslate(0, h)
							end
						end

						if timeLeft >= 0 then
						_, h = UiText("Ïõöãïòõ¼ åôéðéñì " .. math.floor(timeLeft*10)/10 .. "õ")
						end
					UiPop()
					UiTranslate(0, h)
				else
					--If state isn't win we draw the mission failed screen with the reason for failure.
					local h
					UiPush()
						UiTranslate(UiCenter(), 0)
						UiAlign("center top")
						UiPush()
							UiFont("bold.ttf", 44)
							UiScale(2)
							UiColor(.8, 0, 0)
							UiText("ÈÀÄÀÎÉÅ")
							UiTranslate(0, 32)
							UiScale(1.27)
							UiColor(1, 0, 0)
							UiText("ÐÑÏÂÀÌÅÎÏ")
						UiPop()
						UiFont("regular.ttf", 22)
						UiAlign("top left")
						UiTranslate(-144, 180)
						UiColor(.8, .8, .8)
						UiWordWrap(290)
						local reason = ""
						if state == "fail_dead" then
							reason = "Âº óòçìäïì. Âëôºåº, òçòñ¼, óãèéñì¿ ì óùïì ðòçùö óôìýìñìö¼ åãð åôéè. Òïéèìöé ëã õýéöýìîòð ëèòôòå¼¿"
						elseif state == "fail_alarmtimer" then
							reason = "Âãð ñé ùèãïòõ¼ õäéêãö¼ èò óôìäºöì¿ òûôãñº. Ðïãñ äºï ñéåéôñºí."
						elseif state == "fail_missiontimer" then
							reason = "Ô åãõ ëãîòñýìïòõ¼ åôéð¿. Ðòóôòäùíöé éÿé ôãë ì ñãíèìöé äòïéé óòèûòè¿ÿìé óùöì."
						end
						_,h = UiText(reason)
					UiPop()
					UiTranslate(0, 40+h)
				end
			UiPop()
			UiTranslate(0, UiHeight()-endScreenHeight)
			
			--Buttons at bottom
			UiPush()
				UiTranslate(UiCenter(), 0)
				UiFont("regular.ttf", 26)
				UiAlign("center middle")
				UiButtonImageBox("ui/common/box-outline-6.png", 6, 6, 1, 1, 1, 0.8)

				--If state is win we add the play again button
				if state == "win" then
					SetBool("savegame.mod."..missionId..".complete",true)

					UiPush()
						UiTranslate(0, -20)
						UiColor(.7, 1, .8, 0.2)
						UiImageBox("ui/common/box-solid-6.png", 260, 40, 6, 6)
						UiColor(1,1,1)
						if UiTextButton("Ðôòèòïêìö¼", 260, 40) then
							startHub()
						end
					UiPop()
					UiTranslate(0, 47)

					UiPush()
						UiTranslate(0, -20)
						--UiColor(.7, 1, .8, 0.2)
						--UiImageBox("ui/common/box-solid-6.png", 260, 40, 6, 6)
						UiColor(1,1,1)
						if UiTextButton("Éçôãö¼ õñòåã", 260, 40) then
							Restart()
						end
					UiPop()
					UiTranslate(0, 47)
				end

				--Add the quickload button if we are able to quick load
				UiPush()
					if not GetBool("game.canquickload") then
						UiDisableInput()
						UiColorFilter(1,1,1,0.5)
					end
					if UiTextButton("Áºõöôã¿ ëãçô.", 260, 40) then
						Command("game.quickload")
					end
				UiPop()
				UiTranslate(0, 47)

				--If the state isn't win we add the restart mission button
				if state ~= "win" then
					if UiTextButton("Ðéôéëãóùõî ëãèãñì¿", 260, 40) then
						Restart()
					end
					UiTranslate(0, 47)
					
					UiTranslate(0, 20)
					if UiTextButton("Ïöðéñã ðìõõìì", 260, 40) then
						startHub()
					end
					UiTranslate(0, 47)
				end
					
				--Add button to go back to the main menu
				UiTranslate(0, 20)
				if UiTextButton("Ãïãåñòé ðéñ¾", 220, 40) then
					Menu()
				end
				UiTranslate(0, 47)

				UiTranslate(0, 40)

				_,endScreenHeight = UiGetRelativePos()
			UiPop()
		UiPop()
	end
end

