const inputs = {
    motd: document.getElementById("motd"),
    maxPlayers: document.getElementById("maxPlayers"),
    gamemode: document.getElementById("gamemode"),
    difficulty: document.getElementById("difficulty"),
    viewDistance: document.getElementById("viewDistance"),
    simDistance: document.getElementById("simDistance"),
    whitelist: document.getElementById("whitelist"),
    spawnProtection: document.getElementById("spawnProtection"),
    requireResourcePack: document.getElementById("requireResourcePack"),
    resourcePack: document.getElementById("resourcePack"),
    resourcePackSHA1: document.getElementById("resourcePackSHA1")
};

const statusMessage = document.getElementById("statusMessage");
const saveButton = document.getElementById("saveOptions");

// Optionen laden
async function loadOptions() {
    const res = await fetch("/getOptions");
    const data = await res.json();

    inputs.motd.value = data["motd"] || "";
    inputs.maxPlayers.value = data["max-players"] || 20;
    inputs.gamemode.value = data["gamemode"] || "survival";
    inputs.difficulty.value = data["difficulty"] || 2;
    inputs.viewDistance.value = data["view-distance"] || 10;
    inputs.simDistance.value = data["simulation-distance"] || 10;
    inputs.whitelist.checked = data["white-list"] === "true";
    inputs.spawnProtection.value = data["spawn-protection"] || 16;
    inputs.requireResourcePack.checked = data["require-resource-pack"] === "true";
    inputs.resourcePack.value = data["resource-pack"] || "";
    inputs.resourcePackSHA1.value = data["resource-pack-sha1"] || "";
}

// Optionen speichern
saveButton.addEventListener("click", async () => {
    const newOptions = {
        "motd": inputs.motd.value,
        "max-players": inputs.maxPlayers.value,
        "gamemode": inputs.gamemode.value,
        "difficulty": inputs.difficulty.value,
        "view-distance": inputs.viewDistance.value,
        "simulation-distance": inputs.simDistance.value,
        "white-list": inputs.whitelist.checked.toString(),
        "spawn-protection": inputs.spawnProtection.value,
        "require-resource-pack": inputs.requireResourcePack.checked.toString(),
        "resource-pack": inputs.resourcePack.value,
        "resource-pack-sha1": inputs.resourcePackSHA1.value
    };

    const res = await fetch("/setOptions", {
        method: "POST",
        headers: {"Content-Type": "application/json"},
        body: JSON.stringify(newOptions)
    });

    statusMessage.textContent = await res.text();
});

loadOptions();


const iconButton = document.getElementById("uploadServerIcon");
const iconPreview = document.getElementById("serverIconPreview");

iconButton.onclick = () => {
    const input = document.createElement("input");
    input.type = "file";
    input.accept = "image/png";
    input.onchange = async () => {
        const file = input.files[0];
        if (!file) return;

        const formData = new FormData();
        formData.append("serverIcon", file);

        const res = await fetch("/uploadServerIcon", {
            method: "POST",
            body: formData
        });

        const text = await res.text();
        alert(text);

        // Icon Vorschau aktualisieren
        iconPreview.src = "../../minecraft-server/icon.png?" + new Date().getTime();
    };
    input.click();
};