import {WebWebSocketHandler} from "./WebWebSocketHandler";
import {Modal} from "./Modal";
import {WebHandler} from "./WebHandler";
import {Pane} from "./Pane";
import {WebSocketPayload} from "../WebSocketPayload";
import {WebSocketEvent} from "../WebSocketEvent";
import {ButtonType} from "./ButtonType";
import {Containers} from "./Containers";
import {Setup} from "./Setup";
import {RequestType} from "../RequestType";

const main = document.getElementById("main");
const time = document.getElementById("right");

document.addEventListener("DOMContentLoaded", async () => {
    console.log("DOM Loaded");
    WebHandler.openPane(Pane.NONE);

    let modal = new Modal("start", "iTender");
    let txt = document.createElement("p");
    txt.innerText = "Willkommen"
    modal.addContent(txt);
    modal.loader = true;
    //modal.open();
    await connect();

    setupOnClickEvents();

    setTimeout(load, 100);
});

function setupOnClickEvents() {
    // Menu BTN
    const menuBtn = document.getElementById("menuBtn") as HTMLButtonElement;
    menuBtn.disabled = true;

    function doMenu() {
        if (WebHandler.currentPane != Pane.MENU) {
            WebHandler.openPane(Pane.MENU);
        } else {
            WebHandler.openPane(Pane.MAIN);
        }
    }

    menuBtn.onclick = doMenu;

    // Menu buttons
    const menuContainersBtn = document.getElementById("menu_containers") as HTMLButtonElement;
    const menuStatsBtn = document.getElementById("menu_stats") as HTMLButtonElement;

    menuContainersBtn.onclick = Containers.openMenu;


    menuStatsBtn.onclick = async () => {

        let statsModal = new Modal("stats", "Statistiken");

        let txt = document.createElement("p");
        txt.innerHTML = `Folgende Statistiken wurden erfasst.`;
        statsModal.addContent(txt);

        let div = document.createElement("div");
        div.style.textAlign = "left";
        statsModal.addContent(div);

        let list = document.createElement("ul");
        div.append(list);

        statsModal.addContent(document.createElement("br"));
        statsModal.addButton(ButtonType.PRIMARY, "Schließen", () => statsModal.close());

        WebWebSocketHandler.request(RequestType.STATS).then((payload) => {
            let li = document.createElement("li");
            li.innerText = "Cocktails ausgegeben: " + payload.data["content"]["drinks_finished"];
            list.append(li);

            li = document.createElement("li");
            li.innerText = "Häufigster Cocktail: " + payload.data["content"]["drink_most"];
            list.append(li);

            li = document.createElement("li");
            li.innerText = "Anzahl Ingredients: " + payload.data["content"]["count_ingredients"];
            list.append(li);

            li = document.createElement("li");
            li.innerText = "Anzahl Cocktails: " + payload.data["content"]["count_cocktails"];
            list.append(li);

        });

        await statsModal.open();
    };

    const menuSettingsBtn = document.getElementById("menu_settings") as HTMLButtonElement;
    const menuSetupBtn = document.getElementById("menu_setup") as HTMLButtonElement;

    menuSettingsBtn.onclick = () => WebHandler.openPane(Pane.SETTINGS);
    menuSetupBtn.onclick = () => {
        let payload = new WebSocketPayload(WebSocketEvent.SETUP, false, true);
        WebWebSocketHandler.send(payload);
    }

    WebWebSocketHandler.request(RequestType.CONTAINERS).then(Setup.onContainerUpdate);


    // Settings Btns
    const downloadDrinks = document.getElementById("settings_refreshDrinks") as HTMLButtonElement;
    downloadDrinks.onclick = () => {
        WebWebSocketHandler.request(RequestType.DOWNLOAD_DRINKS);
    }

}

function load() {
    if (!main || !time)
        return;

    setInterval(() => {
        let currentDate = new Date();
        time.innerText = "" + (currentDate.getHours() < 10 ? "0" + currentDate.getHours() : currentDate.getHours()) + ":" + (currentDate.getMinutes() < 10 ? "0" + currentDate.getMinutes() : currentDate.getMinutes());
    }, 1000);
}

let wsHandler;

function connect(): Promise<void> {
    return new Promise(resolve => {
        wsHandler = new WebWebSocketHandler(resolve);
    });

}
