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

ExpansionsStatus =
{
    DLC_NOT_INSTALLED = 0,
    DLC_INSTALLATION_PAUSED = 1,
    DLC_INSTALLATION_IN_PROGRESS = 2,
    DLC_INSTALLED = 3
}

ExpansionsView = {}
ExpansionsView =
{
	selectedItem = nil,
	selectedIdx = 0,
	alwaysUnlocked = false,

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

	createItem = function(self, dlcInfo)
		local isInstalled = dlcInfo.isEmbedded == true or 
			GetInt("dlc.available." .. dlcInfo.dlcName or "") == ExpansionsStatus.DLC_INSTALLED

		local item =
		{
			level = dlcInfo.level,
			image = dlcInfo.image,
			name = dlcInfo.name,
			dlcName = dlcInfo.dlcName,
			isEmbedded = dlcInfo.isEmbedded,

			isInstalled = isInstalled,

			selected = false,

			w = 350,
			h = 200,

			startLevel = function(self)
				Command("mods.play", self.level)
			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
                	UiImageBox("common/item_preview_gradient_frame.png", self.w, self.h, 10, 10)
                end

                if self.selected and InputPressed("menu_accept") then
                	if self.isInstalled then
                		self:startLevel()
                	else
                		Command("dlc.openstore", self.dlcName)
                	end
                end
			end,

			drawForNotGamepad = function(self)
				UiNavComponent(self.w, self.h)
				
                local mouseX, mouseY = UiGetCanvasMousePos()
                local isFocused = UiIsMouseInRect(self.w, self.h) and UiIsInClipRegion(mouseX, mouseY)
                local isClicked = isFocused and InputPressed("lmb")

                UiImageBox("common/item_preview_gradient.png", self.w, self.h, 8, 0)
                if self.selected or isFocused then
                	UiImageBox("common/item_preview_gradient_frame.png", self.w, self.h, 10, 10)
                end

				if isClicked then
					if self.isInstalled then
                		self:startLevel()
                	else
                		Command("dlc.openstore", self.dlcName)
                	end
				end
			end,

			draw = function(self)
				if not self.isEmbedded then
					self.isInstalled = GetInt("dlc.available." .. dlcInfo.dlcName or "") == ExpansionsStatus.DLC_INSTALLED
				end

				UiPush()
					if not self.isInstalled then
						UiColorFilter(.2, .2, .2)
					else
						UiColorFilter(1, 1, 1, 1)
					end

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

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

				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
		}

		return item
	end,

	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)
		if not (IsRunningOnPlaystation() or IsRunningOnXbox()) then
            Command("mods.refresh")
        end

		self.listItems = {}

		for i, dlcInfo in ipairs(gExpansions) do
			local item = self:createItem(dlcInfo)
            item.id = i
			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
}


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