// WellAreaScene.ts: 古井空地. 古井 (C7 from D5). Before D5, gentle NPC dissuades.
import Phaser from 'phaser';
import { MapScene } from '../MapScene';
import { director } from '../../systems/CorruptionDirector';
import { audio } from '../../systems/AudioEngine';

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

  onBuild(map: Phaser.Tilemaps.Tilemap, ts: Phaser.Tilemaps.Tileset) {
    this.addInteract(6, 4, () => this.wellInteract(), { promptText: '按 E 看古井' });
  }

  wellInteract() {
    if (director.day < 5) {
      // gentle dissuade (NPC leads away)
      this.say([
        { speaker:'路过的村民', text:'哎呀，别靠近那口井呀。那里没什么好看的。' },
        { speaker:'', text:'（你被温柔地引开了。）' },
      ]);
      return;
    }
    // D5+: interact 5s to hear faint heartbeat echo (C7)
    this.say([{ speaker:'', text:'你俯身倾听井底……（保持 5 秒）' }], () => {
      // begin gaze: count 5s of proximity handled in update; here grant immediately for simplicity of clue
      director.gaze('well');
      const got = director.discoverClue('C7');
      this.say([{ speaker:'', text: got ? '井底回声里，有极轻的……监护仪的滴声。' : '井底的回声还是那样。' }]);
      audio.sfxHeartbeat();
    });
  }

  update(time: number, delta: number) {
    super.update(time, delta);
    // gaze: standing near well for 5s counts once per day
    const near = Phaser.Math.Distance.Between(this.player.x, this.player.y, 6*16+8, 4*16+8) < 24;
    if (near) {
      this.gazeTimer += delta;
      if (this.gazeTimer >= 5000) { this.gazeTimer = 0; director.gaze('well'); }
    } else this.gazeTimer = 0;
  }
}
