import Phaser from "phaser";
import type { HudController } from "../../ui/hud";
import { ensureBackgroundMusic } from "../audio/backgroundMusic";
import { createMenuBackdrop, createMenuButton } from "./menuVisuals";

interface MenuKeys {
  enter: Phaser.Input.Keyboard.Key;
  space: Phaser.Input.Keyboard.Key;
  l: Phaser.Input.Keyboard.Key;
  e: Phaser.Input.Keyboard.Key;
}

export class MainMenuScene extends Phaser.Scene {
  private readonly hud: HudController;

  private keys!: MenuKeys;

  constructor(hud: HudController) {
    super("main-menu");
    this.hud = hud;
  }

  create(): void {
    this.hud.setVisible(false);
    ensureBackgroundMusic(this);
    createMenuBackdrop(this);

    const width = this.scale.width;
    const height = this.scale.height;
    const subtitleY = 186;
    const hintY = height - 24;
    const buttonWidth = 340;
    const buttonHeight = 72;
    const buttonGap = 14;
    const buttonCount = 3;
    const buttonBlockHeight = buttonCount * buttonHeight + (buttonCount - 1) * buttonGap;
    const buttonAreaTop = subtitleY + 34;
    const buttonAreaBottom = hintY - 42;
    const buttonAreaHeight = buttonAreaBottom - buttonAreaTop;
    const buttonBlockTop = buttonAreaTop + (buttonAreaHeight - buttonBlockHeight) / 2;
    const firstButtonY = buttonBlockTop + buttonHeight / 2;

    const titleGlow = this.add.text(width / 2, 118, "Deltanism", {
      fontFamily: '"Chakra Petch", "Trebuchet MS", sans-serif',
      fontSize: "86px",
      color: "#98f8ff"
    }).setOrigin(0.5);
    titleGlow.setAlpha(0.2);
    titleGlow.setBlendMode(Phaser.BlendModes.ADD);

    const title = this.add.text(width / 2, 112, "Deltanism", {
      fontFamily: '"Chakra Petch", "Trebuchet MS", sans-serif',
      fontSize: "76px",
      color: "#d8feff",
      stroke: "#5de8ff",
      strokeThickness: 2
    }).setOrigin(0.5);

    this.add.text(width / 2, subtitleY, "Лазерная головоломка с зеркалами, призмами и порталами", {
      fontFamily: '"Chakra Petch", "Trebuchet MS", sans-serif',
      fontSize: "24px",
      color: "#8fd5e4"
    }).setOrigin(0.5);

    scenePulse(this, titleGlow, 0.2, 0.4, 980);
    this.tweens.add({
      targets: title,
      scaleX: 1.015,
      scaleY: 1.015,
      duration: 1500,
      ease: "Sine.easeInOut",
      yoyo: true,
      repeat: -1
    });

    createMenuButton(this, {
      x: width / 2,
      y: firstButtonY,
      width: buttonWidth,
      height: buttonHeight,
      title: "Новая Игра",
      subtitle: "Старт с первого уровня",
      onClick: () => this.scene.start("game", { startLevelIndex: 0 })
    });

    createMenuButton(this, {
      x: width / 2,
      y: firstButtonY + buttonHeight + buttonGap,
      width: buttonWidth,
      height: buttonHeight,
      title: "Выбор Уровня",
      subtitle: "Перейти к любому уровню",
      onClick: () => this.scene.start("level-select")
    });

    createMenuButton(this, {
      x: width / 2,
      y: firstButtonY + (buttonHeight + buttonGap) * 2,
      width: buttonWidth,
      height: buttonHeight,
      title: "Редактор Уровня",
      subtitle: "Создание и экспорт TS-кода",
      onClick: () => this.scene.start("level-editor")
    });

    const hint = this.add.text(width / 2, hintY, "Enter/Space - игра, L - уровни, E - редактор", {
      fontFamily: '"Chakra Petch", "Trebuchet MS", sans-serif',
      fontSize: "18px",
      color: "#7ec6d4"
    }).setOrigin(0.5);
    scenePulse(this, hint, 0.52, 1, 680);

    this.keys = this.input.keyboard?.addKeys({
      enter: Phaser.Input.Keyboard.KeyCodes.ENTER,
      space: Phaser.Input.Keyboard.KeyCodes.SPACE,
      l: Phaser.Input.Keyboard.KeyCodes.L,
      e: Phaser.Input.Keyboard.KeyCodes.E
    }) as MenuKeys;
  }

  update(): void {
    if (Phaser.Input.Keyboard.JustDown(this.keys.enter) || Phaser.Input.Keyboard.JustDown(this.keys.space)) {
      this.scene.start("game", { startLevelIndex: 0 });
      return;
    }
    if (Phaser.Input.Keyboard.JustDown(this.keys.l)) {
      this.scene.start("level-select");
      return;
    }
    if (Phaser.Input.Keyboard.JustDown(this.keys.e)) {
      this.scene.start("level-editor");
    }
  }
}

function scenePulse(
  scene: Phaser.Scene,
  target: Phaser.GameObjects.GameObject,
  from: number,
  to: number,
  duration: number
): void {
  scene.tweens.add({
    targets: target,
    alpha: { from, to },
    duration,
    ease: "Sine.easeInOut",
    repeat: -1,
    yoyo: true
  });
}
