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

export class InternTower extends TowerBase {
  private selfDestroyTimer: number = 0
  private readonly SELF_DESTROY_INTERVAL = 1000
  private destroyed: boolean = false
  public onSelfDestroy?: (tower: InternTower) => void

  constructor(scene: Phaser.Scene, gridX: number, gridY: number) {
    // attackRange=1.5：近战覆盖相邻格，避免格子边界浮点漏攻击
    super(scene, gridX, gridY, 50, 1.5, 15, 1.5, 'tower-intern')
  }

  override update(delta: number, enemies: EnemyBase[]): void {
    if (this.destroyed) return
    super.update(delta, enemies)
    this.selfDestroyTimer += delta
    if (this.selfDestroyTimer >= this.SELF_DESTROY_INTERVAL) {
      this.selfDestroyTimer -= this.SELF_DESTROY_INTERVAL
      if (Math.random() < 0.01) {
        GameManager.getInstance().addHC(25)
        this.showMessage('实习生跑路！+25HC', '#22C55E')
        this.destroyed = true
        this.onSelfDestroy?.(this)
        this.destroy()
        return
      }
    }
  }

  attack(target: EnemyBase): void {
    if (Math.random() < 0.05 && target.hp < 500) {
      target.takeDamage(9999)
      this.showMessage('整顿职场！秒杀！', '#A3E635')
    } else {
      target.takeDamage(this.attackDamage)
    }
    this.showMeleeEffect(target)
  }

  private showMeleeEffect(target: EnemyBase): void {
    const g = this.scene.add.graphics()
    g.fillStyle(0x22c55e, 0.6)
    g.fillCircle(target.x, target.y, 12)
    g.setDepth(15)
    this.scene.time.delayedCall(150, () => g.destroy())
  }

  private showMessage(msg: string, color: string): void {
    const txt = this.scene.add
      .text(this.px, this.py - 30, msg, {
        fontFamily: 'VT323, monospace', fontSize: '14px',
        color, backgroundColor: '#052e16', padding: { x: 4, y: 2 },
      }).setOrigin(0.5, 1).setDepth(20)
    this.scene.tweens.add({
      targets: txt, y: this.py - 55, alpha: 0,
      duration: 1200, onComplete: () => txt.destroy(),
    })
  }
}
