import type Phaser from 'phaser'
import { EnemyBase, type PathPoint } from './EnemyBase'
import { getRandomQuote } from '../data/quotes'
import { AudioEngine } from '../AudioEngine'

export class BossVP extends EnemyBase {
  private skillTimer: number = 20000
  private onDestroyTower?: () => void
  private bossLabel!: Phaser.GameObjects.Text

  constructor(
    scene: Phaser.Scene,
    pathPoints: PathPoint[],
    onDestroyTower?: () => void,
    speedMultiplier: number = 1.0,
    hpMultiplier: number = 1.0
  ) {
    super(scene, pathPoints, 800, 40, 30, 150, 'enemy-boss', speedMultiplier, hpMultiplier)
    this.onDestroyTower = onDestroyTower
    const bossSize = this.cellW * 1.3
    this.imageSprite.setDisplaySize(bossSize, bossSize)
    this.imageSprite.setDepth(12)
    scene.cameras.main.flash(800, 255, 0, 0, false)
    // Boss 登场音效
    AudioEngine.getInstance().playBossAppear()
    this.showBossAlert()
    this.bossLabel = scene.add.text(this.x, this.y + this.cellH * 0.5, '空降VP', {
      fontFamily: 'VT323, monospace', fontSize: '14px',
      color: '#FBBF24', backgroundColor: '#7c2d12', padding: { x: 4, y: 1 },
    }).setOrigin(0.5, 0).setDepth(15)
  }

  private showBossAlert(): void {
    const alert = this.scene.add
      .text(this.scene.scale.width / 2, this.scene.scale.height / 2, '空降VP来袭', {
        fontFamily: 'VT323, monospace', fontSize: '36px',
        color: '#FBBF24', backgroundColor: '#7F1D1D', padding: { x: 16, y: 8 },
      }).setOrigin(0.5, 0.5).setDepth(50)
    this.scene.tweens.add({
      targets: alert, alpha: 0, duration: 2500, delay: 500,
      onComplete: () => alert.destroy(),
    })
  }

  override update(delta: number): void {
    if (this.isDead) return
    super.update(delta)
    this.skillTimer -= delta
    if (this.skillTimer <= 0) {
      this.skillTimer = 20000
      this.triggerOrgRestructure()
    }
    if (this.bossLabel) {
      this.bossLabel.setPosition(this.x, this.y + this.cellH * 0.5)
    }
    this.imageSprite.setTint(this.skillTimer < 3000 ? 0xff6600 : 0xfbbf24)
  }

  private triggerOrgRestructure(): void {
    this.onDestroyTower?.()
    // Boss 技能音效
    AudioEngine.getInstance().playBossSkill()
    const txt = this.scene.add
      .text(this.x, this.y - 40, '组织架构调整！', {
        fontFamily: 'VT323, monospace', fontSize: '18px',
        color: '#FBBF24', backgroundColor: '#7C2D12', padding: { x: 6, y: 3 },
      }).setOrigin(0.5, 1).setDepth(25)
    this.scene.tweens.add({
      targets: txt, y: this.y - 70, alpha: 0,
      duration: 2000, onComplete: () => txt.destroy(),
    })
  }

  override destroy(): void {
    this.bossLabel?.destroy()
    super.destroy()
  }

  getQuote(): string { return getRandomQuote('BossVP') }
}
