import { playMusic, stopMusic } from '../audio.js';

export default class MainMenu extends Phaser.Scene {
    constructor() {
        super({ key: 'MainMenu' });
    }

    preload() {
        this.load.json('gameConfig', 'config/game.json');
        this.load.audio('mainTheme', 'assets/music/00-main-theme.mp3');
    }

    shutdown() {
        stopMusic(this);
    }

    create() {
        playMusic(this, 'mainTheme');

        const cx = 800, cy = 450;

        // Title
        this.add.text(cx, 280, 'ATTENTION RETRO DISORDER', {
            fontSize: '64px',
            fontFamily: 'monospace',
            color: '#00ff00',
            stroke: '#005500',
            strokeThickness: 4,
        }).setOrigin(0.5);

        this.add.text(cx, 370, 'A MULTI-GAME RETRO EXPERIENCE', {
            fontSize: '28px',
            fontFamily: 'monospace',
            color: '#00aa00',
        }).setOrigin(0.5);

        // Game list
        const games = [
            '★  COLORADO DEFENSE  (Missile Command)',
            '★  CODE BUG INVADERS  (Space Invaders)',
            '★  DOT DUDE  (Pac-Man)',
            '★  SMASH OUT  (Arkanoid)',
        ];
        games.forEach((txt, i) => {
            this.add.text(cx, 470 + i * 38, txt, {
                fontSize: '22px',
                fontFamily: 'monospace',
                color: '#ffff00',
            }).setOrigin(0.5);
        });

        // Blink "Press SPACE"
        const prompt = this.add.text(cx, 730, 'PRESS SPACE OR CLICK TO START', {
            fontSize: '32px',
            fontFamily: 'monospace',
            color: '#ffffff',
        }).setOrigin(0.5);

        this.tweens.add({
            targets: prompt,
            alpha: 0,
            duration: 600,
            yoyo: true,
            repeat: -1,
        });

        this.input.keyboard.once('keydown-SPACE', () => this.startGame());
        this.input.once('pointerdown', () => this.startGame());
    }

    startGame() {
        const cfg = this.cache.json.get('gameConfig');
        this.scene.start('GameManager', { config: cfg, level: 1, lives: cfg.lives });
    }
}
