#include settings.js
#include themes.js
#include ui/elems.js

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

    const getPosition = function(end) {
        return Umi.UI.Elements.MessageInput[end ? 'selectionEnd' : 'selectionStart'];
    };
    const setPosition = function(pos, end) {
        Umi.UI.Elements.MessageInput[end ? 'selectionEnd' : 'selectionStart'] = pos;
    };

    const getText = function() {
        return Umi.UI.Elements.MessageInput.value;
    };
    const setText = function(text) {
        Umi.UI.Elements.MessageInput.value = text;
    };

    return {
        AccentColours: accentColours,
        AccentReload: function() {
            const available = Object.keys(accentColours),
                name = Umi.Settings.get('style'),
                compact = 'chat--compact',
                classes = ['umi'];

            if(available.indexOf(name) < 0)
                return;

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

            if(!Umi.Settings.get('tmpDisableOldThemeSys'))
                classes.push('umi--' + name);

            if(Umi.UI.Elements.Chat.className.indexOf('hidden') >= 0)
                classes.push('hidden');

            Umi.UI.Elements.Chat.className = '';
            Umi.UI.Elements.Chat.classList.add.apply(Umi.UI.Elements.Chat.classList, classes);
            if(Umi.Settings.get('compactView')) {
                if(Umi.UI.Elements.Chat.className.indexOf(compact) < 0)
                    Umi.UI.Elements.Messages.classList.add(compact);
            } else
                Umi.UI.Elements.Messages.classList.remove(compact);

            if(Umi.Settings.get('autoScroll'))
                Umi.UI.Elements.Messages.scrollTop = Umi.UI.Elements.Messages.scrollHeight;
        },
        Focus: function() {
            Umi.UI.Elements.MessageInput.focus();
        },
        SetPosition: setPosition,
        GetPosition: getPosition,
        GoToStart: function() {
            setPosition(0);
        },
        GoToEnd: function() {
            setPosition(Umi.UI.Elements.MessageInput.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,
    };
})();
