#include "expansion_sandbox_logic.lua"
#include "../script/helpers/uifr.lua"
#include "ui/options.lua"
#include "ui/ui_extensions.lua"
#include "ui/ui_helpers.lua"

function drawExpansionSandbox(scale)
	local open = true
	UiPush()
		UiModalBegin()
		
		local w = 1400
		local h = 440

		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 = SandboxExpansionView.listState.inputBehaviour
		inputBeh.disabled = false
		inputBeh:update()
		inputBeh.disabled = true
		local prevIdx = SandboxExpansionView.selectedIdx
		if inputBeh.prevRequested then
			SandboxExpansionView.selectedIdx = clamp(SandboxExpansionView.selectedIdx - 1, 1, #SandboxExpansionView.listItems)
		end

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

		if prevIdx ~= SandboxExpansionView.selectedIdx then
			SandboxExpansionView.listState:scrollToItem(SandboxExpansionView.selectedIdx, true)
			SandboxExpansionView.listItems[SandboxExpansionView.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 text = GetTranslatedStringByKey("UI_TEXT_FREE_ROAM_EXP")
			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 SandboxExpansionView.selectedItem and (not SandboxExpansionView.selectedItem.locked) then
					table.insert(icons, 1, { ico="[[menu:menu_accept;iconsize=42,42]]", txt="loc@UI_BUTTON_PLAY" })
				end

				UiDrawHintsCentered(icons)
			UiPop()
		end

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