
/** @type Config */
var CONFIG;

/** @constructor */
function Config(configData) {
    this.deviceId = null;

    /** @type {Object.<string, Object<string, string>>} serviceType => { serviceId => serviceName } */
    this.services = {};
    /** @type {string|undefined} */
    this.emojiProvider;
    /** @type{boolean|undefined} */
    this.displayAvatar;
    /** @type{boolean|undefined} */
    this.colorfulNames;
    /** @type{boolean|undefined} */
    this.scrollAvatars;

    // Load global configurations
    for (var i =0, nbConfig = configData.length; i < nbConfig; i++)
        if (configData[i]["service"] === null && configData[i]["device"] === null)
            this.mergeConfig(JSON.parse(configData[i]["config"]));
}

Config.prototype.mergeConfig = function(configData) {
    if (configData["services"])
        for (var i in configData["services"]) {
            this.services[i] = configData["services"][i];
        }
    if (configData["emojiProvider"] !== undefined)
        this.emojiProvider = configData["emojiProvider"];
    if (configData["displayAvatar"] !== undefined)
        this.displayAvatar = configData["displayAvatar"];
    if (configData["colorfulNames"] !== undefined)
        this.colorfulNames = configData["colorfulNames"];
    if (configData["scrollAvatars"] !== undefined)
        this.scrollAvatars = configData["scrollAvatars"];
};

/** @return {string|undefined} */
Config.prototype.getEmojiProvider = function() {
    return this.emojiProvider;
};

Config.prototype.isDisplayAvatars = function() {
    return this.displayAvatar !== false;
};

Config.prototype.isScrollAvatars = function() {
    return this.scrollAvatars !== false;
};

Config.prototype.isColorfulNames = function() {
    return this.colorfulNames === true;
};

Config.prototype.commitNewSettings = function(newSettings) {
    for (var i in newSettings) {
        // Just to check not empty object
        // TODO only global config atm
        this.mergeConfig(newSettings);
        onConfigUpdated();
        HttpRequestWrapper(HttpRequestMethod.POST, "api/settings?service=null&device=null").send(JSON.stringify(newSettings));
        return;
    }
};

Config.prototype.compileCSS = function() {
    var rules = {},
        css = '';

    if (!this.isDisplayAvatars()) {
        rules[".chatsystem-content .chatmsg-authorGroup .chatmsg-author-img-wrapper"] = ["display: none"];
        rules[".chatsystem-content .chatmsg-authorGroup"] = ["display: flex"];
        rules[".chatsystem-content .chatmsg-authorGroup .chatmsg-author"] = ["position:initial", "vertical-align:top", "min-width:75px"];
        rules[".chatsystem-content .chatmsg-authorGroup .chatmsg-author-messages"] = ["padding-top:0", "padding-left:0", "display:inline-block", "margin-top:-4px", "flex:1"];
        rules[".chatmsg-author-name::before"] = [ "content:'<'" ];
        rules[".chatmsg-author-name::after"] = [ "content:'>'" ];
    }
    if (!this.isColorfulNames()) {
        rules[".chatmsg-authorGroup .chatmsg-item .chatmsg-link-user, .chatsystem-content a.chatmsg-author-name, .chatmsg-author-name"] = ["background-color: transparent !important;"];
    }
    for (var i in rules) {
        css += i +'{';
        rules[i].forEach(function(rule) {
            css += rule +';';
        });
        css += '}';
    }
    return css;
};

CONFIG = new Config([]);

