const button = document.getElementById("toggleServer");
const status = document.querySelector(".online");

button.addEventListener("click", async () => {
    if (status.textContent === "Online") {
        await fetch("/stop");
    } else {
        await fetch("/start");
    }

    updateStatus();
});

async function updateStatus() {
    const res = await fetch("/status");
    const data = await res.json();

    if (data.online) {
        status.textContent = "Online";
        status.style.backgroundColor = "rgb(0, 181, 0)";
        button.textContent = "Stop";
        button.style.backgroundColor = "red";
    } else {
        status.textContent = "Offline";
        status.style.backgroundColor = "gray";
        button.textContent = "Start";
        button.style.backgroundColor = "rgb(0, 181, 0)";
    }
}

setInterval(updateStatus, 3000);
updateStatus();

async function updatePortAnzeige() {
    try {
        const portRes = await fetch("/getPort");
        const portData = await portRes.json();

        const ipRes = await fetch("/getIP");
        const ipData = await ipRes.json();

        const port = parseInt(portData.port);
        const bedrockPortValue = port + 1;

        const portElement = document.querySelector(".ip-adresse");
        const bedrockPort = document.getElementById("bedrock-port");

        portElement.textContent = `IP: ${ipData.ip}:${port}`;
        bedrockPort.textContent = `Bedrock Port: ${bedrockPortValue}`;

    } catch (err) {
        console.error("IP/Port konnte nicht geladen werden:", err);
    }
}

// gleich beim Laden aufrufen
updatePortAnzeige();