/// <reference path="~/web1/lib1/innovaphone.lib1.js" />

var innovaphone = innovaphone || {};

innovaphone.Avatar = innovaphone.Avatar || function (start, user, domain, appdomain) {
    var api = start.consumeApi("com.innovaphone.avatar"),
        base = null,
        token = null,
        salt = null,
        providers = [];
    var clientApi = start.consumeApi("com.innovaphone.client");

    if (!appdomain) appdomain = domain;
    api.onupdate.attach(init);
    init();

    function init() {
        providers = [];
        for (var i = 0; i < api.providers.length; i++) {
            var model = api.model[api.providers[i]];
            providers.push({ base: model.url.slice(0, model.url.lastIndexOf("/") + 1) + model.info.filename, domain: model.info.domain, token: model.info.token, salt: innovaphone.crypto.randomString(16) });
        }
    }

    this.url = function (sip, size, dn) {
        if (!sip) sip = "";
        else if (sip == undefined) sip = "";
        var at = sip.indexOf("@");
        var d = appdomain;
        if (at != -1) {
            d = sip.slice(at + 1);
            sip = sip.slice(0, at);
        }
        var p = providers.find(function (p) { return p.domain == d });
        if (p) {
            return p.base + "?sip=" + encodeURIComponent(sip) + "&salt=" + p.salt + "&auth=" + encodeURIComponent(innovaphone.crypto.sha256(p.token + p.salt + sip)) + "&size=" + size + (dn ? ("&dn=" + encodeURIComponent(dn)) : "") + "&user=" + encodeURIComponent(user) + "&dom=@" + domain + ((sip == user) ? "&no-cache" : "");
        }
        else {
            return "";
        }
    }

    var id = {};
    var next = 1;
    this.url1 = function (sip, size, url) {
        if (url) return "url(" + url + ")";
        if (!sip) sip = "";
        else if (sip == undefined) sip = "";
        var at = sip.indexOf("@");
        var d = appdomain;
        var u = sip;
        if (at != -1) {
            d = sip.slice(at + 1);
            u = sip.slice(0, at);
        }
        var p = providers.find(function (p) { return p.domain == d });
        if (p) {
            return `url(${p.base}?sip=${encodeURIComponent(u)}&salt=${p.salt}&auth=${encodeURIComponent(innovaphone.crypto.sha256(p.token + p.salt + u))}&size=${size}&user=${encodeURIComponent(user)}&dom=@${domain}${(sip == user) ? "&no-cache" : ""})`;
        }
        if (at == -1) sip += "@" + appdomain;
        if (!id[sip]) {
            id[sip] = next++;
            document.body.style.setProperty("--img-" + id[sip], "none");
            presence = new clientApi.Src(function (obj) {
                if (obj.msg.mt == "PresenceUpdated" && obj.msg.sip == sip && obj.msg.ep.avatar) {
                    let url = obj.msg.ep.avatar;
                    if (url.toLowerCase().startsWith("http:")) url = "https" + url.slice(4);
                    document.body.style.setProperty("--img-" + id[sip], "url(" + url + ")");
                }
            }).send({ mt: "SubscribePresence", sip: sip });
        }
        return `var(--img-${id[sip]})`;
    }
}
