var innovaphone = innovaphone || {};
innovaphone.fixcert = innovaphone.fixcert || {};

var STORAGE_ID_SCHEME = "appclient-scheme",
    STORAGE_ID_CONFIG = "appclient-config",
    startSchemes = {
        "dark": {
            "--client-bg1": "#232323",
            "--client-bg2": "#161616",
            "--forms": "#1E1E1E",
            "--bg1": "#282828",
            "--bg2": "#333333",
            "--bg3": "#474747",
            "--c1": "#efefef",
            "--c2": "#939393",
            "--highlight-bg": "#595959",
            "--button-bg": "#3D3D3D",
            "--accent": "#f59b10",
            "--red": "#ef4d64",
            "--red-hover": "#ff6d88",
            "--green": "#7cb270",
            "--green-hover": "#9bd48a",
            "--yellow": "#f59b10",
            "--radio-bg": "#efefef",
            "--logo-c0": "#ffffff",               // text
            "--logo-c1": "#808080",               // neutral color (--white)
            "--logo-c2": "#17baae",               // green (--yellow from light scheme)
            "--logo-c3": "#f59b10",               // orange (--yellow from dark scheme)
        },

        "light": {
            "--client-bg1": "#e5e5e5",
            "--client-bg2": "#ffffff",
            "--forms": "#ededed",
            "--bg1": "#e3e3e3",
            "--bg2": "#f5f5f5",
            "--bg3": "#ffffff",
            "--c1": "#000000",
            "--c2": "#404040",
            "--highlight-bg": "#f9f9f9",
            "--button-bg": "#cccccc",
            "--accent": "#f59b10",
            "--red": "#ef4d64",
            "--red-hover": "#ff6d88",
            "--green": "#7cb270",
            "--green-hover": "#9bd48a",
            "--yellow": "#f59b10",
            "--radio-bg": "#f7f7f7",
            "--logo-c0": "#4a4a4a",               // text
            "--logo-c1": "#808080",               // neutral color (--c2 from light scheme)
            "--logo-c2": "#17baae",               // green (--yellow from light scheme)
            "--logo-c3": "#f59b10",               // orange (--yellow from dark scheme)
        }
    };

innovaphone.fixcert.Start = innovaphone.fixcert.Start || function (start) {
    if (!browserSupported()) {
        new NotSupportedPage();
        return;
    }

    var lang = new innovaphone.lib1.Languages(fixcertTexts, start.lang),
        schemes = new innovaphone.ui1.CssVariables(startSchemes),
        ui = new innovaphone.fixcert.Ui(lang);

    schemes.activate(localStorage.getItem(STORAGE_ID_SCHEME) || "dark");
}

function NotSupportedPage() {
    var warning = new innovaphone.ui1.Div("text-align: center; font-size: 24px; color: #414141;", "The version of the web browser control is not supported. Update if possible.");
    this.createNode("body", "background-color: #dde2e3; padding: 40px;");
    this.add(warning);
}
NotSupportedPage.prototype = innovaphone.ui1.nodePrototype;

function browserSupported() {
    try {
        if (!innovaphone.ui1.cssVariablesSupported()) return false;
        if (!localStorage) return false;
        localStorage.setItem("localStorageTest", "test");
        localStorage.removeItem("localStorageTest");
    }
    catch (e) {
        return false;
    }
    return true;
}

innovaphone.fixcert.Ui = innovaphone.fixcert.Ui || function (lang) {
    var that = this,
        header = new innovaphone.ui1.Node("header", "display: flex; flex: 0 0 auto; height: 86px; background-color: var(--client-bg2); flex-direction: column; justify-content: center; align-items: center;", null, "screen-header"),
        logo = new innovaphone.ui1.Svg("height: 65px;", "appclient_logo.svg#logo0"),
        main = null,
        trustCert = null;

    function init() {
        that.createNode("body");
        that.add(header);
        header.add(logo);
        main = that.add(new innovaphone.ui1.Scrolling("position:absolute; top: 100px; right: 0; bottom: 0; left: 0; display: flex; flex-direction: column; jutsify-content: space-between;", -1, 0, 5, "var(--bg1)"));
        main.add(new innovaphone.fixcert.TrustCert(lang, that));
    }

    init();
}
innovaphone.fixcert.Ui.prototype = innovaphone.ui1.nodePrototype;

