#include "script/common.lua"
#include "starthub.lua"
#include "ui/helpers/input.lua"

DURATION = GetFloatParam("time", 10)
LETTERBOX = GetBoolParam("letterbox", false)

time = 0
hasCamera = false
fontsInititated = false
musicStarted = false

function remap(value, x1, y1, x2, y2)
	local l = x2 - x1
	local a = (clamp(value, x1, x2) - x1)/l
	return y1 + (y2 - y1) * a
end

pressedTimer = 0.0
function checkInputs()
	local key = InputLastPressedKey()
	if key ~= "" then
		return true
	else
		return wasAnyActionPressed({"interact", "usetool", "jump", "pause", "menu_prev", "menu_next",
			"rmb", "lmb", "zoom", "map", "menu_up", "menu_down", "menu_left", "menu_right", "menu_cancel",
			"menu_accept", "mmb", "extra0"})
	end
end

function init()
	local locs = FindLocations()
	if #locs >= 2 then
		hasCamera = true
		camStart = GetLocationTransform(locs[1])
		camEnd = GetLocationTransform(locs[2])
	end
end



function tick(dt)

	pressedTimer = pressedTimer - dt
	if (pressedTimer > 0.0 and checkInputs()) or GetBool("savegame.mod.cinematic.complete") then
		exitSequence();
	end

	if checkInputs() then
		pressedTimer = 4.0
	end

	if fontsInititated and not musicStarted then
		-- Start music after loading fonts
		if not GetBool("savegame.mod.cinematic.complete") then
			PlayMusic("MOD/script/intro/intro.ogg")
		end

		musicStarted = true
	end

	if musicStarted then
		time = time + dt

		if hasCamera then
			local t = time/(DURATION-1.5)
			t = clamp(t, 0.0, 1.0)
			local cam = Transform(VecLerp(camStart.pos, camEnd.pos, t), QuatSlerp(camStart.rot, camEnd.rot, t))
			SetCameraTransform(cam)
		end

		UiMute(1)

		if not done then
			if time > DURATION then
				exitSequence()
			end
		end
	end

end


function exitSequence()
	SetBool("savegame.mod.cinematic.complete",true)
	startHub()
	done = true
end


function draw()
	SetBool("game.disablepause", true)
	SetBool("game.disablemap", true)
	UiMakeInteractive()
	UiSetCursorState(1)

	local black = math.max(remap(time, 0.0, 1.0, 2.0, 0.0), remap(time, DURATION - 3.0,  0.0, DURATION - 1.5, 1.0))
	UiPush()
		UiColor(0,0,0, black)
		UiRect(UiWidth(), UiHeight())
	UiPop()

	if not fontsInititated then
		--just to remove font load stutter
		UiColor(0,0,0)
		UiFont("bold.ttf", 30)
		UiText(GetTranslatedStringByKey("UI_TEXT_FREYA_WOO"))
		UiFont("bold.ttf", 24)
		UiText(GetTranslatedStringByKey("UI_TEXT_FREYA_WOO"))
		fontsInititated = true
	end

	if pressedTimer > 0.0 then
		UiPush()
		UiFont("bold.ttf", 24)
		local alpha = math.min(pressedTimer, 0.5) / 0.5
		UiColor(1,1,1, alpha)
		UiTranslate(UiWidth()/2, UiHeight() - 110)
		UiAlign("center")
		UiText(GetTranslatedStringByKey("UI_TEXT_PRESS_AGAIN"))
		UiPop()
	end

	if LETTERBOX then
		UiPush()
			UiColor(0,0,0)
			UiRect(UiWidth(), 100)
			UiTranslate(0, UiHeight()-100)
			UiRect(UiWidth(), 100)
		UiPop()
	end

	if time > 4.2 then
		UiPush()
			UiAlign("center")

			local c = remap(time, DURATION-1.5, 1.0, DURATION-1.0, 0.0)
			UiColor(c,c,c)
			UiFont("bold.ttf", 30)

			UiTranslate(UiCenter(), UiMiddle())
			
			UiTranslate(0,-40)
			UiText(GetTranslatedStringByKey("UI_TEXT_FREYA_WOO"),true)
			
			UiTranslate(0, 50)
			if time > 6.3 then
				UiScale(3.3)
				UiText(GetTranslatedStringByKey("UI_TEXT_ART_VANDALS"))
			end

		UiPop()
	end
end

