import Phaser from 'phaser';

/**
 * Scène de menu - Demande permission gyroscope et lance le jeu
 */
export class MenuScene extends Phaser.Scene {
    private startButton?: Phaser.GameObjects.Text;
    private gyroStatus?: Phaser.GameObjects.Text;
    private hasStarted: boolean = false;

    constructor() {
        super({ key: 'MenuScene' });
    }

    create(): void {
        const width = this.cameras.main.width;
        const height = this.cameras.main.height;

        // Titre
        const title = this.add.text(width / 2, height / 3, 'MARIO RUNNER', {
            fontSize: '64px',
            color: '#ffffff',
            fontStyle: 'bold',
            stroke: '#000000',
            strokeThickness: 6,
        });
        title.setOrigin(0.5);

        // Instructions
        const instructions = this.add.text(
            width / 2,
            height / 2,
            'Inclinez le téléphone pour avancer/reculer\nAppuyez sur le bouton pour sauter',
            {
                fontSize: '24px',
                color: '#ffffff',
                align: 'center',
            }
        );
        instructions.setOrigin(0.5);

        // Bouton démarrer
        this.startButton = this.add.text(width / 2, height / 1.5, 'DÉMARRER', {
            fontSize: '48px',
            color: '#4CAF50',
            fontStyle: 'bold',
            backgroundColor: '#000000',
            padding: { x: 20, y: 10 },
        });
        this.startButton.setOrigin(0.5);
        this.startButton.setInteractive({ useHandCursor: true });

        // Status gyroscope
        this.gyroStatus = this.add.text(width / 2, height - 150, '', {
            fontSize: '16px',
            color: '#ffeb3b',
            align: 'center',
            backgroundColor: '#000000',
            padding: { x: 10, y: 10 },
            wordWrap: { width: width - 40 },
        });
        this.gyroStatus.setOrigin(0.5);

        // Click sur le bouton
        this.startButton.on('pointerdown', () => {
            this.requestGyroPermission();
        });

        // Effet hover
        this.startButton.on('pointerover', () => {
            this.startButton?.setScale(1.1);
        });

        this.startButton.on('pointerout', () => {
            this.startButton?.setScale(1);
        });
    }

    /**
     * Demande la permission gyroscope (iOS) et lance le jeu
     */
    private requestGyroPermission(): void {
        // Simplifié : on part immédiatement en jeu, sans attendre la permission.
        this.gyroStatus?.setText('Lancement du jeu...');
        this.startGame();
    }

    /**
     * Lance la scène de jeu
     */
    private startGame(): void {
        if (this.hasStarted || this.scene.isActive('GameScene')) return;
        this.hasStarted = true;
        this.scene.start('GameScene');
    }
}
