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

SandboxExpansionView = {}

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

	listState = UiExpCreateListState(true),
	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.items = {}

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

				w = 350,
				h = 200,

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

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

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

					SandboxExpansionView.selectedItem = self
					SandboxExpansionView.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
						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 = SandboxExpansionView.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 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 SandboxExpansionView.alwaysUnlocked)
					self.locked = false

					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
}

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