import Phaser from 'phaser';
import { BootScene } from './scenes/shared/BootScene';
import { ParadeScene } from './scenes/shared/ParadeScene';
import { NightScene } from './scenes/door/NightScene';
import { DoorScene } from './scenes/door/DoorScene';
import { NightSummaryScene } from './scenes/door/NightSummaryScene';
import { IncidentReportScene } from './scenes/door/IncidentReportScene';
import { FloorDemoScene } from './scenes/floor/FloorDemoScene';
import { JuiceDemoScene } from './ui/JuiceDemoScene';
import { MixDeskScene } from './ui/MixDeskScene';

// TODO(integration): Phase 2 owns scene routing (menu -> roster -> night). For
// v0.1 the game IS the Door night, so it boots straight into it. The demo scenes
// stay reachable via dev routes below.
const game = new Phaser.Game({
  type: Phaser.AUTO,
  width: 640,
  height: 360,
  pixelArt: true,
  backgroundColor: '#0a0a12',
  scale: {
    mode: Phaser.Scale.FIT,
    autoCenter: Phaser.Scale.CENTER_BOTH,
    zoom: Phaser.Scale.MAX_ZOOM,
  },
  // FloorDemoScene registers twice: 'Floor' is the night-mode instance NightScene
  // launches with a NightContext; 'FloorDemo' stays the standalone demo (key F).
  scene: [
    NightScene,
    DoorScene,
    NightSummaryScene,
    IncidentReportScene,
    new FloorDemoScene('Floor'),
    BootScene,
    ParadeScene,
    new FloorDemoScene('FloorDemo'),
    JuiceDemoScene,
    MixDeskScene,
  ],
});

// debug handle (harmless in prod; used by dev tooling)
(window as unknown as { game: Phaser.Game }).game = game;

// TODO(integration): dev routing until Phase 2's shell exists.
// Keys: N night (the game) · P parade · F floor demo · J juice demo.
// URL hashes #night/#parade/#floor/#juice pick the boot scene.
// NightScene owns DoorScene/NightSummaryScene as children; stopping Night stops them.
const ROUTES = { night: 'Night', parade: 'Parade', floor: 'FloorDemo', juice: 'JuiceDemo' } as const;
type RouteKey = keyof typeof ROUTES;

const showScene = (target: (typeof ROUTES)[RouteKey]): void => {
  for (const key of ['Night', 'Door', 'NightSummary', 'Parade', 'FloorDemo', 'JuiceDemo']) {
    if (key !== target && game.scene.isActive(key)) game.scene.stop(key);
  }
  if (!game.scene.isActive(target)) game.scene.start(target);
};

// NightScene is scene[0] and auto-starts. Hash routing must wait until it is
// actually up before swapping, or both scenes end up running on top of each other.
const hash = window.location.hash.slice(1) as RouteKey;
if (hash in ROUTES && ROUTES[hash] !== 'Night') {
  const route = (): void => {
    if (!game.scene.isActive('Night')) return;
    game.events.off(Phaser.Core.Events.POST_STEP, route);
    showScene(ROUTES[hash]);
  };
  game.events.on(Phaser.Core.Events.POST_STEP, route);
}

window.addEventListener('keydown', (e) => {
  const k = e.key.toLowerCase();
  if (k === 'n') showScene('Night');
  else if (k === 'p') showScene('Parade');
  else if (k === 'f') showScene('FloorDemo');
  else if (k === 'j') showScene('JuiceDemo');
  else if (k === 'm') {
    // The mix desk OVERLAYS whatever is running (it attaches to the active
    // engine), so the ear pass can happen during a real night. Toggle, not route.
    if (game.scene.isActive('MixDesk')) game.scene.stop('MixDesk');
    else {
      game.scene.run('MixDesk');
      game.scene.bringToTop('MixDesk');
    }
  }
});
