import { sharedState } from "./game/state.js";
import { playSfx, primeSfx } from "./sfx.js";

// Handler functions (same as p5_panel but without p5 scoping)
function resetRun(message) {
    sharedState.runtime.runActive = true;
    sharedState.runtime.hasKey = false;
    sharedState.runtime.roundsCompleted = 0;
    sharedState.runtime.elapsedSeconds = 0;
    sharedState.runtime.message = message;
    sharedState.config.level = 1;
    try { window.mazeGameApi.generateLevel(); } catch (e) { console.warn(e); }
    updateDisplay();
}

function restartLevel(message) {
    sharedState.runtime.hasKey = false;
    sharedState.runtime.elapsedSeconds = 0;
    sharedState.runtime.message = message;
    try { window.mazeGameApi.generateLevel(); } catch (e) { console.warn(e); }
    updateDisplay();
}

function randomizeSeed() {
    sharedState.config.seed = Math.floor(Math.random() * 100000);
    sharedState.runtime.message = `Seed set to ${sharedState.config.seed}.`;
    updateDisplay();
}

// Update display from shared state
function updateDisplay() {
    document.getElementById("value-width").textContent = sharedState.config.mazeWidth;
    document.getElementById("value-height").textContent = sharedState.config.mazeHeight;
    document.getElementById("value-deadends").textContent = sharedState.config.minChestDeadEnds;
    
    document.getElementById("status-seed").textContent = sharedState.config.seed;
    document.getElementById("status-level").textContent = sharedState.config.level;
    document.getElementById("status-time").textContent = sharedState.runtime.elapsedSeconds.toFixed(1);
    document.getElementById("status-key").textContent = sharedState.runtime.hasKey ? "yes" : "no";
    document.getElementById("status-rounds").textContent = sharedState.runtime.roundsCompleted;
    document.getElementById("status-message").textContent = sharedState.runtime.message;
}

// Initialize event listeners
document.getElementById("btn-start").addEventListener("click", () => {
    primeSfx();
    playSfx("click", 0.7);
    resetRun("Run started.");
});

document.getElementById("btn-restart").addEventListener("click", () => {
    primeSfx();
    playSfx("click", 0.7);
    restartLevel("Level restarted.");
});

document.getElementById("btn-randomize").addEventListener("click", () => {
    primeSfx();
    playSfx("click", 0.7);
    randomizeSeed();
});

document.getElementById("slider-width").addEventListener("input", (e) => {
    primeSfx();
    const value = parseInt(e.target.value) | 1;
    sharedState.config.mazeWidth = value;
    document.getElementById("value-width").textContent = value;
});

document.getElementById("slider-height").addEventListener("input", (e) => {
    primeSfx();
    const value = parseInt(e.target.value) | 1;
    sharedState.config.mazeHeight = value;
    document.getElementById("value-height").textContent = value;
});

document.getElementById("slider-deadends").addEventListener("input", (e) => {
    primeSfx();
    const value = parseInt(e.target.value);
    sharedState.config.minChestDeadEnds = value;
    document.getElementById("value-deadends").textContent = value;
});

// Update status display on game loop
setInterval(() => {
    if (sharedState.runtime.runActive) {
        document.getElementById("status-time").textContent = sharedState.runtime.elapsedSeconds.toFixed(1);
        document.getElementById("status-key").textContent = sharedState.runtime.hasKey ? "yes" : "no";
        document.getElementById("status-rounds").textContent = sharedState.runtime.roundsCompleted;
    }
}, 100);

// Initial display
updateDisplay();
