import Phaser from 'phaser';

/**
 * Scène de démarrage - Charge les assets de base
 */
export class BootScene extends Phaser.Scene {
    constructor() {
        super({ key: 'BootScene' });
    }

    preload(): void {
        // Barre de chargement
        const width = this.cameras.main.width;
        const height = this.cameras.main.height;

        const progressBar = this.add.graphics();
        const progressBox = this.add.graphics();
        progressBox.fillStyle(0x222222, 0.8);
        progressBox.fillRect(width / 2 - 160, height / 2 - 30, 320, 50);

        const loadingText = this.make.text({
            x: width / 2,
            y: height / 2 - 50,
            text: 'Chargement...',
            style: {
                font: '20px Arial',
                color: '#ffffff',
            },
        });
        loadingText.setOrigin(0.5, 0.5);

        // Progression
        this.load.on('progress', (value: number) => {
            progressBar.clear();
            progressBar.fillStyle(0x4CAF50, 1);
            progressBar.fillRect(width / 2 - 150, height / 2 - 20, 300 * value, 30);
        });

        this.load.on('complete', () => {
            progressBar.destroy();
            progressBox.destroy();
            loadingText.destroy();
        });

        // Sprites du joueur (80x169, 1 frame pour l'instant)
        this.load.spritesheet('player', 'assets/sprites/player_spritesheet.png', {
            frameWidth: 80,
            frameHeight: 169,
        });
        // Frames de marche (sprite individuel)
        this.load.image('player_walk_1', 'assets/sprites/walk_1.png');
        this.load.image('player_walk_2', 'assets/sprites/walk_2.png');
        this.load.image('player_walk_3', 'assets/sprites/walk_3.png');
        this.load.image('player_walk_4', 'assets/sprites/walk_4.png');
        this.load.image('player_jump_1', 'assets/sprites/jump_1.png');
        this.load.image('player_jump_2', 'assets/sprites/jump_2.png');
        this.load.image('player_jump_3', 'assets/sprites/jump_3.png');
        this.load.image('player_jump_4', 'assets/sprites/jump_4.png');
        this.load.image('player_jump_5', 'assets/sprites/jump_5.png');

        // Musique de fond
        this.load.audio('bgm', 'assets/audio/01. Ground Theme.mp3');

        // Effets sonores
        this.load.audio('sfx_jump', 'assets/audio/saut.mp3');
        this.load.audio('sfx_piece', 'assets/audio/piece.mp3');
        this.load.audio('sfx_powerup', 'assets/audio/power-up.mp3');
        this.load.audio('sfx_gameover', 'assets/audio/game-over.mp3');
        this.load.audio('sfx_levelcomplete', 'assets/audio/niveau-termine.mp3');
        this.load.audio('sfx_tuyau', 'assets/audio/tuyau.mp3');
        // Charger en priorité le MP3, mais accepter AIFF en fallback si présent
        this.load.audio('sfx_hit', ['assets/audio/champignon.mp3', 'assets/audio/champignon.aiff']);
        this.load.audio('sfx_super', 'assets/audio/super_tresor.mp3');
        this.load.audio('sfx_saute_champi', 'assets/audio/saute_champi.mp3');

        // Sprites obstacles
        this.load.image('obstacle_mushroom', 'assets/sprites/champignon.png');

        // Vidéo d'intro (mp4 uniquement)
        // Le 3e paramètre 'noAudio' est à false pour garder l'audio si présent
        this.load.video('intro', 'assets/video/intro.mp4', false);

        // TODO: Charger d'autres sprites, backgrounds, sons, etc.
    }

    create(): void {
        // Passer par l'intro vidéo puis le menu
        this.scene.start('IntroScene');
    }
}
