var innovaphone = innovaphone || {};
innovaphone.pbx = innovaphone.pbx || {};
innovaphone.pbx.appclient = innovaphone.pbx.appclient || {};
innovaphone.pbx.appclient.StatusHeader = innovaphone.pbx.appclient.StatusHeader || function (start) {
    var icons = {
        backlight: {
            viewbox: '0 0 32 32',
            code: '<path d="m 15.5,23.5 c 3.875,0 7,-3.15 7,-7 0,-3.85 -3.15,-7.0000003 -7,-7.0000003 -3.85,0 -7,3.1500003 -7,7.0000003 0,3.85 3.125,7 7,7 z"/><path d="m 16.525,6.2499997 v -3.225 c 0,-0.575 -0.45,-1.025 -1.025,-1.025 -0.575,0 -1.025,0.45 -1.025,1.025 v 3.225 c 0,0.575 0.45,1.025 1.025,1.025 0.575,0 1.025,-0.45 1.025,-1.025 z"/><path d="m 14.475,26.75 v 3.225 c 0,0.575 0.45,1.025 1.025,1.025 0.575,0 1.025,-0.45 1.025,-1.025 V 26.75 c 0,-0.575 -0.45,-1.025 -1.025,-1.025 -0.575,0 -1.025,0.45 -1.025,1.025 z"/><path d="m 23.475,9.9749997 2.275,-2.275 c 0.4,-0.4 0.4,-1.05 0,-1.45 -0.4,-0.4 -1.05,-0.4 -1.45,0 l -2.275,2.275 c -0.4,0.4 -0.4,1.05 0,1.45 0.2,0.2000003 0.45,0.3000003 0.725,0.3000003 0.275,0 0.525,-0.1 0.725,-0.3000003 z"/><path d="m 5.25,26.75 c 0.2,0.2 0.45,0.3 0.725,0.3 0.25,0 0.525,-0.1 0.725,-0.3 l 2.275,-2.275 c 0.4,-0.4 0.4,-1.05 0,-1.45 -0.4,-0.4 -1.05,-0.4 -1.45,0 L 5.25,25.3 c -0.4,0.4 -0.4,1.05 0,1.45 z"/><path d="m 30,16.5 c 0,-0.575 -0.45,-1.025 -1.025,-1.025 H 25.75 c -0.575,0 -1.025,0.45 -1.025,1.025 0,0.575 0.45,1.025 1.025,1.025 h 3.225 c 0.55,0 1.025,-0.45 1.025,-1.025 z"/><path d="M 2.025,17.525 H 5.25 c 0.575,0 1.025,-0.45 1.025,-1.025 0,-0.575 -0.45,-1.025 -1.025,-1.025 H 2.025 C 1.45,15.475 1,15.925 1,16.5 c 0,0.575 0.475,1.025 1.025,1.025 z"/><path d="m 25.025,27.05 c 0.25,0 0.525,-0.1 0.725,-0.3 0.4,-0.4 0.4,-1.05 0,-1.45 l -2.275,-2.275 c -0.4,-0.4 -1.05,-0.4 -1.45,0 -0.4,0.4 -0.4,1.05 0,1.45 L 24.3,26.75 c 0.2,0.2 0.475,0.3 0.725,0.3 z"/><path d="m 8.975,9.9749997 c 0.4,-0.4 0.4,-1.05 0,-1.45 l -2.275,-2.275 c -0.4,-0.4 -1.05,-0.4 -1.45,0 -0.4,0.4 -0.4,1.05 0,1.45 l 2.275,2.275 C 7.725,10.175 7.975,10.275 8.25,10.275 c 0.275,0 0.525,-0.1 0.725,-0.3000003 z"/>'
        },
        alarm: {
            viewbox: '0 0 32 32',
            code: "<path d=\"M19.56,17,10.84,1.69a1.69,1.69,0,0,0-3,.09L.22,17.09a1.7,1.7,0,0,0,1.52,2.46H18.08A1.7,1.7,0,0,0,19.56,17Zm-9.09,0h-2V15h2Zm0-4h-2V6.9h2Z\"/>",
        }
    };

    colorSchemes = {
        dark: {
            "--bg1": "#000000",
            "--bg2": "#2B2B2B",
            "--bg3": "#333333",
            "--red": "#EA3F50",
            "--shadow": "#939393",
            "--heading": "#56EBE0",
            "--heading-text": "white",
            "--standard-text": "#A3A3A3",
            "--accent": "#12827B",
            "--accent-text": "#FFFFFF", // text on accent color
            "--orange": "#F59B10",
            "--buttons": "#DBDBDB",
            "--border": "#1B3D41",
            "--border-contrast": "#A3A3A3",
            "--shader": "#01606833",
            "--hover-bg": "#555555",
            "--dialog-shader": "#000000DD",
            "--logo-bg": "#014144",
        },
        light: {
            "--bg1": "#FFFFFF",
            "--bg2": "#E9E9E9",
            "--bg3": "#CCCCCC",
            "--red": "#EA3F50",
            "--shadow": "#3336",
            "--heading": "#006068",
            "--heading-text": "black",
            "--standard-text": "#707070",
            "--accent": "#17BAAE",
            "--accent-text": "#000000", // text on accent color
            "--orange": "#F59B10",
            "--buttons": "#DBDBDB",
            "--border": "#4C8C91",
            "--border-contrast": "#000000",
            "--shader": "#01606833",
            "--hover-bg": "#BFE7E2",
            "--dialog-shader": "#00000099",
            "--logo-bg": "#23a39a",
        },
    };

    function App(start) {
        var that = this;
        if (localStorage["appclient-config"] && JSON.parse(localStorage["appclient-config"])) pwd = JSON.parse(localStorage["appclient-config"]).servicePwd;
        var connection = new innovaphone.appwebsocket.Connection(start.url, "@launcher-status", pwd);
        var colors = new innovaphone.ui1.CssVariables(colorSchemes, start.scheme);
        this.onactivate = new innovaphone.lib1.Event(this);
        this.onmessage = new innovaphone.lib1.Event(this);

        this.createNode("body");
        var main = new innovaphone.ui1.Node("div", "margin: 0px; padding: 0px;display:grid; display: grid; grid-template-columns: auto auto;  ");
        this.add(main);
        update_info = new innovaphone.ui1.Div("width: 75px; height: 25px; margin: 0px; text-align:center").addText("UPDATE");
        var icon1 = new innovaphone.ui1.Div("display: flex");
        main.add(icon1);
        var alarm = new innovaphone.ui1.SvgInline(" height:40px; fill: red; padding: 10px;", icons.alarm.viewbox, icons.alarm.code);
        var a = null;
        a = icon1.add(alarm);
        var sshdvalue = null;
        var remotevalue = null;
        var firmwareupdate = false;
        this.onmessage.attach(function (sender, obj) { 
            if (obj.mt === "ClientStatus") {
                if (obj.sshd) sshdvalue = obj.sshd;
                if (obj.remotebrowserdebug) remotevalue = obj.remotebrowserdebug;
                if (obj.firmwareupdate) {
                    firmwareupdate = true
                    update_info.container.style.backgroundColor = "red";
                    if (a) {
                        icon1.rem(a);
                        a = null;
                    }
                    main.add(update_info);
                } 
                else if (obj.firmwareupdatecomplete) {
                    firmwareupdate = false
                    update_info.container.style.backgroundColor = "green";
                    main.rem(update_info)
                }
                else if (obj.firmwareupdateprogress) {
                    update_info.container.style.backgroundColor = "yellow";
                    update_info.addText(obj.firmwareupdateprogress)
                }
                else if (obj.firmwareupdateprogressi) {
                    update_info.container.style.backgroundColor = "red";
                    update_info.addText(obj.firmwareupdateprogressi)
                }

                if (!firmwareupdate) {
                    if (sshdvalue === "true" || remotevalue === "true") {
                        if (a === null) a = icon1.add(alarm);
                    }
                    else {
                        if (a) {
                            icon1.rem(a);
                            a = null;
                        }
                    }
                }
            }
        });
        connection.onconnected = function (domain, user, dn) {
            connection.send({ mt: "MonitorStatusChanges", value: "1" })
            connection.send({ mt: "io", op: "StartMonitoring" });
        };
        connection.onmessage = function (msg) {
            if (msg) that.onmessage.notify(msg);
        };
        connection.onclosed = function () { console.log("**** CLOSE IP270"); };
        start.onschemechanged.attach(function () { colors.activate(start.scheme); });
        colors.activate(start.scheme);
        //start.onlangchanged.attach(function () { lang.activate(start.lang); });
        //lang.activate(start.lang);
    }
    App.prototype = innovaphone.ui1.nodePrototype;
    return new App(start);

};
