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

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

		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
        local newIdx = prevIdx
		if inputBeh.prevRequested then
			newIdx = clamp(prevIdx - 1, 1, #SandboxView.listItems)
		end

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

        -- we can't loose focused element while using gamepad
        if LastInputDevice() == UI_DEVICE_GAMEPAD then
            if SandboxView.selectedItem == nil then
                SandboxView:selectItem(1)
            end
        end

		if prevIdx ~= newIdx then
            SandboxView:selectItem(newIdx)
		end
		
		UiTranslate(UiCenter(), UiMiddle())
		UiScale(scale)
		UiColorFilter(1, 1, 1, scale)
		UiColor(0, 0, 0, 0.5)
		UiAlign("center middle")
		
		UiImageBox("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 = "[[game://options/;label=loc@UI_TEXT_FREE_ROAM_OPTIONS_LINK_NAME;id=game;color=#FFFF00;]]"
			local text = GetTranslatedStringByKey("UI_TEXT_FREE_ROAM,"..link)
			local _,_,linkid = UiTextWithFont(text, "regular.ttf", 27)
			if linkid == "game" then
				Options.selectedTab = linkid
				SetValue("gOptionsScale", 1.0, "easeout", 0.25)
			end
		UiPop()

		if LastInputDevice() == UI_DEVICE_GAMEPAD then
			UiPush()
				UiTranslate(UiCenter(), UiHeight() - 25)
				UiColor(1, 1, 1, 1)

				local icons = 
				{
					{ ico="[[menu:interact;iconsize=42,42]]", txt="loc@UI_OPEN_OPTIONS" },
					{ 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 
					Options.selectedTab = "game"
					SetValue("gOptionsScale", 1.0, "easeout", 0.25)
				end
			UiPop()
		end

		UiPush()
			UiAlign("top left")
			UiTranslate(30, 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) and SandboxView.listState.scrollTouchId == 0
			UiPop()
			UiListHorizontalWithButtons(1340, 200, SandboxView.listItems, SandboxView.listState)
		UiPop()
		UiModalEnd()
	UiPop()
	return open
end