import Phaser from 'phaser';
import { SCENE_KEYS } from '../data/constants';
import { Director } from '../systems/CorruptionDirector';

export class DreamScene extends Phaser.Scene {
  constructor() {
    super(SCENE_KEYS.DREAM);
  }

  create(): void {
    const { width, height } = this.cameras.main;
    this.cameras.main.setBackgroundColor('#1a1030');

    const stage = Director.stage;
    let text = '甜美的梦……';
    if (stage === 'S2') text = '梦里有什么声音……很轻。';
    if (stage === 'S3') text = '白色的墙一闪而过。';
    if (stage === 'S4' || Director.happiness >= 100) text = '……';

    this.add
      .text(width / 2, height / 2 - 10, text, {
        fontFamily: 'monospace',
        fontSize: '12px',
        color: '#e0d0ff',
      })
      .setOrigin(0.5);

    // S3+ flash white ward 1s first time high happiness night
    if (Director.happiness >= 81 || stage === 'S3' || stage === 'S4') {
      this.time.delayedCall(600, () => {
        this.cameras.main.setBackgroundColor('#e8e8f0');
        this.add
          .text(width / 2, height / 2 + 20, '白色走廊', {
            fontFamily: 'monospace',
            fontSize: '10px',
            color: '#808090',
          })
          .setOrigin(0.5);
      });
      this.time.delayedCall(1600, () => this.finish());
    } else {
      this.time.delayedCall(1200, () => this.finish());
    }
  }

  private finish(): void {
    Director.advanceDay();
    this.scene.start(SCENE_KEYS.HOUSE, { spawn: 'spawn' });
    if (!this.scene.isActive(SCENE_KEYS.UI)) {
      this.scene.launch(SCENE_KEYS.UI);
    }
  }
}
