// FieldScene.ts: 花田. berry bushes (T6 day3), watering point, scattered letters C3 (D2).
import Phaser from 'phaser';
import { MapScene } from '../MapScene';
import { director } from '../../systems/CorruptionDirector';
import { story } from '../../systems/StoryDirector';
import { audio } from '../../systems/AudioEngine';

export class FieldScene extends MapScene {
  constructor() { super('FieldScene', 'map:field', 'field'); }

  onBuild(map: Phaser.Tilemaps.Tilemap, ts: Phaser.Tilemaps.Tileset) {
    const berries = [[4,4],[8,3],[12,5],[6,8],[14,9]];
    berries.forEach(([tx,ty], i) => {
      this.addInteract(tx, ty, () => this.pickBerry(i), {
        promptText: '按 E 采莓果',
        enabled: () => director.day === 3 && !director.tasksDone.has('T6-berry-'+i),
      });
    });
    // watering point
    this.addInteract(2, 11, () => {
      director.smallInteraction('water');
      this.say([{ speaker:'', text:'浇了浇花。' }]);
    }, { promptText: '按 E 浇花' });
    // C3 scattered letters (D2): place an interactable
    this.addInteract(1, 12, () => {
      if (director.day >= 2) {
        const got = director.discoverClue('C3');
        this.say([{ speaker:'', text: got ? '散落的信件……所有邮戳都是同一天。' : '地上的信件，邮戳都一样。' }]);
      } else {
        this.say([{ speaker:'', text:'花田边散落着几封信。' }]);
      }
    }, { promptText: '按 E 捡信件' });
  }

  pickBerry(i: number) {
    director.tasksDone.add('T6-berry-'+i);
    director.smallInteraction('berry');
    audio.sfxBlip();
    const picked = [0,1,2,3,4].filter(k => director.tasksDone.has('T6-berry-'+k)).length;
    this.say([{ speaker:'', text:`采到一颗红红的莓果！(${picked}/5)` }]);
    if (picked >= 5) story.completeTask('T6', 10);
  }
}
