// LakeScene.ts: 湖畔. 龟爷爷 (grandpa - C9 progression), picnic (T7), lake reflection show.
import Phaser from 'phaser';
import { MapScene } from '../MapScene';
import { director } from '../../systems/CorruptionDirector';
import { story } from '../../systems/StoryDirector';
import { NPCS } from '../../data/gamedata';
import { DialogueLine } from '../UIScene';
import { audio } from '../../systems/AudioEngine';

export class LakeScene extends MapScene {
  gpSprite!: Phaser.GameObjects.Sprite;
  reflectionDelay = 0;
  constructor() { super('LakeScene', 'map:lake', 'lake'); }

  onBuild(map: Phaser.Tilemaps.Tilemap, ts: Phaser.Tilemaps.Tileset) {
    this.gpSprite = this.add.sprite(13*16+8, 10*16+8, 'npc', NPCS.grandpa.fieldSprite).setOrigin(0.5,0.9);
    (this.gpSprite as any).npcKey = 'grandpa';
    this.npcSprites.push(this.gpSprite);

    this.addInteract(13, 10, () => {
      if (director.noteNpcTalk('grandpa')) director.sadness('对同一 NPC 一天内对话 ≥5 次（纠缠）', 'grandpa');
      story.recordGreet('grandpa');
      // bread deliver (T1/T8)
      if ((director.day === 1 || director.day === 4) && story.breadTaken[director.day]) {
        story.deliverBread('grandpa', (l) => this.say(l));
        return;
      }
      this.say(story.npcTalk('grandpa'));
    }, { promptText: '按 E 和龟爷爷说话' });

    // picnic (T7 day3)
    this.addInteract(16, 11, () => this.picnic(), { promptText: '按 E 野餐', enabled: () => director.day === 3 && !director.tasksDone.has('T7') });

    // lake reflection (S2+: reflection delays 1 frame)
    this.addInteract(7, 7, () => {
      this.say([{ speaker:'', text:'湖面平静如镜。你看见自己的倒影……好像慢了半拍。' }]);
    }, { promptText: '按 E 看湖面' });
  }

  picnic() {
    story.completeTask('T7', 15);
    audio.sfxDone();
    this.say([
      { speaker:'', text:'铺开野餐布，阳光洒在湖面上。' },
      { speaker:'', text:'（湖面的倒影……比动作慢了一帧。）' },
    ]);
  }
}
