// DebugScene.ts: toggled by backtick ` (§13). No UI footprint when closed.
// Functions: set happiness/awareness, set day 1-7, teleport, set all clues, force ending.
import Phaser from 'phaser';
import { director } from '../systems/CorruptionDirector';

export class DebugScene extends Phaser.Scene {
  open = false;
  panel!: Phaser.GameObjects.Container;
  constructor() { super('DebugScene'); }

  create() {
    this.panel = this.add.container(0,0).setDepth(5000).setVisible(false);
    const bg = this.add.rectangle(160, 90, 300, 160, 0x000000, 0.9).setStrokeStyle(1, 0xffffff);
    this.panel.add(bg);
    const title = this.add.text(160, 18, 'DEBUG 面板（` 开关）', { fontFamily:'monospace', fontSize:'9px', color:'#ffd86b' }).setOrigin(0.5,0);
    this.panel.add(title);

    let y = 36;
    const mk = (label: string, onPress: ()=>void) => {
      const t = this.add.text(20, y, label, { fontFamily:'monospace', fontSize:'8px', color:'#bfe3ff', backgroundColor:'#222222', padding:{x:4,y:2} }).setInteractive({ useHandCursor: true });
      t.on('pointerdown', onPress);
      this.panel.add(t);
      y += 12;
    };

    mk('幸福 = 10 (S0)', () => director.debugSetHappiness(10));
    mk('幸福 = 40 (S1)', () => director.debugSetHappiness(40));
    mk('幸福 = 70 (S2)', () => director.debugSetHappiness(70));
    mk('幸福 = 90 (S3)', () => director.debugSetHappiness(90));
    mk('幸福 = 100 (S4)', () => director.debugSetHappiness(100));
    mk('觉知 = 0 / 50 / 100', () => director.debugSetAwareness(director.awareness >= 75 ? 0 : (director.awareness >= 25 ? 100 : 50)));
    mk('置齐全部线索 (C1-C9)', () => director.debugAllClues());
    mk('设当前日 = 1..7', () => director.debugSetDay(director.day >= 7 ? 1 : director.day + 1));
    mk('强制结局 E1 (糖衣)', () => this.forceEnding('E1'));
    mk('强制结局 E2 (回收)', () => this.forceEnding('E2'));
    mk('强制结局 E3 (苏醒)', () => this.forceEnding('E3'));
    mk('强制结局 E4 (静默)', () => this.forceEnding('E4'));

    // teleport buttons
    const scenes = ['house','plaza','bakery','shop','clinic','lake','field','well_area','fog_boundary'];
    const sceneKeys: Record<string,string> = { house:'HouseScene',plaza:'PlazaScene',bakery:'BakeryScene',shop:'ShopScene',clinic:'ClinicScene',lake:'LakeScene',field:'FieldScene',well_area:'WellAreaScene',fog_boundary:'FogBoundaryScene' };
    const teleText = this.add.text(20, y, '传送：', { fontFamily:'monospace', fontSize:'8px', color:'#888' }); this.panel.add(teleText); y += 10;
    let x = 20;
    for (const s of scenes) {
      const t = this.add.text(x, y, s, { fontFamily:'monospace', fontSize:'7px', color:'#ffd86b', backgroundColor:'#222', padding:{x:3,y:2} }).setInteractive({ useHandCursor:true });
      t.on('pointerdown', () => {
        // stop current map scene then start target
        for (const k of Object.values(sceneKeys)) { this.scene.stop(k); }
        this.scene.launch('UIScene');
        this.scene.start(sceneKeys[s], { spawn: 'default' });
      });
      this.panel.add(t); x += t.width + 4;
    }

    this.input.keyboard!.on('keydown', (event: KeyboardEvent) => {
      if (event.code === 'Backquote' || event.key === '`') {
        this.open = !this.open;
        this.panel.setVisible(this.open);
      }
    });
    // pause input propagation when open handled by being on top
  }

  forceEnding(e: 'E1'|'E2'|'E3'|'E4') {
    // stop map scenes
    for (const k of ['HouseScene','PlazaScene','BakeryScene','ShopScene','ClinicScene','LakeScene','FieldScene','WellAreaScene','FogBoundaryScene','WhiteLayerScene']) this.scene.stop(k);
    this.scene.stop('UIScene');
    this.panel.setVisible(false); this.open = false;
    this.scene.start('EndingScene', { ending: e });
  }
}
