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

Slide = 
{
    nonserializable = true,

    new = function(self, srcImage)
        local sl = 
        {
            image = srcImage,
            fadeOutT = 1.7,
            t = 0,
        }

        setmetatable(sl, self)
        self.__index = self

        return sl
    end,

    draw = function(self)
        UiPush()
            UiTranslateToScreenCenter()
            UiTranslateToScreenMiddle()

            UiAlign("center middle")

            local dt = GetTimeStep()
            self.t = self.t + dt

            local alpha = math.min(self.t / self.fadeOutT, 1.0)

            UiColor(1, 1, 1, alpha)
            UiScale(1.03 + self.t * 0.01)

            if UiHasImage(self.image) then
                UiImage(self.image)
            end
        UiPop()
    end
}


Slideshow = 
{
    nonserializable = true,
    
    switchPeriod = 6,
    timer = 6,

    images = {},
    curImg = 0,

    slidesQueue = TdList:new(),

    init = function(self)
        local lvls =
        {
            { "hub", true },
            { "lee", isLevelUnlocked("lee") },
            { "marina", isLevelUnlocked("marina") },
            { "mansion", isLevelUnlocked("mansion") },
            { "mall", isLevelUnlocked("mall") },
            { "caveisland", isLevelUnlocked("caveisland") },
            { "frustrum", isLevelUnlocked("frustrum") },
            { "carib", isLevelUnlocked("carib") },
            { "factory", isLevelUnlocked("factory") },
            { "cullington", isLevelUnlocked("cullington") },
            { "tillaggaryd", HasKey("savegame.mod.builtin-artvandals.cinematic.complete") }
        }

        local appendImages = function(imgs, l)
            local i = 1
            while UiHasImage("ui/menu/slideshow/".. l .. i .. ".jpg") do
                local img = "ui/menu/slideshow/".. l .. i .. ".jpg"
                imgs[#imgs + 1] = img
                i = i + 1
            end
        end

        local images = {}
        -- append images from unlocked levels
        tableForEach(
            tableFilter(lvls, function(l) return l[2] end), 
            function(l) appendImages(images, l[1]) end)


        local groupKeys = 
            tableTransform(
                ListKeys("promo.groups"), 
                function(g) return "promo.groups." .. g end)

        local promoImgs = 
            tableTransform(
                tableFlatten(
                    tableTransform(
                        groupKeys, 
                        function(k) 
                            return tableTransform(
                                ListKeys(k .. ".items"), 
                                function(it) 
                                    return k .. ".items." .. it .. ".full_image"
                                end)
                        end)),
                function(item) return GetString(item) end)

        tableForEach(
            tableFilter(promoImgs, function(img) return img ~= "" end), 
            function(img)
                images[#images + 1] = img
            end)

        self.images = tableShuffle(images)

        self.curImg = 1
        self.timer = self.switchPeriod
        self.slidesQueue:pushRight(Slide:new(self.images[self.curImg]))
    end,

    draw = function(self)
        UiPush()
            if self.timer < 0 then
                self.timer = self.switchPeriod

                self.curImg = math.fmod(self.curImg, #self.images) + 1
                self.slidesQueue:pushRight(Slide:new(self.images[self.curImg]))

                if self.slidesQueue:size() > 2 then
                    local removedSlide = self.slidesQueue:popLeft()
                    UiUnloadImage(removedSlide.image)
                end
            end

            self.timer = self.timer - GetTimeStep()

            self.slidesQueue:forEach(function(sl) sl:draw() end)
        UiPop()
    end
}