#include "common.lua"
#include "game.lua"
#include "ui/ui_extensions.lua"

pTime = GetFloatParam("time", 10)
pWaitForTrigger = GetStringParam("trigger", "")
pLetterbox = GetBoolParam("letterbox", false)
pWaitTime = GetFloatParam("fadeWaitTime", 5)
pFinalFadeTime = GetFloatParam("finalFadeTime", 3)

time = 0
cinematic = nil
index = 1
cam = false
wait_for_trigger = false
end_triggered = false
time_to_end = pWaitTime

pCredits = nil

function init()
	if pWaitForTrigger ~= nil and string.len(pWaitForTrigger) ~= 0 then
		wait_for_trigger = true
		RegisterListenerTo(pWaitForTrigger, "triggerEnd")
	end

	local missionId = GetString("game.levelid")
	for _,c in pairs(gCinematic) do
		for i=1,#c.parts do
			if c.parts[i].id == missionId then
				cinematic = c
				index = i
			end
		end
	end

	if not cinematic then
		cinematic = {}
		cinematic.music = ""
		cinematic.esc = "nothing"
		cinematic.parts = { { id="", file="", layers="", time=10} }
	end

	PlayMusic(cinematic.music)

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

function tick(dt)
	SetBool("game.disablepause", true)
	SetBool("game.disablemap", true)
	UiMakeInteractive()
    UiSetCursorState(UI_CURSOR_HIDE)

	if InputPressed("esc") or wasAnyActionPressed({"crouch", "menu_cancel"}) then
		exitSequence()
	end

	time = time + dt

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

	UiMute(1)

    if not wait_for_trigger and pTime - time <= pWaitTime then
        triggerEnd()
    end

    if end_triggered then
        time_to_end = time_to_end - dt
        if time_to_end <= 0 then
            endCinematics()
        end
    end
end

function triggerEnd()
    if not end_triggered then
        end_triggered = true
    end
end

function endCinematics()
    resetEventListener()
	local nextPart = cinematic.parts[index+1]
	if nextPart then
		StartLevel(nextPart.id, nextPart.file, nextPart.layers, true)
	else
		exitSequence()
	end
end

function resetEventListener()
    UnregisterListener(pWaitForTrigger, "triggerEnd")
end

function exitSequence()
    resetEventListener()
	if cinematic.esc=="hub" then
		startHub()
	else
		Menu()
	end
end


function draw()
    draw_letterbox()
    draw_credits_title()
    fade()
end

function draw_credits_title()
	if pCredits then
		UiPush()
			UiColor(1,1,1)
			UiFont("bold.ttf", 64)
			UiTextOutline(0,0,0,1.0,0.6)

			UiTranslate(100, UiHeight()-250)
			UiFont("bold.ttf", 32)
			UiText("loc@UI_TEXT_A_GAME")
			UiFont("bold.ttf", 64)
			UiTranslate(0, 50)
			UiText("loc@UI_TEXT_TUXEDO_LABS")
		UiPop()
	end
end

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

function fade()
	local black = 0
	if index == #cinematic.parts then
		if time <= 1 then
			black = 1-time
		end
		if end_triggered then
			black = 1-time_to_end/pFinalFadeTime
			UiMute(black, true)
		end
	else
		if time <= 1 then
			black = 1-time
		end
		if not wait_for_trigger and pTime-time < 1 then
			black = 1-(pTime-time)
		end
	end
	UiPush()
		UiColor(0,0,0, clamp(black, 0, 1))
		UiRect(UiWidth(), UiHeight())
	UiPop()
end
