#include themes.js
#include utility.js

Umi.UI.View = (function() {
    const accentColours = {
        'dark': 'Dark',
        'light': 'Light',
        'blue': 'Blue',
        'purple': 'Purple',
        'archaic': 'Archaic'
    };

    const getPosition = function(end) {
        return $i('umi-msg-text')[end ? 'selectionEnd' : 'selectionStart'];
    };
    const setPosition = function(pos, end) {
        $i('umi-msg-text')[end ? 'selectionEnd' : 'selectionStart'] = pos;
    };

    const getText = function() {
        return $i('umi-msg-text').value;
    };
    const setText = function(text) {
        $i('umi-msg-text').value = text;
    };

    return {
        AccentColours: accentColours,
        AccentReload: function() {
            const style = mami.settings.get('style');

            if(!accentColours.hasOwnProperty(style))
                return;

            // should probably be moved elsewhere eventually
            // the entire AccentReload function should probably be axed
            UmiThemeApply(style);

            const chat = $i('umi-chat');
            if(chat instanceof Element) {
                const compactView = mami.settings.get('compactView');
                chat.classList.toggle('chat--compact', compactView);
                $i('umi-messages').classList.toggle('chat--compact', compactView);

                const forceOldOff = mami.settings.get('tmpDisableOldThemeSys');
                for(const name in accentColours)
                    if(accentColours.hasOwnProperty(name))
                        chat.classList.toggle(`umi--${name}`, !forceOldOff && name === style);
            }
        },
        Focus: function() {
            $i('umi-msg-text').focus();
        },
        SetPosition: setPosition,
        GetPosition: getPosition,
        GoToStart: function() {
            setPosition(0);
        },
        GoToEnd: function() {
            setPosition($i('umi-msg-text').value.length);
        },
        GetSelectionLength: function() {
            let length = getPosition(true) - getPosition();
            if(length < 0)
                length = getPosition() - getPosition(true);
            return length;
        },
        EnterAtCursor: function(text, overwrite) {
            const value = getText(),
                current = getPosition();
            let out = '';

            out += value.slice(0, current);
            out += text;
            out += value.slice(current + (overwrite ? text.length : 0));

            setText(out);
            setPosition(current);
        },
        GetSelectedText: function() {
            return getText().slice(getPosition(), getPosition(true));
        },
        GetText: getText,
        SetText: setText,
    };
})();
