#include "../../game.lua"

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

	listState = nil,
	listItems = {},

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

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

		if LastInputDevice() == UI_DEVICE_GAMEPAD then
			self.selectedItem = self.listItems[1]
			self.selectedItem.selected = true
		end

		InputClear()
	end,

	init = function(self)
		self.listState = UiCreateListState(true)
		self.listState.itemsPadding = 20
		self.listState.background.w = 1340
		self.listState.thumb.w = 300
		self.listState.scrollbarOffset = 30
		self.listState.allowListDrag = false
		self.listState.ignoreWheelScroll = true

		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,

				selectSelf = function(self)
					if SandboxView.selectedItem ~= nil and self.id == SandboxView.selectedItem.id then
						return
					end

					UiSound("common/click.ogg")
					if SandboxView.selectedItem ~= nil then
						SandboxView.selectedItem.selected = false
					end

					SandboxView.selectedItem = self
					SandboxView.selectedItem.selected = true
				end,

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

					UiImageBox("ui/common/item_preview_gradient.png", self.w, self.h, 8, 0)
	                if self.selected then
	                	UiImageBox("ui/common/item_preview_gradient_frame.png", self.w, self.h, 10, 10)
	                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("ui/common/item_preview_gradient.png", self.w, self.h, 8, 0)
	                if isFocused then
						UiImageBox("ui/common/item_preview_gradient_frame.png", self.w, self.h, 10, 10)
	                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("ui/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
}