#include "../../../config/missions_fp.lua"
#include "../../../config/events.lua"
#include "../../maps_manager.lua"
#include "../end_screen.lua"
#include "script/common.lua"
#include "ui/ui_helpers.lua"
#include "ui/ui_extensions.lua"

-- Custom heist mission implementation
-- This script requires heist.lua in the level to work properly

function init()
	state = ""

	missionId = "paperboy"
	SetBool("level.campaign", true) -- To get adjustment of health and mission time to work properly
	SetString("level.mission", missionId)

	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.mission."..missionId..".score")
		local oldTimeLeft = GetFloat("savegame.mod.mission."..missionId..".timeleft")

		if score > oldScore or (score == oldScore and timeLeft > oldTimeLeft) then
			highScore = true
			if GetInt("savegame.mod.mission."..missionId..".score") == 0 then
				SetString("savegame.mod.lastcompleted", missionId)
			end
			SetInt("savegame.mod.mission."..missionId..".score", score)
			SetFloat("savegame.mod.mission."..missionId..".timeleft", timeLeft)
			SetFloat("savegame.mod.mission."..missionId..".missiontime", missionTime)
            if not GetBool("savegame.mod.mission." .. missionId) then
                SetBool("savegame.mod.mission."..missionId, true)
                TriggerEvent(EVENTS.PICK_UP_MONEY, MissionsFP[missionId].reward .. ';anim_delay=1.1')
            end
		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()
	local canvas = UiCanvasSize()
	UiWindow(canvas.w, canvas.h)

	--End screen
	if endScreen > 0 then
		-- for end screen we do not want to draw a cursor
		if LastInputDevice() == UI_DEVICE_GAMEPAD then
			UiSetCursorState(UI_CURSOR_HIDE_AND_LOCK)
		else
			UiSetCursorState(UI_CURSOR_SHOW)
		end
		--Set gEndScreenScale to ease in the end screen
		SetValue("endScreenScale", 1, "easeout", 0.5)
		--Call to draw the endscreen
		-- drawEndScreen(endScreenScale, state)	

		EndScreen.draw(endScreenScale, state, MissionsFP, missionId, {
			exit = GoToGarage,
			skip = function(id)
				SetInt("savegame.mod.mission."..id..".score", MissionsFP[id].required)
				SetBool("savegame.mod.mission."..id, true)
				SetString("savegame.mod.lastcompleted", id)
				-- syncActivities(id, false, true)
			end,
		})
	end
end
