
var innovaphone = innovaphone || {};
innovaphone.Standalone = function (htm) {
    var that = this;
    this.createNode("body", "background-color:var(--bg1)");

    var schemes = new innovaphone.ui1.CssVariables(standaloneColors, "dark");

    var texts = new innovaphone.lib1.Languages(standaloneTexts, "de");

    Login.prototype = innovaphone.ui1.nodePrototype;
    var login = null;
    var appIFrame = null;
    const params = new URLSearchParams(window.location.search);
    var appId = params.get('app');
    var app;
    var email;

    if (false && localStorage.getItem("standalone")) {
    }
    else {
        login = that.add(new Login(function (e, htm) {
            email = e;
            that.rem(login);
            if (appIFrame) that.rem(appIFrame);
            appIFrame = that.add(new innovaphone.ui1.Node("iframe", "display:block; position:absolute; left:0px; width:100%; top:0px; height:100%; border:none"));
            appIFrame.container.src = htm + "?name=" + appId ;
            appIFrame.addEvent("load", () => {
                var accountsPath = location.pathname.slice(1, location.pathname.indexOf("/", 1));
                appIFrame.container.contentWindow.postMessage(JSON.stringify({ mt: "Show" }));
                appIFrame.container.contentWindow.postMessage(JSON.stringify({ mt: "ApiUpdate", apis: { "com.innovaphone.client": { "@client": { title: "myApps", model: { user: { sip: email, domain: accountsPath } } } } } }));
            });
        }));
    }

    window.addEventListener("message", function (e) {
        if (e.data && typeof e.data == "string") {
            var obj = JSON.parse(e.data);
            switch (obj.mt) {
                case "getLogin":
                    if (app && email) {
                        app.sendSrc({ mt: "GetLogin", account: email, sip: obj.app, challenge: obj.challenge }, (msg) => {
                            var appfile = msg.appurl.slice(msg.appurl.lastIndexOf("/") + 1);
                            e.source.postMessage(JSON.stringify({ mt: "Login", src: obj.src, pbxObj: msg.pbxObj, app: appfile, domain: msg.domain, sip: msg.sip, guid: msg.guid, dn: msg.dn, digest: msg.digest }), "*");
                        })
                    }
                    break;
                case "loginResult":
                    break;
                case "logout":
                    break;
            }
        }
    });

    function Login(onlogin) {
        this.createNode("div", "position:absolute; left:0px; right:0px; top:0px; bottom:0px; display:flex; justify-content:space-around");
        InText.prototype = innovaphone.ui1.nodePrototype;
        var column = this.add(new innovaphone.ui1.Div("position:relative; width:400px; height:100%; display:flex; flex-direction:column; justify-content:space-around"));
        var form = column.add(new innovaphone.ui1.Div("position:relative; width:100%; display:flex; flex-direction:column; font-size:14px; color:var(--c1); background-color:var(--bg2); padding:20px"));
        var email = form.add(new InText("email"));
        var password = form.add(new InText("password","password")).addEvent("keydown", function (e) {
            if (e.keyCode === 13) {
                getLogin();
            }
        });
        var buttons = form.add(new innovaphone.ui1.Div("position:relative; display:flex; justify-content:space-around"));
        buttons.add(new innovaphone.ui1.Div("position:relative; width:70px; background-color:var(--bg1); text-align:center; margin:10px 0px 0px; padding:4px; cursor:pointer")).addTranslation(texts, "login").addEvent("click", getLogin);

        function getLogin() {
            if (app) app.close();
            app = new innovaphone.appwebsocket.Connection("../websocket", "-", null, null, appConnected, appMessage, appError, appError, appGetLogin, email.getValue());

            function appConnected() {
                app.sendSrc({ mt: "ReadApps", sip: appId }, (msg) => {
                    if (msg.mt == "App") {
                        onlogin(email.getValue(), msg.url + ".htm");
                    }
                });
            }

            function appMessage() {
            }

            function appError() {
            }

            function appGetLogin(appObj, challenge, dn, salt) {
                var hash = innovaphone.crypto.sha256("accounts" + ":" + salt + ":" + password.getValue());
                var digest = app.calculateDigest("-", "-", email.getValue(), "00000000000000000000000000000000", dn, challenge, hash);
                app.login({ app: "-", domain: "-", sip: email.getValue(), guid: "00000000000000000000000000000000", dn: dn, digest: digest });
                localStorage.setItem("standalone", JSON.stringify({ email: email.getValue(), key: hash }));
            }
        }
    }

    function InText(label, type) {
        this.createNode("div", "position:relative; display:flex; margin:4px");
        this.add(new innovaphone.ui1.Div("position:relative; width:100px")).addTranslation(texts, label);
        var inp = this.add(new innovaphone.ui1.Input(undefined, undefined, undefined, undefined, type));
        this.getValue = () => inp.getValue();
        this.setValue = (v) => inp.setValue(v);
    }
}
innovaphone.Standalone.prototype = innovaphone.ui1.nodePrototype;
