#include "common_components.lua"
#include "ui/ui_extensions.lua"
#include "active_mods_logic.lua"


ActiveModsList.currentItemPos = 0
function drawComponentActiveModsItem(index)
    local itemW = 424
    local itemH = 34
	local itemId = UiNavComponent(itemW, itemH)
	local isFocused = UiIsMouseInRect(itemW, itemH) or UiIsComponentInFocus(itemId)
	local isClicked = isFocused and (InputPressed("menu_accept") or InputPressed("lmb"))
	local isGamepad = LastInputDevice() == UI_DEVICE_GAMEPAD

	if isClicked or (isGamepad and isFocused) then
		if ActiveModsList.currentModIndex ~= index then
			UiSound("common/click.ogg")
			ActiveModsList.currentModIndex = index
		end
	end
    UiPush()
        UiColorFilter(1.0, 1.0, 1.0, 1)
		if ActiveModsList.currentModIndex == index or isFocused then
			UiPush()
				UiColor(1, 1, 1, 1)
				UiImageBox("common/rect_c#d9d9d9_o0.12_cr8.png", itemW, itemH, 9, 9)
			UiPop()
		end
		if ActiveModsList.currentModIndex == index then
			local _, y = UiGetCursorPos()
			ActiveModsList.currentItemPos = y
		end
        UiPush()
            UiTranslate(12, 6)
			if ActiveModsList.currentModIndex == index then
				UiColor(1, 1, 0.5, 1)
			else
                UiColor(0.53, 0.53, 0.53, 1)
			end
            UiFont("regular.ttf", 29)
            UiText(ActiveModsList.mods[index].name)
        UiPop()
    UiPop()
end

ActiveModsList.backGround = 
{
    w = 12.0,
    h = 716.0,

    draw = function(self)
        UiPush()
            UiColor(1, 1, 1, 1)
            UiImageBox("common/rect_c#525252_o1.00_cr4.png", self.w, self.h, 4, 4)
        UiPop()
    end,
}
ActiveModsList.thumb = 
{
    w = 8.0,
    h = 184.0,

    draw = function(self)
        UiPush()
            UiTranslate(2.0, 2.0)
            UiColor(1, 1, 1, 1)
            UiImageBox("common/rect_c#ffffff_o1.00_cr3.png", self.w, self.h, 3, 3)
        UiPop()
    end,
}
ActiveModsList.scrollBar = UiCreateScrollBar(ActiveModsList.thumb, ActiveModsList.backGround)

function drawComponentActiveModsFrame()
    UiPush()
        UiColorFilter(1.0, 1.0, 1.0, 1)
        UiPush()
            UiTranslate(0.0, 81.0)
			UiPush()
				UiList(424.0, 716.0, ActiveModsList.listItems, ActiveModsList.scrollBar)
            UiPop()
			UiTranslate(0, 751)
            UiColor(0.8, 0.8, 0.8, 0.8)
			if GetString("game.input.curdevice") == "gamepad" then
				UiTranslate(484/2 - 30, 0)
				local hintBack = {{ ico = "[[menu:menu_cancel;iconsize=42,42]]", txt = "loc@UI_BACK" }}
				UiDrawHintsCentered(hintBack)
			end
        UiPop()
    UiPop()
end

function drawDescription()
	UiPush()
            UiFont("regular.ttf", 27)
			UiWordWrap(380)
			local currentMod = ActiveModsList.mods[ActiveModsList.currentModIndex]
			local _, th = UiGetTextSize(currentMod.tags)
			local _, h = UiGetTextSize(currentMod.description)
			local height = math.max(154, 10 + th + 20 + h + 10)

			UiTranslate(494, ActiveModsList.currentItemPos-96)

            UiImageBox("common/rect_c#000_o0.55_cr8.png", 400, height, 8, 8)
			UiAlign("top left")
            UiTranslate(10, 10)
			UiText(GetTranslatedStringByKey("UI_TEXT_TAGS").." "..currentMod.tags)
			UiTranslate(0, th + 20)
            UiText(currentMod.description)
        UiPop()
end

function drawComponentActiveModsWindow(p0)
	UiBlur(1)
    UiTranslate(UiCenter() - 484 / 2, UiMiddle() - 890 / 2)
    UiPush()
        UiColorFilter(1.0, 1.0, 1.0, 1)

        UiPush()
			UiColor(1, 1, 1, 1)
			UiImageBox("common/rect_c#000_o0.55_cr8.png", 484, 890, 8, 8)
		UiPop()

		if ActiveModsList.currentItemPos > 190 and ActiveModsList.currentItemPos < 872 then
			drawDescription()
		end

        UiPush()
			local textActiveMods = GetTranslatedStringByKey(p0)
            UiFont("bold.ttf", 36)
			UiTranslate(484 / 2 - UiGetTextSize(textActiveMods) / 2, 60)
            UiColor(1.0, 1.0, 1.0, 1)
            UiText(textActiveMods)
        UiPop()
        UiTranslate(30.0, 15.0)
        drawComponentActiveModsFrame()
    UiPop()
end

function makeActiveModsListItems()
	for i=1, #ActiveModsList.mods do
		table.insert(ActiveModsList.listItems,
			{
				w = 424.0,
				h = 45.0,

				draw = function(self)
					drawComponentActiveModsItem(i)
				end,
			}
		)
	end
end

function hideCursor()
	if LastInputDevice() == UI_DEVICE_GAMEPAD then
        UiSetCursorState(UI_CURSOR_HIDE_AND_LOCK)
    end
end

function drawActiveMods()
	hideCursor()
	ActiveModsList.refresh()
	makeActiveModsListItems()
    drawComponentActiveModsWindow("UI_TEXT_ACTIVE_MODS_UPPERCASE")
end