#include "../script/helpers/utils.lua"
#include "ui/components/saber_credits.lua"
#include "music.lua"

local Credits = Credits
Credits.headerType = { p = "p", s1 = "s1", s2 = "s2", s3 = "s3" }
local header = Credits.createHeader
local p, s1, s2, s3 = Credits.headerType.p, Credits.headerType.s1, Credits.headerType.s2, Credits.headerType.s3
local group = Credits.createGroup

Credits.data = {
    header(s1, "Folkrace DLC Team"),

	header(s2, "Game design"),
	group("Game Designers", { "Egor Zherikhov", "Ivan Trofimov", "Igor Nereto"}),

	header(s2, "Art and Visuals"),
	group("Lead Artist", { "Anna Misharina"}),
	group("3D Artists", { "Alina Privalova", "Maria Gross", "Vladislav Kuzichkin", "Victoria Dementieva"}),
	group("Technical Artists", { "Vasilii Nikulov", "Lidia Zainullina"}),
	group("UI/UX Designer", { "Aleksandr Golubev"}),

	header(s2, "Engineering"),
	group("Lead Developer", { "Arseniy Zvezda"}),
	group("Developers", { "Daniil Agurevnin", "Igor Ryumkin", "Evgeny Smirnov", "Vera Balabenkova", "Nikita Aksenov"}),

	header(s2, "Music and sound design"),
	header(s3, "Music composed by: Douglas Holmquist"),
	group("Drums", { "Andreas Baw" }),
	group("Bass", { "David Carlsson" }),
	group("Guitars, Piano, Organ", { "Douglas Holmquist" }),
	group("Sound Designers", { "Egor Ivanov", "Dima Koltsov"}),

	header(s2, "Production"),
	group("Producers", { "Simon Belov", "Alexander Lavrishko"}),
	group("Associate Producer", { "Maria Pankratova"}),

	header(s2, "Quality Assurance"),
	group("Quality Assurance Project Lead", { "Victoria Bagdasaeva"}),
	group("Quality Assurance Team Lead", { "Aleksandr Pronkin"}),
	group("Quality Assurance Team", { "Ruslan Borzenko", "Andrei Ilin", "Denis Smorodnikov", "Kseniia Anoshkina", "Armen Revunts", "Aleksandr Kozlov", "Roman  Klychkov"}),
}
Credits.drawBlock = function(block)
	local indent = 0

	UiPush()
	if block.blockType == Credits.blockType.header then
		UiTranslateToScreenCenter()
		UiWordWrap(1580)
		UiTextAlignment("center")
		UiAlign("top center")
		if block.blockData.headerType == Credits.headerType.p then
			UiFont("bold.ttf", 85)
			indent = 100 + 85 * Credits.linesCount(block.blockData.text)
		elseif block.blockData.headerType == Credits.headerType.s1 then
			UiFont("bold.ttf", 60)
			indent = 100 + 60 * Credits.linesCount(block.blockData.text)
		elseif block.blockData.headerType == Credits.headerType.s2 then
			UiFont("bold.ttf", 50)
			indent = 60 + 50 * Credits.linesCount(block.blockData.text)
		elseif block.blockData.headerType == Credits.headerType.s3 then
			UiFont("bold.ttf", 40)
			indent = 50 + 40 * Credits.linesCount(block.blockData.text)
		end

		Credits.drawText(block.blockData.text)
	elseif block.blockType == Credits.blockType.group then
		local lineHeight = 50
		UiTranslate(0, lineHeight / 2)
		UiAlign("middle left")
		UiTranslateToScreenCenter()
		UiFont("bold.ttf", 50)
		Credits.drawText(block.blockData.names[1])

		UiPush()
			UiFont("medium.ttf", 37)
			UiGetTextSize(block.blockData.groupLabel)
			UiTranslate(-50, 0)
			UiAlign("middle right")
			Credits.drawText(block.blockData.groupLabel)
		UiPop()

		indent = lineHeight

		for i=2,#block.blockData.names do
			indent = indent + lineHeight + 30
			UiTranslate(0, 30 + lineHeight)
			Credits.drawText(block.blockData.names[i])
		end

		if Credits.nextBlock ~= nil and Credits.nextBlock.blockType == Credits.blockType.header then
			if Credits.nextBlock.blockData.headerType == Credits.headerType.p then
				indent = indent + 150
			else
				indent = indent + 100
			end
		else
			indent = indent + 60
		end
	end

	UiPop()

	return indent
end

local hint_cond = false
local music_playing = false
local prevent_levelchange = false
function PlayCredits()
    if music_playing == false then -- in case of nil, manually play later
        StopMusic()
        PlayMusic(MUSIC_CREDITS)
        music_playing = true
    end
    Credits.draw()

    if Credits.fade < 1 and Credits.fade > 0 and not prevent_levelchange and select(2, UiGetCursorPos()) < (UiHeight()+150) then
        prevent_levelchange = true
    end

    if not hint_cond and Credits.startTime ~= 0 then
        hint_cond = true
    end
    if hint_cond then
	    Credits.drawHint()
    end
    return Credits.finished, prevent_levelchange
end

function ResetCredits(bool)
    Credits.startTime = 0
    music_playing = bool
    ClearKey("savegame.mod.credits")
    ClearKey("savegame.mod.credits_time")
end

function SetMusicPlaying(bool) music_playing = bool end

function GetCreditsTime() return GetTime() - Credits.startTime end

function SetCreditsTime(time) Credits.prepare(time) end
