// BootScene.ts: load all assets then go to Title.
import Phaser from 'phaser';
import mapsMeta from '../data/mapsMeta';

export class BootScene extends Phaser.Scene {
  constructor() { super('BootScene'); }

  preload() {
    this.load.tilemapTiledJSON('map:house', 'assets/maps/house.json');
    this.load.tilemapTiledJSON('map:plaza', 'assets/maps/plaza.json');
    this.load.tilemapTiledJSON('map:bakery', 'assets/maps/bakery.json');
    this.load.tilemapTiledJSON('map:shop', 'assets/maps/shop.json');
    this.load.tilemapTiledJSON('map:clinic', 'assets/maps/clinic.json');
    this.load.tilemapTiledJSON('map:lake', 'assets/maps/lake.json');
    this.load.tilemapTiledJSON('map:field', 'assets/maps/field.json');
    this.load.tilemapTiledJSON('map:well_area', 'assets/maps/well_area.json');
    this.load.tilemapTiledJSON('map:fog_boundary', 'assets/maps/fog_boundary.json');
    this.load.tilemapTiledJSON('map:white_layer', 'assets/maps/white_layer.json');

    this.load.image('tileset', 'assets/tilesets/tileset.png');
    this.load.spritesheet('player', 'assets/sprites/player.png', { frameWidth: 16, frameHeight: 24 });
    this.load.spritesheet('npc', 'assets/sprites/npc.png', { frameWidth: 16, frameHeight: 24 });
    this.load.spritesheet('portraits', 'assets/sprites/portraits.png', { frameWidth: 24, frameHeight: 24 });

    // small font-like rendering uses default; no external font.
  }

  create() {
    // build player animations once
    const dirs = ['down','up','right','left'] as const;
    dirs.forEach((dir, row) => {
      this.anims.create({ key: `walk-${dir}`, frames: this.anims.generateFrameNumbers('player', { start: row*4, end: row*4+3 }), frameRate: 8, repeat: -1 });
      // idle = first frame of the row
      this.anims.create({ key: `idle-${dir}`, frames: [ { key:'player', frame: row*4 } ], frameRate: 1 });
    });
    // twin bird flap
    this.anims.create({ key: 'bird-flap', frames: this.anims.generateFrameNumbers('npc', { frames:[15,16] }), frameRate: 4, repeat: -1 });

    this.scene.start('TitleScene');
  }
}

export const MAP_KEYS: Record<string,string> = {
  house: 'map:house', plaza: 'map:plaza', bakery: 'map:bakery', shop: 'map:shop',
  clinic: 'map:clinic', lake: 'map:lake', field: 'map:field', well_area: 'map:well_area',
  fog_boundary: 'map:fog_boundary', white_layer: 'map:white_layer',
};

export const SCENE_KEYS: Record<string,string> = {
  house: 'HouseScene', plaza: 'PlazaScene', bakery: 'BakeryScene', shop: 'ShopScene',
  clinic: 'ClinicScene', lake: 'LakeScene', field: 'FieldScene', well_area: 'WellAreaScene',
  fog_boundary: 'FogBoundaryScene', white_layer: 'WhiteLayerScene',
};
