import Phaser from 'phaser';
import { SCENE_KEYS } from '../data/constants';
import { audio } from '../audio/ProceduralAudio';

export class TitleScene extends Phaser.Scene {
  constructor() {
    super(SCENE_KEYS.TITLE);
  }

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

    this.add
      .text(width / 2, 48, '崩坏童话', {
        fontFamily: 'monospace',
        fontSize: '20px',
        color: '#fff8e8',
        stroke: '#c06030',
        strokeThickness: 3,
      })
      .setOrigin(0.5);

    this.add
      .text(width / 2, 72, '蜜糖村', {
        fontFamily: 'monospace',
        fontSize: '28px',
        color: '#ffe08a',
        stroke: '#d07030',
        strokeThickness: 4,
      })
      .setOrigin(0.5);

    this.add
      .text(width / 2, 110, '按 E 或 空格 开始', {
        fontFamily: 'monospace',
        fontSize: '10px',
        color: '#fff0d0',
      })
      .setOrigin(0.5);

    this.add
      .text(width / 2, 150, 'WASD / 方向键移动 · E 交互 · ` Debug', {
        fontFamily: 'monospace',
        fontSize: '8px',
        color: '#d0e8ff',
      })
      .setOrigin(0.5);

    // Decorative sunflower-ish pixels
    for (let i = 0; i < 8; i++) {
      const a = (i / 8) * Math.PI * 2;
      this.add.circle(
        width / 2 + Math.cos(a) * 40,
        100 + Math.sin(a) * 12,
        3,
        0xffd040,
      );
    }
    this.add.circle(width / 2, 100, 5, 0xc08030);

    const start = () => {
      void audio.unlock();
      this.input.keyboard?.off('keydown-E', start);
      this.input.keyboard?.off('keydown-SPACE', start);
      this.scene.start(SCENE_KEYS.NAME);
    };
    this.input.keyboard?.on('keydown-E', start);
    this.input.keyboard?.on('keydown-SPACE', start);
  }
}
