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

local FONT_SIZE = 24
gFinishTrigger = GetStringParam("finishTrigger", "PART1_CUTSCENE_FINISH")

gTime = nil
gTimeLast = nil
gShows = nil

function justPassed(t)
	return gTime >= t and gTimeLast < t
end


function getFirst(str,split)
	split = split or " "
	local i = string.find(str, split)
	if i then
		return string.sub(str, 1, i-1)
	else 
		return str
	end
end

function getSecond(str, split)
	split = split or " "
	local i = string.find(str, split)
	if i then
		return string.sub(str, i+1, string.len(str))
	end
	return ""
end


--------------------------------------------------------------------------------------------------------------------


function pause(dt, params)
	return gTime < params.length
end


--------------------------------------------------------------------------------------------------------------------
tickerPos = nil
done = nil
hideticker = 0
event = nil
eventTimer = nil
eventTime = nil

function newsShow(dt, params)
	if gTime == 0 then
		tickerPos = 900
		done = false
		hideticker = 1
		event = 0
		eventTimer = 0.0
		eventTime = 0.0
	end

	if justPassed(1.0) then
		SetValue("hideticker", 0, "easeout", 1.0)
	end

	if eventTimer <= 0.0 then
		event = event + 1
		if event > #params.events then
			event = 1
		end
		eventTimer = 5.0
		eventTime = 0.0
	end
	
	if event > 0 then
		local e = params.events[event]
		local first = getFirst(e)
		local second = getSecond(e)
		if first == "live" then
			if second ~= "" then
				UiPush()
					UiTranslate(30, 30)
					UiScale(0.25)
					UiImage(second)
				UiPop()
			end
		end
	end
	
	eventTimer = eventTimer - dt
	eventTime = eventTime + dt
	
	UiPush()
		UiTranslate(0, 390 + 120*hideticker)
		UiColor(0.3, 0.0, 0.0)
		UiRect(UiWidth(), 60)

		UiPush()
			UiFont("bold.ttf", FONT_SIZE)
			UiAlign("left middle")
			UiTranslate(0, -15)
			UiColor(1,1,1, 0.5)
			local w, _ = GetTextSize(params.title)
			UiRect(w + 20, 30)
			UiColor(0,0,0)
			UiTranslate(10, 0)
			UiText(params.title)
		UiPop()

		
		UiColor(1,1,1)
		UiTranslate(tickerPos, 30)
		tickerPos = tickerPos - dt*170
		UiFont("bold.ttf", 48)
		UiAlign("left middle")
		w, _ = UiText(params.ticker)
		if not done and tickerPos + w < UiWidth() then
			done = true
            if gFinishTrigger ~= nil and #gFinishTrigger ~= 0 then
                TriggerEvent(gFinishTrigger)
            end
		end
	UiPop()

	UiPush()
		UiTranslate(UiWidth()-60, 12)
		UiColor(1, 1, 1, 0.5)
		UiRect(60, 26)

		UiTranslate(10, 20)
		UiColor(0.5, 0, 0, 1)
		UiFont("bold.ttf", 24)
		UiText("loc@UI_TEXT_LCN")
	UiPop()

	return not done
end

--------------------------------------------------------------------------------------------------------------------


function addShow(showFunc, showParams)
	gShows[#gShows+1] = {func = showFunc, params = showParams}
end


function isMissionCompleted(missionId)
	return GetInt("savegame.mission."..missionId..".score") > 0
end


function init()
	gTime = 0
	gTimeLast = 0

	gShows = {}
	addShow(newsShow,
		{
			title = GetTranslatedStringByKey("TITLE_LEE_CONVICTED_FOR_TAX_EVASION"),
			ticker = GetTranslatedStringByKey("TICKER_FOLLOWING_A"),
			events = 
			{
				"live",
				"live part1ending/lee-convicted1.jpg", 
				"live part1ending/lee-convicted2.jpg", 
				"live part1ending/lee-convicted3.jpg", 
				"live",
				"live part1ending/parisa-mugshot.jpg", 
				"live part1ending/parisa-mugshot.jpg", 
			}
		}	
	)

	gCurrentShow = 1
end

function draw()
	local scale = UiHeight()/480
	UiScale(UiHeight()/480)
	UiWindow(UiWidth()/scale, 480, true)
	UiAlign("left")
	local dt = GetTimeStep()
	UiPush()
		local _ = not gShows[gCurrentShow].func(dt, gShows[gCurrentShow].params)
	UiPop()
	gTime = gTime + dt
end


