import type Phaser from 'phaser'
import { TowerBase } from './TowerBase'
import type { EnemyBase } from '../enemies/EnemyBase'

export class PPTMasterTower extends TowerBase {
  constructor(scene: Phaser.Scene, gridX: number, gridY: number) {
    super(scene, gridX, gridY, 100, 3, 5, 1.5)
    this.drawSprite()
  }

  drawSprite(): void {
    if (!this.sprite) return
    this.sprite.clear()
    // 橙色圆形
    this.sprite.fillStyle(0xf59e0b, 1)
    this.sprite.fillCircle(0, 0, 16)
    // 圆心白点
    this.sprite.fillStyle(0xffffff, 0.9)
    this.sprite.fillCircle(0, 0, 5)
    this.sprite.setPosition(this.px, this.py)
    this.sprite.setDepth(10)
  }

  attack(target: EnemyBase): void {
    // AOE 攻击：对射程内所有怪物造成伤害 + 减速
    this.showAoeEffect()
  }

  /** 该方法由 TowerManager 调用，传入全体敌人 */
  attackAoe(enemies: EnemyBase[]): void {
    const rangePx = this.attackRange * 80 // TILE_SIZE
    this.showAoeEffect()
    for (const e of enemies) {
      if (e.isDead) continue
      const dx = e.sprite.x - this.px
      const dy = e.sprite.y - this.py
      const dist = Math.sqrt(dx * dx + dy * dy)
      if (dist <= rangePx) {
        e.takeDamage(this.attackDamage)
        // 黑话领域：减速40%持续2秒
        e.addSlow(0.4, 2000)
      }
    }
  }

  private showAoeEffect(): void {
    const g = this.scene.add.graphics()
    g.lineStyle(2, 0xf59e0b, 0.8)
    g.strokeCircle(this.px, this.py, this.attackRange * 80)
    g.setDepth(12)
    this.scene.tweens.add({
      targets: g,
      alpha: 0,
      scaleX: 1.2,
      scaleY: 1.2,
      duration: 400,
      onComplete: () => g.destroy(),
    })
  }
}
