// FogBoundaryScene.ts: 雾墙边界. Before D5 cannot approach (dissuaded). E3 exit.
import Phaser from 'phaser';
import { MapScene } from '../MapScene';
import { director } from '../../systems/CorruptionDirector';

export class FogBoundaryScene extends MapScene {
  gazeTimer = 0;
  constructor() { super('FogBoundaryScene', 'map:fog_boundary', 'fog_boundary'); }

  onBuild(map: Phaser.Tilemaps.Tilemap, ts: Phaser.Tilemaps.Tileset) {
    this.addInteract(12, 4, () => this.fogInteract(), { promptText: '按 E 看雾墙' });
    // E3 exit
    this.addInteract(13, 4, () => this.tryE3Exit(), {
      promptText: '按 E 走进雾墙',
      enabled: () => director.day >= 5 && director.awareness >= 70 && director.clues.size >= 6,
    });
  }

  fogInteract() {
    if (director.day < 5) {
      this.say([
        { speaker:'', text:'你想靠近村庄尽头的雾……' },
        { speaker:'路过的村民', text:'那边走不得呀，那边没有路。回来吧。' },
        { speaker:'', text:'（你被温柔地劝了回来。）' },
      ]);
      return;
    }
    director.gaze('fog');
    this.say([{ speaker:'', text:'雾墙在村庄尽头翻涌，里面好像有什么……白的。' }]);
  }

  tryE3Exit() {
    if (director.awareness >= 70 && director.clues.size >= 6) {
      // go to white layer (E3)
      director.tasksDone.add('T15');
      this.scene.stop('UIScene');
      this.scene.stop('DebugScene');
      this.scene.start('WhiteLayerScene', { spawn: 'default' });
    } else {
      this.say([{ speaker:'', text:'雾墙太厚了，你走不进去……' }]);
    }
  }

  update(time: number, delta: number) {
    super.update(time, delta);
    const near = Phaser.Math.Distance.Between(this.player.x, this.player.y, 12*16+8, 4*16+8) < 24;
    if (near) {
      this.gazeTimer += delta;
      if (this.gazeTimer >= 5000) { this.gazeTimer = 0; director.gaze('fog'); }
    } else this.gazeTimer = 0;
  }
}
