#include "script/common.lua"
pTimeLimit = GetFloatParam("timelimit", 60)

pSound = GetStringParam("sound", "escape-car.ogg")
pAcceleration = GetFloatParam("acceleration", 6)
pMaxSpeed = GetFloatParam("maxspeed", 22)
pFollowCam = GetIntParam("followcam", 1)
pDynamic = GetBoolParam("dynamic", false)
pArrowSize = 2

function init()
	done = false
	
	timer = -1

	timeBeforeWin = 0

	warnTime = 0

	fadeToBlackAlpha = 0
	fadeInAlpha = 4

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

	--Escape setup
	local arrow = FindLocation("arrow")
	if arrow ~= 0 then
		arrowSprite = LoadSprite("gfx/arrowdown.png")
		arrowPos = GetLocationTransform(arrow).pos
		arrowPos[2] = arrowPos[2] + pArrowSize/2
	end

	cameraPos = GetLocationTransform(FindLocation("escapecamera")).pos
	body = FindBody("escapevehicle")
	shapes = GetBodyShapes(body)
	for i=1,#shapes do
		SetShapeEmissiveScale(shapes[i], 0.0)
	end
	
	SetTag(body, "nocull")
	--SetTag(body, "interact", "Pick up newspapers")
	transform = GetBodyTransform(body)
	dir = TransformToParentVec(transform, Vec(0, 0, -1))
	sound = LoadSound(pSound)
	speed = 0
	followCam = true
	hatch = FindShape("hatch")

	rested = false
	fadeDone = false

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


function tick(dt)

	local state = GetString("level.state")
	local complete = GetBool("level.complete")

	-- handle music
	if GetString("level.state") == "" then
		if GetBool("level.delivering") and not complete then
			PlayMusic("MOD/music/tillaggaryd_hunted.ogg")
		else
			PlayMusic("MOD/music/tillaggaryd.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", "Òöãôºé çãëéöº")
		end
		return
	end

	--Third person win/lose camera
	if state == "win" or state == "fail_alarmtimer" or state == "fail_missiontimer" then
		if followCam then
			t = Transform(cameraPos, QuatLookAt(cameraPos, GetBodyTransform(body).pos))
			if pFollowCam == 0 then
				followCam = false
			end
		end
		SetCameraTransform(t, 90)
	end

	

	if not fadeDone and rested then
		-- Start fireworks when fade is done
		if fadeToBlackAlpha <= 0 then
			--SetBool("game.disableinput", false)
			fadeDone = true
			SetTag(body, "interact", "Åëêãíöé èòðòí")
			
			SetBool("level.delivering",true)

			if state == "" then
				SetFloat("level.timelimit", pTimeLimit + GetFloat("level.missiontime") + GetFloat("options.game.campaign.time"))
			end
		end
		SetBool("hud.disable",true)		
		SetBool("game.disableinput", true)
		return
	end

	if not rested and GetTime() > 5 then 
		--Draw rested arrow
		if arrowSprite then
			local c = GetCameraTransform().pos
			c[2] = arrowPos[2]
			local p = VecCopy(GetBodyTransform(body).pos)
			p[2] = p[2] + math.sin(GetTime()*5)*0.3+3.8
			local t = Transform(p, QuatLookAt(arrowPos, c))
			DrawSprite(arrowSprite, t, pArrowSize, pArrowSize, 1, 1, 1, 1, true, false)
		end
	end

	if state == "" then		
		if GetPlayerInteractBody() == body then
			if complete then
				DrawBodyOutline(body, 1)
			end
			if InputPressed("interact") then
				if complete then
					SetString("level.state", "win")
				else
					if fadeDone then
						SetString("hud.notification", "Òñãýãïã åãð ñùêñò åºóòïñìö¼ åõé ñéòäûòèìðºé üéïì")
					end
				end
			end
		end	
		if GetPlayerInteractBody() == body and not GetBool("level.delivering") and not rested then
            
			DrawBodyOutline(body, 1)
			if InputPressed("interact") then
				SetString("hud.notification", "")
                RegisterTool("papergun","Paper Gun","MOD/vox/papergun.vox")
                SetBool("game.tool.papergun.enabled",true)
                SetString("game.player.tool","papergun")
                SetInt("game.tool.papergun.ammo",20)

				SetBool("level.run",true)

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

				fadeToBlackAlpha = 4
				SetValue("fadeToBlackAlpha",0,"linear",4)
				RemoveTag(body, "interact")
				SetBool("level.disablequicksave", true)				
			end
		end
		if complete then
			SetTag(body, "interact", "Åëêãíöé èòðòí")
			--Draw escape vehicle arrow
			if arrowSprite then
				local c = GetCameraTransform().pos
				c[2] = arrowPos[2]
				local p = VecCopy(arrowPos)
				p[2] = p[2] + math.sin(GetTime()*5)*0.3+0.3
				local t = Transform(p, QuatLookAt(arrowPos, c))
				DrawSprite(arrowSprite, t, pArrowSize, pArrowSize, 1, 1, 1, 1, true, false)
			end
		end

		
	end

	if done then
		return
	end

	
end

function update(dt)
	--Drive away
	if GetString("level.state") == "win" then
		if speed == 0 then
			PlaySound(sound)
			for i=1,#shapes do
				SetShapeEmissiveScale(shapes[i], 1.0)
			end
			--Close hatch on trucks
			if hatch ~= 0 then
				local t = GetShapeLocalTransform(hatch)
				t.rot = Quat()
				SetShapeLocalTransform(hatch, t)
			end
		end
		speed = speed + pAcceleration * dt
		if speed > pMaxSpeed then speed = pMaxSpeed end
		local vel = VecScale(dir, speed)
		transform.pos = VecAdd(transform.pos, VecScale(vel, dt))
		if pDynamic then
			SetBodyDynamic(body, true)
			SetBodyVelocity(body, vel)
		end
		SetBodyTransform(body, transform)
	end
end

function draw()
	if GetBool("game.tool.papergun.enabled") == false then
		return
	end
	-- Rest notification
	if not rested then
		local x, y, d = UiWorldToPixel(GetBodyTransform(body).pos)
		if d > 0 then
			local restInVanAlpha = math.min(math.max(1.0-(d-10)/5, 0.0), 1.0)
			UiPush()
				UiFont("bold.ttf", 24)
				UiTranslate(x, y - 50)
				UiAlign("center middle")
				local msg = "Ðòèçòöòå¼öé õåòí ðãôþôùö, ëãöéð óòèòêèìöé å ðãþìñé"
				local w, h = UiGetTextSize(msg)
				UiColorFilter(1, 1, 1, restInVanAlpha)
				UiColor(1,1,1,0.5)
				UiImageBox("ui/common/box-solid-10.png", w+20, h+10, 6, 6)
				UiColor(0, 0, 0)
				UiText(msg)
			UiPop()
		end
	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