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

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

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

    const toggle = function(baseId) {
        const button = 'umi-msg-menu-btn-' + baseId,
            menu = 'umi-msg-menu-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: {
                type: 'button',
                id: 'umi-msg-menu-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);
                $i('umi-msg-container').insertBefore(createButton(baseId, title), $i('umi-msg-send'));
                $i('umi-msg-menu').appendChild(
                    $e({ attrs: { 'class': ['input__menu', 'input__menu--' + baseId], id: 'umi-msg-menu-sub-' + baseId } })
                );
            }
        },
        AddButton: function(baseId, title, onClick) {
            if(ids.indexOf(baseId) < 0) {
                ids.push(baseId);
                $i('umi-msg-container').insertBefore(createButton(baseId, title, onClick), $i('umi-msg-send'));
            }
        },
        Get: function(baseId, button) {
            const id = 'umi-msg-menu-' + (button ? 'btn' : 'sub') + '-' + baseId;
            if(ids.indexOf(baseId) >= 0)
                return $i(id);
            return null;
        },
        Remove: function(baseId) {
            $ri('umi-msg-menu-btn-' + baseId);
            $ri('umi-msg-menu-sub-' + baseId);
        },
        Toggle: toggle,
        CreateButton: createButton,
    };
})();
