import { Physics } from 'phaser';
import { rng } from '../utils/random.js';

export class Platform extends Physics.Arcade.Sprite {
  constructor(scene, x, y, type = 'stable') {
    super(scene, x, y, 'platform');
    scene.add.existing(this);

    // Moving platforms need a dynamic body; everything else is static.
    const isMoving = type === 'moving';
    scene.physics.add.existing(this, !isMoving);

    this.platformType = type;
    this.breakingState = 0;
    this.moveSpeed = 0;
    this.moveRange = 0;
    this.startX = x;
    this.glowTween = null;

    if (isMoving) {
      this.body.setAllowGravity(false);
      this.body.setImmovable(true);
      this.moveSpeed = rng.between(50, 120) * rng.sign();
      this.moveRange = rng.between(60, 160);
    } else if (type === 'breaking') {
      this.setTint(0x999999);
    } else if (type === 'reorg') {
      // Phantom "reorg" platform: semi-transparent, vanishes shortly after use.
      this.setTint(0x38bdf8);
      this.setAlpha(0.55);
      this.glowTween = scene.tweens.add({
        targets: this,
        alpha: 0.3,
        duration: 700,
        yoyo: true,
        repeat: -1,
        ease: 'Sine.easeInOut',
      });
    } else if (type === 'genesis') {
      this.setTint(0xffd700);
      this.genesisGlow = scene.add.ellipse(x, y + 10, 100, 30, 0xffd700, 0.25)
        .setDepth(this.depth - 1);
      this.glowTween = scene.tweens.add({
        targets: this.genesisGlow,
        scaleX: 1.4,
        scaleY: 1.4,
        alpha: 0.1,
        duration: 800,
        yoyo: true,
        repeat: -1,
      });
    }
  }

  preUpdate(time, delta) {
    super.preUpdate(time, delta);
    if (this.platformType === 'moving' && this.body) {
      this.setVelocityX(this.moveSpeed);
      if (Math.abs(this.x - this.startX) > this.moveRange) {
        this.moveSpeed *= -1;
      }
      if (this.genesisGlow) this.genesisGlow.setPosition(this.x, this.y + 10);
    }
  }

  onPlayerLand(player) {
    if (this.platformType === 'breaking') {
      if (this.breakingState > 0) return false;
      this.breakingState = 2;
      this.disableBody(true, false);
      this.scene.tweens.add({
        targets: this,
        alpha: 0,
        scaleX: 0.3,
        scaleY: 0.1,
        duration: 250,
        onComplete: () => this.destroy(),
      });
      return true;
    }
    if (this.platformType === 'reorg') {
      // Allow this bounce, then vanish after a short grace window.
      if (this.breakingState > 0) return true;
      this.breakingState = 1;
      if (this.glowTween) { this.glowTween.stop(); this.glowTween = null; }
      this.setAlpha(0.55);
      this.scene.time.delayedCall(300, () => {
        this.breakingState = 2;
        if (this.body) this.disableBody(true, false);
        this.scene.tweens.add({
          targets: this,
          alpha: 0,
          scaleX: 0.2,
          scaleY: 0.2,
          duration: 200,
          onComplete: () => this.destroy(),
        });
      });
      return true;
    }
    return true;
  }

  isBroken() {
    return this.breakingState >= 2;
  }

  destroy(fromScene) {
    if (this.glowTween) {
      this.glowTween.stop();
      this.glowTween = null;
    }
    if (this.genesisGlow) {
      this.genesisGlow.destroy();
      this.genesisGlow = null;
    }
    super.destroy(fromScene);
  }
}
