#include "game.lua"


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


function getFirst(str,split)
	split = split or " "
	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 " "
	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


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

function newsIntro(dt, params)
	if gTime == 0 then
		logo = 0
		breaking = -400
		news = 400
		white = 0
		UiSound("tv/news-intro.ogg")
	end

	if justPassed(0.2) then
		SetValue("logo", 8, "bounce", 1)
	end
	if justPassed(1.3) then
		SetValue("breaking", 30, "easeout", 0.4)
	end
	if justPassed(1.5) then
		SetValue("news", 30, "easeout", 0.4)
	end
	if justPassed(1.9) then
		white = 0.5
		SetValue("white", 0, "linear", 1.4)
	end

	UiPush()
		UiTranslate(UiCenter(), UiMiddle())
		UiFont("bold.ttf", 24)
		UiColor(0.5,0,0)
		UiPush()
			UiTranslate(0, -50)
			UiScale(logo)
			UiAlign("center middle")
			UiText("LCN")
		UiPop()
		UiPush()
			UiTranslate(breaking, 70)
			UiScale(3.0)
			UiAlign("right")
			UiText("Ãòô¿ýìé")
		UiPop()
		UiPush()
			UiTranslate(news, 70)
			UiScale(3.0)
			UiAlign("left")
			UiText("ñòåòõöì")
		UiPop()
	UiPop()
	UiColor(1,1,1,white)
	UiRect(640, 480)
	
	return gTime < 4.0
end

function newsOutro(dt, params)
	host = "Host - Catlyn Sandstream"

	if gTime == 0 then
		logo = 0
		breaking = -400
		news = 400
		white = 0
		UiSound("tv/news-outro.ogg")
	end

	UiImage("tv/news_end.jpg")

	UiPush()
		UiFont("bold.ttf", 24)
		UiAlign("left middle")
		UiTranslate(0, 380)
		UiColor(1,1,1, 0.5)
		UiRect(330, 30)
		UiColor(0,0,0)
		UiTranslate(10, 0)
		UiText(host)
	UiPop()

	if justPassed(0.2) then
		SetValue("logo", 8, "bounce", 1)
	end
	
	UiPush()
		UiTranslate(UiCenter(), UiMiddle())
		UiFont("bold.ttf", 24)
		UiColor(0.5,0,0)
		UiPush()
			UiTranslate(0, -50)
			UiScale(logo)
			UiAlign("center middle")
			UiText("LCN")
		UiPop()
	UiPop()
	UiColor(1,1,1,white)
	UiRect(640, 480)
	
	return gTime < 4.0
end



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


function news(dt, params)
	reporter = params.reporter or "reporter"
	public = params.public or "man"

	if gTime == 0 then
		tickerPos = 900
		done = false
		hideticker = 1
		event = 0
		eventTimer = 0.0
		eventTime = 0.0
		endSound = false
				
		randomStudio = math.random(0,3)
		randomReporter = math.random(0,3)

	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()
					local t = 1.0
					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", 24)
			UiAlign("left middle")
			UiTranslate(0, -15)
			UiColor(1,1,1, 0.5)
			UiRect(330, 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", 24)
		UiScale(2.0)
		UiAlign("left middle")
		local w, h = UiText(params.ticker)
		if tickerPos + w*2.0 < 0 then
			done = true
		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("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
	gFrame = 0
	gFadeToBlack = 0

	gShows = {}
	addShow(news,
		{
			title = "Ìì òõùêèéñ ëã ñéùóïãöù ñãïòçòå",
			ticker = "Ðòõïé õéôìì ñéõå¿ëãññºû ðéêèù õòäòí ìñüìèéñöòå å îòðóãñìì Lee Chemicals, åïãèéïéü ì çéñéôãï¼ñºí èìôéîöòô Ìòùôéñõ Ìì-ðïãèþìí äºï ãôéõöòåãñ õéçòèñ¿ ùöôòð ëã ùîïòñéñìé òö ùóïãöº ñãïòçòå. Ã-êã Óéôèìðãñ ìë óòïìüìì çòåòôìö, ýöò äïãçòèãô¿ ûòôòþéí ôãäòöé óòïìüìì ì õìï¼ñòí óòèèéôêîé õòòäÿéõöåã ù ñìû éõö¼ ñéòóôòåéôêìðºé èòîãëãöéï¼õöåã.",
			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
	
	shakeX = 0
	shakeY = 0
	shakeDx = 0
	shakeDy = 0

	talk = false
	talkTimer = 10
	
end



function update()
	gFrame = gFrame + 1
	shakeDx = (shakeDx + math.random(-100,100)*0.0003) * 0.9
	shakeDy = (shakeDy + math.random(-100,100)*0.0003) * 0.9
	shakeX = (shakeX + shakeDx) * 0.99
	shakeY = (shakeY + shakeDy) * 0.99
	if shakeX > 1.0 then shakeX = 1.0 shakeDx = 0.0 end
	if shakeX < -1.0 then shakeX = -1.0 shakeDx = 0.0 end
	if shakeY > 1.0 then shakeY = 1.0 shakeDy = 0.0 end
	if shakeY < -1.0 then shakeY = -1.0 shakeDy = 0.0 end

	talkTimer = talkTimer - 1
	if talkTimer <= 0 then
		talk = not talk
		talkTimer = math.random(2, 10)
	end
	
	blink = (gFrame%130) < 5
end


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


