#include "script/common.lua"

pTimeLimit = GetFloatParam("timelimit", 60)
pArrowSize = 2

function init()
	fadeToBlackAlpha = 0
	fadeInAlpha = 4

	SetBool("level.delivering", false)
	SetBool("level.deliveryComplete", false)
	Spawned = false

	vehicle = FindBody("vehicle")

    arrowSprite = LoadSprite("gfx/arrowdown.png")
	local vehicleArrow = FindLocation("vehicleArrow")
    local vehicleArrowTr = GetLocationTransform(vehicleArrow)
    vehicleArrowTr.pos[2] = vehicleArrowTr.pos[2] + pArrowSize/2
    vehicleArrowLTr = TransformToLocalTransform(GetBodyTransform(vehicle), vehicleArrowTr)
    Delete(vehicleArrow)

	shapes = GetBodyShapes(vehicle)
	for i=1, #shapes do SetShapeEmissiveScale(shapes[i], 0.0) end

	SetTag(vehicle, "nocull")
	SetTag(vehicle, "interact", "loc@START_MISSION")

	rested = false
	fadeDone = false

	SetString("level.state", "")
end


function tick(dt)
	local state = GetString("level.state")

	-- handle music
	if GetString("level.state") == "" then
		if GetBool("level.delivering") and not GetBool("level.complete") then
			PlayMusic("MOD/music/heist.ogg")
		else
			PlayMusic("MOD/music/ambient.ogg")
		end
	end

	--gotta pick up the old newspapers first
	if GetBool("game.tool.papergun.enabled") == false then
		--gotta pick up the old newspapers first
		if GetTime() > 10 then
			SetString("hud.notification", GetTranslatedStringByKey("UI_HUD_NOTE_PICK_UP"))
		end
		return
	end

	if not fadeDone and rested then
		if fadeToBlackAlpha <= 0 then
			fadeDone = true

			SetBool("level.delivering",true)

			if state == "" then
				SetFloat("level.timelimit", pTimeLimit + GetFloat("level.missiontime") + GetFloat("options.game.campaign.time"))
			end
		end
        if not Spawned and fadeInAlpha == 0 then
            local tr = GetBodyTransform(vehicle)
            Delete(vehicle)
            Spawn("stationwagon2.xml", tr, true, true)
            Spawned = true
        end
		SetBool("hud.disable", true)
		SetBool("game.disableinput", true)
		return
	end

	if state == "" then
		if not GetBool("level.delivering") and not rested then
			if arrowSprite and not GetBool("game.map.enabled") then
				local c = GetCameraTransform().pos
                local tr = TransformToParentTransform(GetBodyTransform(vehicle), vehicleArrowLTr)
				c[2] = tr.pos[2]
				local p = VecCopy(tr.pos)
				p[2] = p[2] + math.sin(GetTime()*5)*0.3+0.3
				local t = Transform(p, QuatLookAt(tr.pos, c))
				DrawSprite(arrowSprite, t, pArrowSize, pArrowSize, 1, 1, 1, 1, true, false)
			end
		end

		if GetPlayerInteractBody() == vehicle and not GetBool("level.delivering") and not rested then
			DrawBodyOutline(vehicle, 1)
			if InputPressed("interact") then
				SetString("hud.notification", "")
                RegisterTool("papergun", "PAPER_GUN", "MOD/script/missions/mission_paperboy/papergun.xml")
                SetBool("game.tool.papergun.enabled", true)
                SetString("game.player.tool", "papergun")
                SetInt("game.tool.papergun.ammo", 10)

				SetBool("level.run",true)

				rested = true
				fadeInAlpha = 4
				SetValue("fadeInAlpha", 0, "linear", 0.5)

				fadeToBlackAlpha = 4
				SetValue("fadeToBlackAlpha", 0 ,"linear", 4)
				RemoveTag(vehicle, "interact")
				SetBool("level.disablequicksave", true)
			end
		end
	end
end

function draw()
	if GetBool("game.tool.papergun.enabled") == false then
		return
	end

	if GetString("level.state") ~= "" then
		return
	end

	if fadeToBlackAlpha > 0 then
		UiPush()
			UiColor(0, 0, 0, fadeToBlackAlpha-fadeInAlpha)
			UiTranslate(UiCenter(), UiMiddle())
			UiAlign("center middle")
			UiRect(UiWidth(), UiHeight())
		UiPop()
	end
end
