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



function drawExpansions(scale)
    local open = true
    UiPush()
        UiModalBegin()
        local contentWidth = ExpansionsView.listState.contentSpan
        
        local w = 1400
        local h = contentWidth < (w -60) and 418 or 460

        if LastInputDevice() == UI_DEVICE_GAMEPAD then
            h = h + 58
        end

        -- override the behaviour of navigation handling for the list
        -- the list should scroll itself even if it's not in focus
        UiIgnoreNavigation()
        local inputBeh = ExpansionsView.listState.inputBehaviour
        inputBeh.disabled = false
        inputBeh:update()
        inputBeh.disabled = true
        local prevIdx = ExpansionsView.selectedIdx
        local newIdx = prevIdx
        if inputBeh.prevRequested then
            newIdx = clamp(prevIdx - 1, 1, #ExpansionsView.listItems)
        end

        if inputBeh.nextRequested then
            newIdx = clamp(prevIdx + 1, 1, #ExpansionsView.listItems)
        end
        inputBeh:clearRequests()

        -- we can't loose focused element while using gamepad
        if LastInputDevice() == UI_DEVICE_GAMEPAD then
            if ExpansionsView.selectedItem == nil then
                ExpansionsView:selectItem(1)
            end
        end

        if prevIdx ~= newIdx then
            ExpansionsView:selectItem(newIdx)
        end
        
        UiTranslate(UiCenter(), UiMiddle())
        UiScale(scale)
        UiColorFilter(1, 1, 1, scale)
        UiColor(0, 0, 0, 0.5)
        UiAlign("center middle")
        
        UiImageBox("common/box-solid-shadow-50.png", w, h, -50, -50)
        UiWindow(w, h)
        UiAlign("left top")
        UiColor(1,1,1)
        if InputPressed("menu_cancel") or (not UiIsMouseInRect(UiWidth(), UiHeight()) and InputPressed("lmb")) then
            open = false
        end

        UiPush()
            UiFont("bold.ttf", 37)
            UiColor(1,1,1)
            UiAlign("center")
            UiTranslate(UiCenter(), 60)
            UiText("loc@UI_TEXT_EXPANSIONS")
        UiPop()
        
        UiPush()
            UiTranslate(UiCenter(), 115)
            local margin = 20
            UiWordWrap(w - 2 * margin)
            UiColor(0.8, 0.8, 0.8)
            UiTextAlignment("center")
            UiAlign("center")
            UiTextWithFont("loc@UI_TEXT_EXPANSIONS_ARE", "regular.ttf", 27)
        UiPop()

        if LastInputDevice() == UI_DEVICE_GAMEPAD then
            UiPush()
                UiTranslate(UiCenter(), UiHeight() - 25)
                UiColor(1, 1, 1, 1)

                local icons = 
                {
                    { ico="[[menu:menu_cancel;iconsize=42,42]]", txt="loc@UI_BUTTON_EXIT"}
                }

                if ExpansionsView.selectedItem.isInstalled then
                    table.insert(icons, 1, { ico="[[menu:menu_accept;iconsize=42,42]]", txt="loc@UI_BUTTON_PLAY" })
                else
                    table.insert(icons, 1, { ico="[[menu:menu_accept;iconsize=42,42]]", txt="loc@UI_STORE" })
                end

                UiDrawHintsCentered(icons)
            UiPop()
        end

        UiPush()
            local offset = 30
            ExpansionsView.width = ExpansionsView.listState.background.w
            if #ExpansionsView.listItems < 4 then
                offset = (w - contentWidth) / 2
                ExpansionsView.width = contentWidth
            end

            UiAlign("top left")
            UiTranslate(offset, 190)
            ExpansionsView.alwaysUnlocked = GetInt("options.game.sandbox.unlocklevels") == 1
            UiPush()
				offset = 98*(#ExpansionsView.listItems < 4 and 0 or 1)
				UiTranslate(offset/2, 0)
				ExpansionsView.mouseOver = UiIsMouseInRect(ExpansionsView.width-offset, 200)
			UiPop()
            UiListHorizontalWithButtons(ExpansionsView.width, 200, ExpansionsView.listItems, ExpansionsView.listState)
        UiPop()
        UiModalEnd()
    UiPop()
    return open
end