innovaphone.fixcert.TrustCert = innovaphone.fixcert.TrustCert || function (lang, ui) {
    var fixcert = this,
        info = null,
        h1 = null,
        text = null,
        btnCont = null,
        myappsBtn = null,
        closeWindow = null,
        screenshotsCont = null,
        screenshots = null,
        text2 = null,
        iframe = null;

    function Text(type, text, color, margin, size) {
        this.createNode(type).addTranslation(lang, text);
        this.container.style.color = color ? color : "var(--c1)";
        this.container.style.margin = margin ? margin : "0px";
        this.container.style.fontSize = size ? size : "16px";
    }
    Text.prototype = innovaphone.ui1.nodePrototype;

    function init() {
        fixcert.createNode("div", "display: flex; flex-direction: column; justify-content: center; margin: 0px; padding: 0px; ");
        info = fixcert.add(new innovaphone.ui1.Node("div", "display:flex; flex-direction: column;align-items: center; justify-content: center; text-align: center;"));
        h1 = fixcert.add(new innovaphone.ui1.Node("h1").addTranslation(lang, "certConf"));
        text = fixcert.add(new innovaphone.ui1.Node("div", "flex: 1 1 auto; margin: 0px 20px 20px 20px; flex: 0 0 auto; font-size: 16px; color: var(--c1); text-align: center; word-wrap: break-word"));

        closeWindow = new Text("p", "close", "var(--c2)", "10px 0px", "14px");

        text.add(new Text("p", "certNeedsTrust", null, "0px 5px 5px 5px"));
        text.add(new Text("span", "certNavigate"));
        text.add(new Text("span", "certSettingsPath", "var(--c2)"));
        text.add(new Text("span", "certChoose"));
        text.add(new Text("span", "certSettings", "var(--c2"));
        text.add(new Text("span", "certActivate"));

        screenshotsCont = fixcert.add(new innovaphone.ui1.Div("display: flex; justify-content: center"));
        screenshots = screenshotsCont.add(new innovaphone.ui1.Div("display: flex; overflow-x: auto;", null, "screenshots"));
        screenshots.add(new innovaphone.ui1.Node("img", "height: 280px; margin: 10px;")).container.setAttribute("src", "fixcert-01-00.png");
        screenshots.add(new innovaphone.ui1.Node("img", "height: 280px; margin: 10px;")).container.setAttribute("src", "fixcert-01-01.png");
        screenshots.add(new innovaphone.ui1.Node("img", "height: 280px; margin: 10px;")).container.setAttribute("src", "fixcert-01-02.png");
        screenshots.add(new innovaphone.ui1.Node("img", "height: 280px; margin: 10px;")).container.setAttribute("src", "fixcert-01-03.png");
        screenshots.add(new innovaphone.ui1.Node("img", "height: 280px; margin: 10px;")).container.setAttribute("src", "fixcert-01-04.png");

        var text2 = fixcert.add(new innovaphone.ui1.Div("flex: 1 1 auto; margin: 20px 20px 20px 20px; flex: 0 0 auto; font-size: 16px; color: var(--c1); text-align: center; word-wrap: break-word")).addTranslation(lang, "certBack");

        btnCont = fixcert.add(new innovaphone.ui1.Node("a", "display: flex; justify-content: center; border: none;"));
        btnCont.container.setAttribute("href", "com.innovaphone.clientiosstart:");
        myappsBtn = btnCont.add(new innovaphone.ui1.Svg("width: 40px; height: 40px; cursor: pointer", "appclient_logo.svg#signet"));

        iframe = fixcert.add(new innovaphone.ui1.Node("iframe", "height: 0px; width: 0px; border: none; box-shadow: none;"));
        iframe.container.setAttribute("src", "cert.mobileconfig");
    }

    init();
}
innovaphone.fixcert.TrustCert.prototype = innovaphone.ui1.nodePrototype;