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();
        });

        // Charger les sprites des deux joueurs
        // User1 (Baptiste) - dimensions réelles du spritesheet 93x224px
        this.load.spritesheet('player_user1', 'assets/sprites/user1/player_spritesheet.png', {
            frameWidth: 93,
            frameHeight: 224,
        });
        this.load.image('player_user1_walk_1', 'assets/sprites/user1/walk_1.png');
        this.load.image('player_user1_walk_2', 'assets/sprites/user1/walk_2.png');
        this.load.image('player_user1_walk_3', 'assets/sprites/user1/walk_3.png');
        this.load.image('player_user1_walk_4', 'assets/sprites/user1/walk_4.png');
        this.load.image('player_user1_jump_1', 'assets/sprites/user1/jump_1.png');
        this.load.image('player_user1_jump_2', 'assets/sprites/user1/jump_2.png');
        this.load.image('player_user1_jump_3', 'assets/sprites/user1/jump_3.png');
        this.load.image('player_user1_jump_4', 'assets/sprites/user1/jump_4.png');
        this.load.image('player_user1_jump_5', 'assets/sprites/user1/jump_5.png');

        // User2 (Julien) - dimensions réelles du spritesheet
        this.load.spritesheet('player_user2', 'assets/sprites/user2/player_spritesheet.png', {
            frameWidth: 93,
            frameHeight: 224,
        });
        this.load.image('player_user2_walk_1', 'assets/sprites/user2/walk_1.png');
        this.load.image('player_user2_walk_2', 'assets/sprites/user2/walk_2.png');
        this.load.image('player_user2_walk_3', 'assets/sprites/user2/walk_3.png');
        this.load.image('player_user2_walk_4', 'assets/sprites/user2/walk_4.png');
        this.load.image('player_user2_jump_1', 'assets/sprites/user2/jump_1.png');
        this.load.image('player_user2_jump_2', 'assets/sprites/user2/jump_2.png');
        this.load.image('player_user2_jump_3', 'assets/sprites/user2/jump_3.png');
        this.load.image('player_user2_jump_4', 'assets/sprites/user2/jump_4.png');
        this.load.image('player_user2_jump_5', 'assets/sprites/user2/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');
        this.load.audio('sfx_chien', 'assets/audio/chien.mp3');

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

        // Sprites plateformes (blocs de différentes tailles)
        this.load.image('platform_40', 'assets/sprites/decor/b_40.png');
        this.load.image('platform_73', 'assets/sprites/decor/b_73.png');
        this.load.image('platform_130', 'assets/sprites/decor/b_130.png');
        this.load.image('platform_140', 'assets/sprites/decor/b_140.png');
        this.load.image('platform_179', 'assets/sprites/decor/b_179.png');

        // Sol (vert)
        this.load.image('ground_tile', 'assets/sprites/decor/sol_150.png');

        // Sprites objets de jeu
        this.load.image('gift_sprite', 'assets/sprites/decor/gift_40.png');
        this.load.image('star_sprite', 'assets/sprites/decor/star_40.png');

        // Selection joueur (portrait)
        this.load.image('user1', 'assets/images/user1.jpg');
        this.load.image('user2', 'assets/images/user2.jpg');

        // Images finales (affichées après la vidéo de fin)
        this.load.image('finale_user1', 'assets/sprites/user1/image finale1.jpeg');
        this.load.image('finale_user2', 'assets/sprites/user2/image finale2.jpeg');

        // Vidéos d'intro (mp4 uniquement) - une par joueur
        // Le 3e paramètre 'noAudio' est à false pour garder l'audio si présent
        // Ajout d'un timestamp pour forcer le rechargement (éviter le cache)
        const timestamp = Date.now();
        this.load.video('intro_user1', `assets/video/intro_user1.mp4?v=${timestamp}`, false);
        this.load.video('intro_user2', `assets/video/intro_user2.mp4?v=${timestamp}`, false);

        // Vidéo de fin (quand le joueur gagne)
        this.load.video('end', `assets/video/zoe cadeaux1.mp4?v=${timestamp}`, false);

        // Sprites du chien
        this.load.image('dog-idle-1', 'assets/sprites/dog/idle_01.png');
        this.load.image('dog-idle-2', 'assets/sprites/dog/idle_02.png');
        this.load.image('dog-idle-3', 'assets/sprites/dog/idle_03.png');
        this.load.image('dog-idle-4', 'assets/sprites/dog/idle_04.png');
        this.load.image('dog-walkback-1', 'assets/sprites/dog/walkback_01.png');
        this.load.image('dog-walkback-2', 'assets/sprites/dog/walkback_02.png');
        this.load.image('dog-walkback-3', 'assets/sprites/dog/walkback_03.png');
        this.load.image('dog-walkback-4', 'assets/sprites/dog/walkback_04.png');
        this.load.image('dog-turn-1', 'assets/sprites/dog/turn_01.png');
        this.load.image('dog-turn-2', 'assets/sprites/dog/turn_02.png');
        this.load.image('dog-turn-3', 'assets/sprites/dog/turn_03.png');

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

    create(): void {
        // Créer les animations du chien
        this.createDogAnimations();

        // Demarrer par la selection joueur (portrait sur mobile)
        this.scene.start('PlayerSelectScene');
    }

    /**
     * Crée les animations du chien
     */
    private createDogAnimations(): void {
        // Animation IDLE (queue qui remue)
        this.anims.create({
            key: 'dog-idle',
            frames: [
                { key: 'dog-idle-1' },
                { key: 'dog-idle-2' },
                { key: 'dog-idle-3' },
                { key: 'dog-idle-4' }
            ],
            frameRate: 6,
            repeat: -1
        });

        // Animation WALK BACK (marche vers la gauche)
        this.anims.create({
            key: 'dog-walkback',
            frames: [
                { key: 'dog-walkback-1' },
                { key: 'dog-walkback-2' },
                { key: 'dog-walkback-3' },
                { key: 'dog-walkback-4' }
            ],
            frameRate: 8,
            repeat: -1
        });

        // Animation TURN (demi-tour)
        this.anims.create({
            key: 'dog-turn',
            frames: [
                { key: 'dog-turn-1' },
                { key: 'dog-turn-2' },
                { key: 'dog-turn-3' }
            ],
            frameRate: 10,
            repeat: 0
        });
    }
}
