glue42splash.on("updateStatus", (arg) => {
    updateStatus(arg.text);
});

glue42splash.on("pingGW", (arg) => {
    const gwURL = arg.gwURL;

    const retry = () => {
        console.log(`testing connectivity to ${gwURL}`);

        const webSocket = new WebSocket(gwURL);
        console.log(webSocket);

        webSocket.onopen = () => {
            console.log(`received open from GW on ${gwURL} - resolving...`);
            webSocket.close();
        };

        webSocket.onerror = (err) => {
            console.log(`received error from GW on ${gwURL} - will retry`, err);
            setTimeout(() => {
                retry();
            }, 500);
        };
    };

    retry();
});

document.addEventListener("DOMContentLoaded", function (event) {
    // copyright
    const copyright = document.getElementById("copyright");
    const year = new Date().getFullYear();
    copyright.innerHTML = `&copy; ${year} ${copyright.innerHTML}`;

    // parse url params - splash.html?message=Starting&env=DEMO&region=INTEROP.IO&version=9.0.0.25
    const urlParams = new URLSearchParams(window.location.search);
    const message = urlParams.get("message");
    updateStatus(message);
    const version = urlParams.get("version");
    if (version) {
        const env = urlParams.get("env");
        const region = urlParams.get("region");
        const status = document.getElementById("version");
        status.innerHTML = `${version} (${env}) - ${region}`;
    }
});

function updateStatus(message) {
    console.log(`updating status to ${message}`);
    var status = document.getElementById("status");
    status.innerHTML = message + "...";
}
