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() {
    const statusSeed = document.getElementById("status-seed");
    const statusLevel = document.getElementById("status-level");
    const statusMazeSize = document.getElementById("status-maze-size");
    const statusChests = document.getElementById("status-chests");
    const statusTime = document.getElementById("status-time");
    const statusKey = document.getElementById("status-key");
    const statusRounds = document.getElementById("status-rounds");
    const statusMessage = document.getElementById("status-message");
    
    if (!statusSeed || !statusLevel || !statusMazeSize || !statusChests || !statusTime || !statusKey || !statusRounds || !statusMessage) {
        console.warn("Some status display elements are missing from DOM");
        return;
    }
    
    statusSeed.textContent = sharedState.config.seed;
    statusLevel.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);
    
    statusMazeSize.textContent = `${effectiveWidth}x${effectiveHeight}`;
    statusChests.textContent = effectiveChests;
    statusTime.textContent = sharedState.runtime.elapsedSeconds.toFixed(1);
    statusKey.textContent = sharedState.runtime.hasKey ? "yes" : "no";
    statusRounds.textContent = sharedState.runtime.roundsCompleted;
    statusMessage.textContent = sharedState.runtime.message;
}

// Initialize event listeners
const btnStart = document.getElementById("btn-start");
const btnRestart = document.getElementById("btn-restart");
const btnRandomize = document.getElementById("btn-randomize");

if (btnStart) {
    btnStart.addEventListener("click", () => {
        primeSfx();
        playSfx("click", 0.7);
        resetRun("Run started.");
    });
}

if (btnRestart) {
    btnRestart.addEventListener("click", () => {
        primeSfx();
        playSfx("click", 0.7);
        restartLevel("Level restarted.");
    });
}

if (btnRandomize) {
    btnRandomize.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);
        
        const statusMazeSize = document.getElementById("status-maze-size");
        const statusChests = document.getElementById("status-chests");
        const statusTime = document.getElementById("status-time");
        const statusKey = document.getElementById("status-key");
        const statusRounds = document.getElementById("status-rounds");
        
        if (statusMazeSize) statusMazeSize.textContent = `${effectiveWidth}x${effectiveHeight}`;
        if (statusChests) statusChests.textContent = effectiveChests;
        if (statusTime) statusTime.textContent = sharedState.runtime.elapsedSeconds.toFixed(1);
        if (statusKey) statusKey.textContent = sharedState.runtime.hasKey ? "yes" : "no";
        if (statusRounds) statusRounds.textContent = sharedState.runtime.roundsCompleted;
    }
}, 100);

// Initial display
updateDisplay();
