#include "script/common.lua"
#include "starthub.lua"
#include "missions.lua"
#include "ui/ui_helpers.lua"

gResetScale = 0

function init()
end

function tick(dt)
	SetBool("game.disablepause", true)
	SetBool("hud.disable", true)
	if GetBool("options.audio.menumusic") then
		PlayMusic("MOD/music/menu-loop.ogg")
	end
	if InputPressed("pause") then 
		Menu()
	end
end

local ResetDraw = (function()
	local backgroundHeight = 0
	return function()
		if gResetScale > 0 then
			UiPush()

				UiIgnoreNavigation(gResetScale < 1.0)

				UiTranslate(180, -40)
				UiScale(gResetScale, 1)
				UiColorFilter(1,1,1,gResetScale)
				UiColor(0, 0, 0, 0.75)
				UiAlign("top left")
				local description = GetTranslatedStringByKey("UI_TEXT_THIS_WILL")
				local backgroundOffset = 20
				local descriptionSize, descriptionSizeY =  UiGetTextSize(description);
				local backgroundWidth = descriptionSize + backgroundOffset
				local offset = (backgroundWidth - descriptionSize) / 2 + 10
				UiImageBox("ui/common/box-solid-10.png", backgroundWidth, backgroundHeight + 20, 10, 10)
				----------------------------------------------------------
				UiBeginFrame()
				UiTranslate(offset, 15)
				UiFont("regular.ttf", 22)
				UiColor(1,1,1)
				local completedMissions = 0
				for m,_ in pairs(gArtMissions) do
					if GetBool("savegame.mod."..m..".complete") then completedMissions = completedMissions + 1 end
				end
				if completedMissions > 0 then
					UiText(description.."\n"..GetTranslatedStringByKey("UI_TEXT_NUMBER_OF_MISSIONS,"..completedMissions))
					UiTranslate(0, 24)
				else
					UiText(description)
				end
				local textReset = GetTranslatedStringByKey("UI_BUTTON_RESET");
				local textCancel = GetTranslatedStringByKey("UI_BUTTON_CANCEL");
				local sizeReset = UiGetTextSize(textReset)
				local sizeCancel = UiGetTextSize(textCancel)
				local biggerString = sizeCancel
				if sizeReset > sizeCancel then
					biggerString = sizeReset
				end
				local buttonWidth = biggerString + 39
				local buttonHeight = 34
				local value = 0.25
				local buttonOffset = 20
				local leftOffset = (backgroundWidth - buttonWidth * 2 - buttonOffset) / 2
				UiTranslate(leftOffset - offset, descriptionSizeY)
				UiFont("regular.ttf", 26)
				UiPush()
					UiColorFilter(1, 0.8, 0.8)
					if UiTextButton(textReset, buttonWidth, buttonHeight) then
						ClearKey("savegame.mod")
						SetValue("gResetScale", 0, "cosine", value)
					end
				UiPop()
				UiTranslate(buttonWidth + buttonOffset, 0)
				if UiTextButton(textCancel, buttonWidth, buttonHeight) then
					SetValue("gResetScale", 0, "cosine", value)
				end
				_, backgroundHeight = UiEndFrame()
			UiPop()
		end
			
	end
end)()

function draw()
	UiMakeInteractive()
	-- for art vandals menu we do now want to draw a cursor
	if LastInputDevice() == UI_DEVICE_GAMEPAD then
		UiSetCursorState(UI_CURSOR_HIDE_AND_LOCK)	
	end

	UiButtonHoverColor(1,1,0.5,1)
	
	UiPush()
		UiColor(0,0,0)
		UiRect(UiWidth(), UiHeight())
	UiPop()
	if UiWidth() > 1920 then
		UiTranslate((UiWidth() - 1920) / 2, 0)
		UiWindow(1920, 1080)
	else
		UiWindow(UiWidth(), UiHeight())
	end
	
	UiImage("menu/background.jpg")
	UiPush()
		UiColor(0, 0, 0, 0.75)
		UiRect(UiWidth(), 150)
		UiColor(1, 1, 1)
		UiFont("bold.ttf", 40)
		UiTranslate(UiCenter(), 60)
		UiAlign("center middle")
		UiPush()
			UiScale(2)
			UiText(GetTranslatedStringByKey("UI_TEXT_ART_VANDALS"))
		UiPop()
		UiTranslate(0, 50)
		UiScale(1.08)
		UiText(GetTranslatedStringByKey("UI_TEXT_A_TEARDOWN"))
	UiPop()
	
	UiPush()

		if gResetScale == 1.0 then
			UiIgnoreNavigation()
		end

		UiColor(0, 0, 0, 0.75)
		UiTranslate(20, 170)
		local menuWidth = 326
		local menuHeight = 256
		local margin = 28
		local buttonWidth = menuWidth-2*margin
		UiImageBox("ui/common/box-solid-10.png", menuWidth, menuHeight, 10, 10)
		UiButtonImageBox("ui/common/box-outline-fill-6.png", 6, 6, 0.96, 0.96, 0.96)
		UiColor(0.96, 0.96, 0.96)
		UiAlign("center middle")
		UiTranslate(menuWidth/2, 40)
		UiFont("regular.ttf", 26)
		if UiTextButton("loc@UI_BUTTON_CAMPAIGN", buttonWidth, 40) then
			startHub()
		end
		UiTranslate(0, 48)
		if UiTextButton("loc@UI_BUTTON_SANDBOX", buttonWidth, 40) then
			StartLevel("museum_sandbox", "MOD/tillaggaryd.xml", "sandbox")
		end
		UiTranslate(0, 48)

		UiTranslate(0, 30)

		UiPush()
			if not HasKey("savegame.mod") then
				UiColorFilter(1,1,1,0.5)
				UiDisableInput()
				UiIgnoreNavigation()
			end
			if UiTextButton("loc@UI_BUTTON_RESET_PROGRESS", buttonWidth, 40) then
				if gResetScale == 0 then
					SetValue("gResetScale", 1, "cosine", 0.25)
				else
					SetValue("gResetScale", 0, "cosine", 0.25)
				end
			end
		UiPop()
		ResetDraw()
		UiTranslate(0, 48)
		if UiTextButton("loc@UI_BUTTON_MAIN_MENU", buttonWidth, 40) then
			Menu()
		end
		UiTranslate(0, 48)
	UiPop()

	drawLocalUserTag()
end


