#include "game.lua"
#include "ui/ui_extensions.lua"
#include "ui/ui_helpers.lua"

SandboxView = {}

SandboxView =
{
	selectedItem = nil,
	selectedIdx = 0,
	alwaysUnlocked = false,

	listState = UiCreateListState(true),
	listItems = {},

	reset = function(self)
		if self.selectedItem ~= nil then
			self.selectedItem.selected = false
			self.selectedItem = nil
		end

		self.selectedIdx = 0
		self.listState:reset()

		if LastInputDevice() == UI_DEVICE_GAMEPAD then
            self:selectItem(1)
        end

		InputClear()
	end,

	init = function(self)
		self.items = {}

		for i=1, #gSandbox do
			local item =
			{
				id = i,
				level = gSandbox[i].level,
				image = gSandbox[i].image,
				name = gSandbox[i].name,
				selected = false,
				locked = false,

				w = 350,
				h = 200,

				startLevel = function(self)
					for i=1, #gSandbox do
						if self.level == gSandbox[i].level then
							StartLevel(gSandbox[i].id, gSandbox[i].file, gSandbox[i].layers)
						end
					end
				end,

				drawForGamepad = function(self)
					UiNavComponent(self.w, self.h)

					UiImageBox("common/item_preview_gradient.png", self.w, self.h, 8, 0)
	                if self.selected then
						UiPush()
							UiColor(1, 1, 0.5, 1)
							UiRoundedRectOutline(self.w, self.h, 6, 3)
						UiPop()
	                end 

	                if self.selected and not self.locked and InputPressed("menu_accept") then
	                	self:startLevel()
	                end  
				end,

				drawForNotGamepad = function(self)
					UiNavComponent(self.w, self.h)
	                local isFocused = SandboxView.mouseOver and UiIsMouseInRect(self.w, self.h)
	                local isClicked = isFocused and InputReleased("lmb")

					UiImageBox("common/item_preview_gradient.png", self.w, self.h, 8, 0)
	                if isFocused or self.selected then
						UiPush()
							UiColor(1, 1, 0.5, 1)
							UiRoundedRectOutline(self.w, self.h, 6, 3)
						UiPop()
	                end

					if isClicked and (not self.locked) then
						self:startLevel()
					end
				end,

				draw = function(self)
					self.locked = not (isLevelUnlocked(self.level) or SandboxView.alwaysUnlocked)

					-- Carib hub is a special case since it doesn't contain any missions to check against, so unlocking once the travel email is recieved
					if self.level == "hub_carib" and GetInt("savegame.message.carib_travel") > 0 then
						self.locked = false
					end

					UiPush()
						if self.locked then
							UiColorFilter(.5, .5, .5)
						else
							UiColorFilter(1, 1, 1 , 1)
						end

						UiColor(1, 1, 1, 1)
						UiImage(self.image)

						if LastInputDevice() == UI_DEVICE_GAMEPAD then
							self:drawForGamepad()
						else
							self:drawForNotGamepad()
						end
					UiPop()

					if self.locked then
						UiPush()
							UiTranslate(self.w / 2, self.h / 2)
							UiAlign("center middle")
							UiColor(1, 1, 1, 0.8)
							UiImageBox("menu/lock.png", 42, 42, 0, 0)
						UiPop()
					end

					UiPush()
						UiFont("bold.ttf", 27)
						UiTranslate(10, 163 + UiFontHeight())
						UiColor(0.8, 0.8, 0.8, 0.8)
						UiWordWrap(self.w - 10)
						UiAlign("bottom")
						UiText(self.name)
					UiPop()
				end
			}

			table.insert(self.listItems, item)
		end

		self:reset()
	end,

    selectItem = function(self, idx)
        if self.selectedItem ~= nil and self.listItems[idx].id == self.selectedItem.id then
            return
        end

		if self.selectedIdx > 0 then
			UiSound("common/click.ogg")
		end

        self.listState:scrollToItem(idx, true)

        if self.selectedItem ~= nil then
            self.selectedItem.selected = false
        end

        self.selectedItem = self.listItems[idx]
        self.selectedItem.selected = true
        self.selectedIdx = idx
    end
}

SandboxView.listState.itemsPadding = 20
SandboxView.listState.background.w = 1340
SandboxView.listState.thumb.w = 300
SandboxView.listState.scrollbarOffset = 30
SandboxView.listState.allowListDrag = false