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

Avatar = {}
Avatar.nonserializable = true

Avatar.GpHint = function(obj)
    local icon = obj.icon
    local hintText = obj.text

    obj.icon = nil
    obj.text = nil

    icon.colorFilter = { 1, 1, 1, 0.7 }

    local view = Ui.HorizontalLayout
    {
        alignment = Ui.V.Middle,

        icon,

        Ui.Text
        {
            id = "hintText",
            font = { "regular.ttf", 22 },
            text = hintText,
            color = { 1, 1, 1, 0.7 }
        }
    }

    return view:new(obj)
end


Avatar.Button = function(obj)
    local ds = Ui.bind().drawState
    local cf = Ui.bind():call(function(it, _)
        if it.disabled then
            return { 0.3, 0.3, 0.3}
        else
            if it.drawState.pressed then
                return { 0.5, 0.5, 0.5 }
            else
                if it.drawState.hovered then
                    return { 1, 1, 0.5 }
                else
                    return { 1, 1, 1 }
                end
            end
        end
    end)

    local b = Ui.Frame
    {
        w = 180,
        h = 40,

        defaultColorFilter = cf,
        colorFilter = cf,

        mainView = {
            margin = 
            { 
                top = ds.pressed:band(2):bor(0), 
                left = ds.pressed:band(2):bor(0)
            }
        },

        Ui.Frame
        {
            w = Ui.bind().parent.w,
            h = Ui.bind().parent.h,

            id = "mainView",

            Ui.Rect
            {
                id = "filling",

                resizeToParent = true,
                color = 0x8FDFA861,
                cornerRadius = 6,
            },

            Ui.RectOutline
            {
                resizeToParent = true,
                color = { 1, 1, 1},
                cornerRadius = 6,
                thickness = 2
            },

            Ui.Text
            {
                id = "label",
                font = { "regular.ttf", 30 },
                vAlign = Ui.V.Middle,
                hAlign = Ui.H.Center,
                color = { 1, 1, 1 },
                uniformHeight = true
            }
        }
    }

    return b:new(obj)
end


Avatar.postUpdateQueue = TdList:new()

Avatar.runOnPostUpdate = function(func, ...)
    Avatar.postUpdateQueue:pushRight({ func = func, fargs = tablePack(...) })
end

Avatar.postUpdate = function()
    if Avatar.postUpdateQueue:isEmpty() then
        return
    end

    while not Avatar.postUpdateQueue:isEmpty() do
        local pending = Avatar.postUpdateQueue:popLeft()
        pending.func(unpack(pending.fargs, 1, pending.fargs.n))
    end
end