import { sharedState } from "./game/state.js";
import { playSfx, primeSfx } from "./game/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 = 60;
    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 = 60;
    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("status-seed").textContent = sharedState.config.seed;
    document.getElementById("status-level").textContent = sharedState.config.level;
    
    // Calculate effective maze size and chest count based on current level
    const roundScale = Math.max(0, sharedState.config.level - 1);
    const effectiveWidth = Math.max(9, sharedState.config.mazeWidth + roundScale * 2);
    const effectiveHeight = Math.max(9, sharedState.config.mazeHeight + roundScale * 2);
    const effectiveChests = Math.max(1, sharedState.config.minChestDeadEnds + roundScale);
    
    document.getElementById("status-maze-size").textContent = `${effectiveWidth}x${effectiveHeight}`;
    document.getElementById("status-chests").textContent = effectiveChests;
    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();
});

// Update status display on game loop
setInterval(() => {
    if (sharedState.runtime.runActive) {
        const roundScale = Math.max(0, sharedState.config.level - 1);
        const effectiveWidth = Math.max(9, sharedState.config.mazeWidth + roundScale * 2);
        const effectiveHeight = Math.max(9, sharedState.config.mazeHeight + roundScale * 2);
        const effectiveChests = Math.max(1, sharedState.config.minChestDeadEnds + roundScale);
        
        document.getElementById("status-maze-size").textContent = `${effectiveWidth}x${effectiveHeight}`;
        document.getElementById("status-chests").textContent = effectiveChests;
        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();
