import Phaser from 'phaser';

export class ParticleManager {
  private scene: Phaser.Scene;

  constructor(scene: Phaser.Scene) {
    this.scene = scene;
  }

  // Пыль при приземлении
  emitLandingDust(x: number, y: number): void {
    const particles = this.scene.add.particles(x, y, 'particle_dust', {
      speed: { min: 30, max: 80 },
      angle: { min: -150, max: -30 },
      scale: { start: 1, end: 0 },
      lifespan: 400,
      gravityY: 200,
      quantity: 6,
      emitting: false,
    });

    particles.explode();
    this.scene.time.delayedCall(500, () => particles.destroy());
  }

  // Пыль при беге
  emitRunningDust(x: number, y: number, direction: number): void {
    const particles = this.scene.add.particles(x, y, 'particle_dust', {
      speed: { min: 20, max: 50 },
      angle: direction > 0 ? { min: 150, max: 180 } : { min: 0, max: 30 },
      scale: { start: 0.8, end: 0 },
      lifespan: 300,
      gravityY: 100,
      quantity: 2,
      emitting: false,
    });

    particles.explode();
    this.scene.time.delayedCall(400, () => particles.destroy());
  }

  // Облако при выстреле
  emitShootCloud(x: number, y: number, direction: number): void {
    const particles = this.scene.add.particles(x, y, 'particle_smoke', {
      speed: { min: 50, max: 100 },
      angle: direction > 0 ? { min: -30, max: 30 } : { min: 150, max: 210 },
      scale: { start: 0.8, end: 0 },
      alpha: { start: 0.6, end: 0 },
      lifespan: 300,
      quantity: 4,
      emitting: false,
    });

    particles.explode();
    this.scene.time.delayedCall(400, () => particles.destroy());
  }

  // Звёзды при оглушении врага
  emitStunStars(x: number, y: number): void {
    const particles = this.scene.add.particles(x, y - 20, 'particle_star', {
      speed: { min: 50, max: 100 },
      angle: { min: -180, max: 180 },
      scale: { start: 1, end: 0.3 },
      lifespan: 500,
      quantity: 5,
      emitting: false,
    });

    particles.explode();
    this.scene.time.delayedCall(600, () => particles.destroy());
  }

  // Искры монеты при сборе
  emitCoinSparkle(x: number, y: number): void {
    const particles = this.scene.add.particles(x, y, 'particle_spark', {
      speed: { min: 80, max: 150 },
      angle: { min: 0, max: 360 },
      scale: { start: 1.2, end: 0 },
      lifespan: 400,
      quantity: 8,
      emitting: false,
    });

    particles.explode();
    this.scene.time.delayedCall(500, () => particles.destroy());
  }

  // Взрыв при смерти игрока
  emitDeathExplosion(x: number, y: number): void {
    // Основные частицы
    const particles = this.scene.add.particles(x, y, 'particle', {
      speed: { min: 100, max: 250 },
      angle: { min: 0, max: 360 },
      scale: { start: 1.5, end: 0 },
      lifespan: 600,
      gravityY: 300,
      quantity: 15,
      tint: [0xff4444, 0xff8888, 0xffaaaa],
      emitting: false,
    });

    particles.explode();

    // Дым
    const smoke = this.scene.add.particles(x, y, 'particle_smoke', {
      speed: { min: 30, max: 80 },
      angle: { min: -120, max: -60 },
      scale: { start: 1.5, end: 0 },
      alpha: { start: 0.5, end: 0 },
      lifespan: 800,
      quantity: 6,
      emitting: false,
    });

    smoke.explode();

    this.scene.time.delayedCall(900, () => {
      particles.destroy();
      smoke.destroy();
    });
  }

  // След снаряда (возвращает эмиттер для привязки к объекту)
  createProjectileTrail(): Phaser.GameObjects.Particles.ParticleEmitter {
    const particles = this.scene.add.particles(0, 0, 'particle', {
      speed: 0,
      scale: { start: 0.8, end: 0 },
      alpha: { start: 0.6, end: 0 },
      lifespan: 200,
      frequency: 30,
      tint: 0x88ff88,
    });

    return particles;
  }
}
