#include "ui/ui_extensions.lua"
#include "ui/ui_helpers.lua"
#include "script/player_characters.lua"
#include "ui/helpers/containers.lua"

AvatarUnlockedPopUpView = {}
AvatarUnlockedPopUpView = Ui.Window
{
    w = Ui.bind():fcall(math.max, Ui.bind().charName.w, 250) + Ui.bind():fcall(IsRunningOnPC):band(170):bor(210),
    h = Ui.bind():fcall(IsRunningOnPC):band(150):bor(180),
    interactable = false,

    animator = 
    { 
        playTime = 0.3,

        onAnimationUpdate = function(self, p)
            local f = (p < 0.5) and (16 * math.pow(p, 5)) or (1 - math.pow(-2 * p + 2, 5) / 2)

            UiTranslateToScreenTopEdge(0, 60)
            UiTranslateToScreenRightEdge(-f * (self.parent.w + 60), 0)
            UiWindow(self.parent.w, self.parent.h, self.parent.clip)
            UiAlign("top left")
            UiTranslateToWindowTopLeft()
        end
    },

    Ui.Frame
    {
        id = "rootFrame",
        resizeToParent = true,

        Ui.Rect
        {   
            resizeToParent = true,
            color = { 0, 0, 0, 0.55 },
            cornerRadius = 8
        },

        Ui.HorizontalLayout
        {
            padding = 10,
            margin = { left = 10, top = 10 },

            Ui.Rect
            {
                id = "charPreview",
                w = Ui.bind().root.rootFrame.h - 20,
                h = Ui.bind().root.rootFrame.h - 20,
                cornerRadius = 8,
                fillImagePath = Ui.bind().viewModel.charPreviewPath
            },

            Ui.VerticalLayout
            {
                margin = { top = 5 },

                Ui.Frame
                {
                    adaptSize = Ui.Adapt.Width,
                    h = 40,

                    Ui.Text
                    {
                        id = "charName",
                        font = Ui.bind():fcall(IsRunningOnPC):band({ "bold.ttf", 36 }):bor({ "bold.ttf", 38 }),
                        color = 0xFFFF7F,
                        text = Ui.bind().viewModel.charName,
                       
                    }
                },

                Ui.Frame
                {
                    w = 280,
                    h = Ui.bind():fcall(IsRunningOnPC):band(80):bor(110),

                    Ui.Text
                    {
                        vAlign = Ui.V.Middle,

                        font = { "medium.ttf", 22 },
                        text = "loc@UI_TEXT_UNLOCKED_CHARACTER",
                        wrapWidth = 260
                    }
                }
            }
        }
    }
}

AvatarUnlockedPopUpViewModel = 
{
    charPreviewPath = "../level/menu/script/avatarui/resources/preview_tracy.png",
    charName = "Tracy LALAL",

    showTime = 0,
    totalShowDuration = 3,
    currentShowDuration = 0,

    onShow = function(self, view)
        self.showTime = GetTime()
        self.currentShowDuration = 0
    end,

    onLogicUpdate = function(self, view)
        self.currentShowDuration = self.currentShowDuration + GetTimeStep()

        if self.currentShowDuration > self.totalShowDuration then
            view:hide()
        end
    end,

    onCharUnlocked = function(self, charId)
        local charInfo = gPlayerCharacters[charId]
        self.charPreviewPath = "../level/menu/script/" .. charInfo.preview
        self.charName = charInfo.name
        self.view:show()
    end
}

AvatarUnlockedPopUp = Ui.resolveMVVM("AvatarUnlockedPopUp")

RegisterListenerTo("CharUnlocked", "onCharUnlocked")

-- create queue because events could trigger when UI part of script environment
-- has not been loaded yet
AvatarUnlockedPopUp.unlockQueue = TdList:new()
AvatarUnlockedPopUp.unlockMonitor = Ui.Object:new
{
    draw = function(self)
        if AvatarUnlockedPopUp.unlockQueue:isEmpty() then
            return
        end

        if AvatarUnlockedPopUp.isClosed then
            local id = AvatarUnlockedPopUp.unlockQueue:popLeft()
            AvatarUnlockedPopUp.viewModel:onCharUnlocked(id)
        end
    end,

    onCreate = function(self)
        UiAddDrawObject(self)
    end
}

function onCharUnlocked(charId)
    AvatarUnlockedPopUp.unlockQueue:pushRight(charId)
end

