function unlockMapDom() {
    document.querySelectorAll(".action").forEach((e) => e.classList.remove("hidden"));
}

function lockMapDom() {
    document.querySelectorAll(".action").forEach((e) => e.classList.add("hidden"));
}

function updateDom() {
    if (gameState !== WAITING) {
        document.querySelector("#ready-button").style.display = "none";
    }

    updatePlayerDom();
    showRemainingReinforcements();
    updateMapDom();
}

function updateMapDom() {
    if (us.isPlaying) {
        unlockMapDom();
    } else {
        lockMapDom();
    }

    for (let region of Object.values(REGIONS)) {
        const element = document.querySelector(`.node[data-name=${region.name}]`);
        element.querySelector(".strength").textContent = region.strength || "";
        element.style.backgroundColor =
            region.owner === null ? "white" : region.owner.getColor();
    }
}

function updatePlayerDom() {
    let list = document.querySelector("#playerList");
    list.replaceChildren();

    for (let playerId of Object.keys(players).sort()) {
        let player = players[playerId];

        let statusSpan = document.createElement("div");
        statusSpan.classList.add("status-span");
        if (gameState === WAITING) {
            if (player.ready) {
                statusSpan.textContent = "R";
                statusSpan.classList.add("ready");
            } else {
                statusSpan.textContent = "N";
                statusSpan.classList.add("not-ready");
            }
        } else {
            if (player.isPlaying) {
                statusSpan.textContent = "P";
            }
        }

        let idSpan = document.createElement("span");
        idSpan.style.color = player.getColor();
        if (playerId === ID) {
            idSpan.textContent = `${playerId} (you)`;
        } else {
            idSpan.textContent = playerId;
        }

        let newDom = document.createElement("li");
        newDom.appendChild(statusSpan);
        newDom.appendChild(idSpan);
        list.appendChild(newDom);
    }
}

document.addEventListener("DOMContentLoaded", () => {
    document.querySelector("#ready-button").addEventListener("click", async (ev) => {
        let nowReady = ev.target.textContent === "Not ready";
        us.ready = nowReady;

        ev.target.classList.toggle("active");
        ev.target.textContent = nowReady ? "Ready" : "Not ready";

        socket.emit("message", Packet.createSetReady(nowReady));

        updatePlayerDom();

        if (allPlayersReady()) {
            await startPregame();
        }
    });

    document.querySelectorAll(".action").forEach((el) =>
        el.addEventListener("click", (ev) => {
            let region = ev.target.closest(".node").dataset.name;
            socket.emit("message", Packet.createRegionClaim(region));
        })
    );

    document.querySelector("#shuffleColors").addEventListener("click", () => {
        Object.values(players).forEach((player) => {
            player.resetColor();
        });
        updatePlayerDom();
    });
});

function showRemainingReinforcements() {
    if (!allPlaced) {
        document.querySelector(
            "#remaining-reinforcements"
        ).innerHTML = `<span>Remaining placements: ${reinforcementsRemaining()}</span>`;
    }
}

function addInfoMessage(message) {
    let child = document.createElement("div");
    child.textContent = message;

    let info = document.querySelector("#info");
    info.appendChild(child);

    setTimeout(() => {
        info.removeChild(child);
    }, 8000);
}
