#include "script/common.lua"
#include "script/wheels.lua"

tid = nil
luggageDone = nil
toolsDone = nil
terminalDone = nil
arrow = nil
luggage = nil
luggageTrigger = nil
bungalowMarker = nil
toolsLocation = nil
terminalScreen = nil
terminalBody = nil
unpackSound = nil
tools = nil
bus = nil
busVel = nil
busTransform = nil
busSound = nil
busActive = nil
busShown = nil

function init()
	tid = 0
	--SetBool("level.flightdone", true)

	luggageDone = false
	toolsDone = false
	terminalDone = false
	
	arrow = LoadSprite("gfx/arrowdown.png")
	luggage = FindBody("luggage")
	luggageTrigger = FindTrigger("luggage")
	bungalowMarker = FindBody("bungalow")
	toolsLocation = FindLocation("tools")

	terminalScreen = FindScreen("terminal", true)
	terminalBody = GetShapeBody(GetScreenShape(terminalScreen))

	unpackSound = LoadSound("unpack.ogg")
	
	--Turn off terminal and hide it
	SetScreenEnabled(terminalScreen, false)
	RemoveTag(terminalBody, "interact")
	RemoveTag(terminalBody, "map")
	hideBody(terminalBody)
	
	--Hide tool pickups
	tools = FindBodies("tool")
	for i=1,#tools do
		hideBody(tools[i])
	end
	
	--Disable all tools
	local toolsNames = ListKeys("game.tool")
	for i=1,#toolsNames do
		local k = "game.tool."..toolsNames[i]..".enabled"
		if GetBool(k) then
			SetBool(k, false)
		end
	end
	SetString("game.player.tool", "none")	
	
	bus = FindBody("bus")
	busVel = Vec(0,0,0)
	busTransform = GetBodyTransform(bus)
	busSound = LoadSound("LEVEL/script/bus.ogg")
	busActive = false
	hideBody(bus)
	busShown = false
	wheelInit(bus)
end


function hideBody(b)
	local t = GetBodyTransform(b)
	t.pos[2] = t.pos[2] - 100
	SetBodyTransform(b, t)
end

function showBody(b)
	local t = GetBodyTransform(b)
	t.pos[2] = t.pos[2] + 100
	SetBodyTransform(b, t)
end


function drawArrowAtPos(pos, minDist, size)
	if GetBool("game.map.enabled") then
		return
	end
	local dp = VecSub(GetCameraTransform().pos, pos)
	dp[2] = 0
	local dist = VecLength(dp)
	local alpha = clamp(dist - minDist, 0.0, 1.0)
	local t = Transform(pos)
	t.pos = VecAdd(t.pos, Vec(0,0.5, 0))
	t.rot = QuatLookAt(t.pos, GetCameraTransform().pos)
	local offset = 1.5 + math.sin(4.0*GetTime())*0.5
	t.pos[2] = t.pos[2] + offset
	DrawSprite(arrow, t, size, size, .3, .3, .3, alpha, false, true)
	DrawSprite(arrow, t, size, size, 1, 1, 1, alpha, true, true)
end


function tick(dt)
	if not GetBool("level.flightdone") then
		return
	end

	tid = tid + dt
	
	if not busShown then
		showBody(bus)
		busShown = true
	end
	
	if tid > 0.5 then
		if not busActive then
			PlaySound(busSound)
			busActive = true
		end
		local fwd = TransformToParentVec(busTransform, Vec(0, 0, 1))
		busVel = VecAdd(busVel, VecScale(fwd, dt*2.0))
		busTransform.pos = VecAdd(busTransform.pos, VecScale(busVel, dt))
		wheelUpdate(-VecLength(busVel))
		SetBodyTransform(bus, busTransform)
	end

	if tid < 1.5 then
		return
	end
	
	if not luggageDone then
		if GetPlayerInteractBody() == luggage and InputPressed("interact") then
			luggageDone = true
			Delete(luggage)
			PlaySound(unpackSound)
			showBody(terminalBody)
			for i=1,#tools do
				showBody(tools[i])
			end
		else
			if IsBodyInTrigger(luggageTrigger, luggage) then
				SetTag(luggage, "interact", "loc@UNPACK")
				SetString("hud.hintinfo", "loc@UI_HUD_HINT_UNPACK_YOUR")
			else
				RemoveTag(luggage, "interact")
				if GetPlayerGrabBody() ~= luggage then
					drawArrowAtPos(GetBodyTransform(luggage).pos, 5, 2)
					SetString("hud.hintinfo", "loc@UI_HUD_HINT_PICK_UP_LUGGAGE")
				else
					local p = GetBodyTransform(bungalowMarker).pos
					SetCameraDof(VecLength(VecSub(GetCameraTransform().pos, p)))
					drawArrowAtPos(p, 5, 4)
					SetString("hud.hintinfo", "loc@UI_HUD_HINT_TAKE_IT")
				end
			end
		end
		return
	end

	if not toolsDone then
		drawArrowAtPos(GetLocationTransform(toolsLocation).pos, 5, 2)
		SetString("hud.hintinfo", GetTranslatedStringByKey("UI_HUD_HINT_PICK_UP_TOOLS"))
		if GetBool("game.tool.sledge.enabled") and GetBool("game.tool.extinguisher.enabled") and GetBool("game.tool.spraycan.enabled") then
			toolsDone = true
			SetScreenEnabled(terminalScreen, true)
			SetTag(terminalBody, "interact", "loc@OPERATE")
			SetTag(terminalBody, "map")
		end
		return
	end

	if not terminalDone then
		drawArrowAtPos(GetBodyTransform(terminalBody).pos, 3, 1.5)
		SetString("hud.hintinfo", GetTranslatedStringByKey("UI_HUD_HINT_CHECK_YOUR"))
		if GetPlayerScreen() == terminalScreen then
			terminalDone = true
		end
	end
end



