#include "sandbox_logic.lua"
#include "ui/ui_helpers.lua"
#include "ui/ui_extensions.lua"
#include "ui/options.lua"

function drawSandbox(scale)
	local open = true
	UiPush()
		UiModalBegin()
		
		local w = 1400
		local h = 418

		if LastInputDevice() == 2 then
			h = h + 58
		end

		-- override the behaviour of navigation handling for the list
		-- the list should scroll itself even if it's not in focus
		UiIgnoreNavigation()
		local inputBeh = SandboxView.listState.inputBehaviour
		inputBeh.disabled = false
		inputBeh:update()
		inputBeh.disabled = true
		local prevIdx = SandboxView.selectedIdx
		if inputBeh.prevRequested then
			SandboxView.selectedIdx = clamp(SandboxView.selectedIdx - 1, 1, #SandboxView.listItems)
		end

		if inputBeh.nextRequested then
			SandboxView.selectedIdx = clamp(SandboxView.selectedIdx + 1, 1, #SandboxView.listItems)
		end

		if prevIdx ~= SandboxView.selectedIdx then
			SandboxView.listState:scrollToItem(SandboxView.selectedIdx)
			SandboxView.listItems[SandboxView.selectedIdx]:selectSelf()
		end
		
		UiTranslate(UiCenter(), UiMiddle())
		UiScale(scale)
		UiColorFilter(1, 1, 1, scale)
		UiColor(0, 0, 0, 0.5)
		UiAlign("center middle")
		
		UiImageBox("ui/common/box-solid-shadow-50.png", w, h, -50, -50)
		UiWindow(w, h)
		UiAlign("left top")
		UiColor(1,1,1)
		if InputPressed("menu_cancel") or (not UiIsMouseInRect(UiWidth(), UiHeight()) and InputPressed("lmb")) then
			open = false
		end

		UiPush()
			UiFont("bold.ttf", 37)
			UiColor(1,1,1)
			UiAlign("center")
			UiTranslate(UiCenter(), 60)
			UiText("loc@UI_TEXT_SANDBOX")
		UiPop()
		
		UiPush()
			UiTranslate(UiCenter(), 115)
			local margin = 20
			UiWordWrap(w - 2 * margin)
			UiColor(0.8, 0.8, 0.8)
			UiTextAlignment("center")
			UiAlign("center")
			local link = "loc@UI_TEXT_FREE_ROAM_OPTIONS_LINK_NAME"
			local text = GetTranslatedStringByKey("UI_TEXT_FREE_ROAM,"..link)
			UiTextWithFont(text, "regular.ttf", 27)
		UiPop()

		if LastInputDevice() == UI_DEVICE_GAMEPAD then
			UiPush()
				UiTranslate(UiCenter(), UiHeight() - 25)
				UiColor(1, 1, 1, 1)
				
				local icons = 
				{
					{ ico="[[menu:menu_cancel;iconsize=42,42]]", txt="loc@UI_BUTTON_EXIT"}
				}

				if SandboxView.selectedItem and (not SandboxView.selectedItem.locked) then
					table.insert(icons, 1, { ico="[[menu:menu_accept;iconsize=42,42]]", txt="loc@UI_BUTTON_PLAY" })
				end

				UiDrawHintsCentered(icons)

				if InputPressed("interact") then 
					optionsTab = "game"
					SetValue("gOptionsScale", 1.0, "easeout", 0.25)
				end
			UiPop()
		end

		UiPush()
			UiAlign("top left")
			UiTranslate(150, 190)
			SandboxView.alwaysUnlocked = GetInt("options.game.sandbox.unlocklevels") == 1
			SandboxView.listState.scrollSpeed = 100
			UiPush()
				local offset = 98
				UiTranslate(offset/2, 0)
				SandboxView.mouseOver = UiIsMouseInRect(1340-offset, 200)
			UiPop()
			UiListHorizontalWithButtons(1340, 200, SandboxView.listItems, SandboxView.listState)
		UiPop()
		UiModalEnd()
	UiPop()
	return open
end