import Phaser from 'phaser';

/**
 * BootScene — loads all assets before the game starts.
 * Uses atlas loaded from public/atlas.json (Phaser hash format, converted from Napoletane atlas).
 *
 * [trueref /local/phaser] — this.load.atlas() loads a texture atlas where frames are
 * referenced by name (e.g. 'bastoni_7') using the Phaser hash format JSON.
 */
export class BootScene extends Phaser.Scene {
  constructor() {
    super({ key: 'BootScene' });
  }

  preload(): void {
    const W = this.scale.width;
    const H = this.scale.height;

    // Loading bar
    const bar = this.add.rectangle(W / 2, H / 2, 400, 20, 0x4caf50);
    const barBg = this.add.rectangle(W / 2, H / 2, 402, 22, 0x1a5c2a);
    barBg.setDepth(0);
    bar.setDepth(1);
    bar.setOrigin(0.5);
    bar.scaleX = 0;

    const label = this.add.text(W / 2, H / 2 - 40, 'Caricamento...', {
      fontFamily: 'serif',
      fontSize: '24px',
      color: '#ffffff',
    }).setOrigin(0.5);

    this.load.on('progress', (value: number) => {
      bar.scaleX = value;
    });

    // Load card atlas (Phaser hash format converted from Napoletane atlas)
    this.load.atlas('cards', 'atlas.png', 'atlas.json');

    // Load card back separately
    this.load.image('retro', 'retro.png');
  }

  create(): void {
    this.scene.start('MenuScene');
  }
}
