import {WebWebSocketHandler} from "./WebWebSocketHandler";
import {RequestType} from "../RequestType";
import {Modal} from "./Modal";
import {ButtonType} from "./ButtonType";

export class Settings {
    public static addListeners() {
        // Settings Btns
        const downloadDrinks = document.getElementById("settings_refreshDrinks") as HTMLButtonElement;
        downloadDrinks.onclick = () => this.onClickRefreshDrinks();
        const getInfo = document.getElementById("settings_getInfo") as HTMLButtonElement;
        getInfo.onclick = () => this.onClickInfo();

        const reload = document.getElementById("settings_reload") as HTMLButtonElement;
        reload.onclick = () => window.location.reload();

        const update = document.getElementById("settings_update") as HTMLButtonElement;
        update.onclick = () => this.update();
    }

    private static onClickRefreshDrinks() {
        WebWebSocketHandler.request(RequestType.DOWNLOAD_DRINKS).then();
    }

    private static onClickUpdate() {

    }

    private static onClickInfo() {
        WebWebSocketHandler.request(RequestType.INFO, null).then((payload) => {
            let modal = new Modal("info", "Systeminformationen");

            let table = document.createElement("table");
            table.style.marginLeft = "auto";
            table.style.marginRight = "auto";

            let th = document.createElement("th");
            table.append(th);

            let tdTh1 = document.createElement("td");
            tdTh1.innerText = "";
            let tdTh2 = document.createElement("td");
            tdTh2.innerText = "";

            th.append(tdTh1, tdTh2);

            let x = [["internet", "Internet-Konnektivität"], ["ip", "IP-Adresse"], ["network", "WiFi-Netzwerk"], ["uptime", "Gerät aktiv seit"], ["version", "Version"], ["author", "Entwickler"], ["contact", "Support-Adresse"]];
            for (let y of x) {
                let tr = document.createElement("tr");
                let td1 = document.createElement("td");
                let td2 = document.createElement("td");

                td1.innerText = y[1];
                td1.style.fontWeight = "bold";
                if (payload.data[y[0]] === true || payload.data[y[0]] === false) {
                    td2.innerText = payload.data[y[0]] == true ? "Verbunden" : "Getrennt";
                } else {
                    td2.innerText = payload.data[y[0]];
                }

                tr.append(td1, td2);

                table.append(tr);
            }
            modal.addContent(table);

            modal.addBR();
            modal.addButton(ButtonType.PRIMARY, "Schließen", () => modal.close());
            modal.open();
        });
    }

    private static update() {
        WebWebSocketHandler.request(RequestType.UPDATE, null).then((payload) => {
            let modal = new Modal("info", "System-Update");
            let txt = document.createElement("p");
            if (payload.data as boolean) {

                txt.innerHTML = `Der iTender wird nun aktualisiert!<br><br>
Sobald das Update installiert ist, wird das System neu gestartet.<br>Die dadurch hergehende Verbindungswarnung kann ignoriert werden.<br>Der iTender stellt die Verbindung automatisch wieder her.<br><br><span style="color:red;font-weight: bold">Schalten Sie das System nicht aus und entfernen Sie nicht das Netzkabel!</span>`;
                modal.addContent(txt);
                modal.loader = true;
            } else {
                txt.innerHTML = `Das System kann nicht aktualisiert werden.<br>iTender hat keine Internet-Konnektivität fest gestellt.<br>Versuchen Sie es zu einem späteren Zeitpunkt erneut.`;
                modal.addButton(ButtonType.PRIMARY, "Schließen", () => modal.close());
            }

            modal.open();
        });
    }
}