#include utility.js
#include ui/elems.js
#include ui/input-menus.js

Umi.UI.InputMenus = (function() {
    const ids = [];
    let current = '';

    const inputMenuActive = 'input__menu--active',
        inputButtonActive = 'input__button--active';

    const toggle = function(baseId) {
        const button = Umi.UI.Elements.MessageMenus.id + '-btn-' + baseId,
            menu = Umi.UI.Elements.MessageMenus.id + '-sub-' + baseId;

        if($c(inputMenuActive).length)
            $c(inputMenuActive)[0].classList.remove(inputMenuActive);

        if($c(inputButtonActive).length)
            $c(inputButtonActive)[0].classList.remove(inputButtonActive);

        if(current !== baseId) {
            $i(menu).classList.add(inputMenuActive);
            $i(button).classList.add(inputButtonActive);
            current = baseId;
        } else current = '';
    };

    const createButton = function(id, title, onClick) {
        return $e({
            tag: 'button',
            attrs: {
                id: Umi.UI.Elements.MessageMenus.id + '-btn-' + id,
                classList: ['input__button', 'input__button--' + id],
                title: title,
                onclick: onClick || (function() {
                    toggle(id);
                }),
            },
        });
    };

    return {
        Add: function(baseId, title) {
            if(ids.indexOf(baseId) < 0) {
                ids.push(baseId);
                Umi.UI.Elements.MessageContainer.insertBefore(createButton(baseId, title), Umi.UI.Elements.MessageSend);
                Umi.UI.Elements.MessageMenus.appendChild(
                    $e({ attrs: { 'class': ['input__menu', 'input__menu--' + baseId], id: Umi.UI.Elements.MessageMenus.id + '-sub-' + baseId } })
                );
            }
        },
        AddButton: function(baseId, title, onClick) {
            if(ids.indexOf(baseId) < 0) {
                ids.push(baseId);
                Umi.UI.Elements.MessageContainer.insertBefore(
                    createButton(baseId, title, onClick),
                    Umi.UI.Elements.MessageSend
                );
            }
        },
        Get: function(baseId, button) {
            const id = Umi.UI.Elements.MessageMenus.id + '-' + (button ? 'btn' : 'sub') + '-' + baseId;
            if(ids.indexOf(baseId) >= 0)
                return $i(id);
            return null;
        },
        Remove: function(baseId) {
            $ri(Umi.UI.Elements.MessageMenus.id + '-btn-' + baseId);
            $ri(Umi.UI.Elements.MessageMenus.id + '-sub-' + baseId);
        },
        Toggle: toggle,
        CreateButton: createButton,
    };
})();
