import { GAME_PHASES, GAME_EVENTS } from './GameConstants.js';

export class TurnManager {
    constructor() {
        this.currentTurn = 0;
        this.currentPhase = GAME_PHASES.SETUP;
        this.listeners = {}; // Simple event system
    }

    startGame() {
        this.currentTurn = 1;
        this.setPhase(GAME_PHASES.HERO); // Jump straight to Hero phase for now
        console.log(`--- TURN ${this.currentTurn} START ---`);
    }

    nextPhase() {
        // Simple sequential flow for now
        switch (this.currentPhase) {
            case GAME_PHASES.POWER:
                this.setPhase(GAME_PHASES.HERO);
                break;
            case GAME_PHASES.HERO:
                // Usually goes to Exploration if at edge, or Monster if not.
                // For this dev stage, let's allow manual triggering of Exploration
                // via UI, so we stay in HERO until confirmed done.
                this.setPhase(GAME_PHASES.MONSTER);
                break;
            case GAME_PHASES.MONSTER:
                this.endTurn();
                break;
            // Exploration is usually triggered as an interrupt, not strictly sequential
        }
    }

    setPhase(phase) {
        if (this.currentPhase !== phase) {
            console.log(`Phase Switch: ${this.currentPhase} -> ${phase}`);
            this.currentPhase = phase;
            this.emit(GAME_EVENTS.PHASE_CHANGED, phase);
        }
    }

    triggerExploration() {
        this.setPhase(GAME_PHASES.EXPLORATION);
        // Logic to return to HERO phase would handle elsewhere
    }

    endTurn() {
        console.log(`--- TURN ${this.currentTurn} END ---`);
        this.currentTurn++;
        this.setPhase(GAME_PHASES.POWER);
    }

    // -- Simple Observer Pattern --
    on(event, callback) {
        if (!this.listeners[event]) this.listeners[event] = [];
        this.listeners[event].push(callback);
    }

    emit(event, data) {
        if (this.listeners[event]) {
            this.listeners[event].forEach(cb => cb(data));
        }
    }
}
