import Phaser from 'phaser';

export type PowerUpType = 'shield' | 'speed' | 'slowmo';

export abstract class BasePowerUp extends Phaser.Physics.Arcade.Sprite {
  protected collected = false;
  public readonly powerUpType: PowerUpType;

  constructor(scene: Phaser.Scene, x: number, y: number, type: PowerUpType, tint: number) {
    super(scene, x, y, 'coin'); // Reuse coin texture for now

    this.powerUpType = type;

    scene.add.existing(this);
    scene.physics.add.existing(this);

    const body = this.body as Phaser.Physics.Arcade.Body;
    body.setAllowGravity(false);
    body.setImmovable(true);

    this.setTint(tint);
    this.setScale(1.3);

    // Float animation
    scene.tweens.add({
      targets: this,
      y: y - 8,
      duration: 1200,
      yoyo: true,
      repeat: -1,
      ease: 'Sine.inOut',
    });

    // Glow effect
    scene.tweens.add({
      targets: this,
      alpha: 0.6,
      duration: 600,
      yoyo: true,
      repeat: -1,
      ease: 'Sine.inOut',
    });
  }

  public collect(): boolean {
    if (this.collected) return false;
    this.collected = true;

    // Collection animation
    this.scene.tweens.killTweensOf(this);

    this.scene.tweens.add({
      targets: this,
      y: this.y - 40,
      alpha: 0,
      scaleX: 2,
      scaleY: 2,
      duration: 300,
      ease: 'Power2',
      onComplete: () => {
        this.destroy();
      },
    });

    return true;
  }

  public isCollected(): boolean {
    return this.collected;
  }

  // Override this to apply the power-up effect
  public abstract applyEffect(scene: Phaser.Scene): void;

  // Duration in ms
  public abstract getDuration(): number;
}
