import { PHYSICS } from '@/config/physics.config';
import { BaseEnemy } from './BaseEnemy';

export class Jumper extends BaseEnemy {
  private moveDirection: 1 | -1 = 1;
  private startX: number;
  private patrolDistance = 150;
  private jumpTimer = 0;

  constructor(scene: Phaser.Scene, x: number, y: number) {
    super(scene, x, y, 'enemy');

    this.startX = x;
    this.setTint(0x88ff88); // Green tint to distinguish
  }

  protected updateBehavior(_time: number, delta: number): void {
    const body = this.body as Phaser.Physics.Arcade.Body;

    // Move in current direction
    body.setVelocityX(PHYSICS.enemies.jumper.speed * this.moveDirection);

    // Jump periodically when on ground
    this.jumpTimer -= delta;
    if (this.jumpTimer <= 0 && body.onFloor()) {
      body.setVelocityY(PHYSICS.enemies.jumper.jumpForce);
      this.jumpTimer = PHYSICS.enemies.jumper.jumpInterval;

      // Squash effect on jump
      this.setScale(0.8, 1.2);
      this.scene.tweens.add({
        targets: this,
        scaleX: 1,
        scaleY: 1,
        duration: 150,
        ease: 'Back.out',
      });
    }

    // Check patrol bounds
    if (this.x > this.startX + this.patrolDistance) {
      this.moveDirection = -1;
      this.setFlipX(true);
    } else if (this.x < this.startX - this.patrolDistance) {
      this.moveDirection = 1;
      this.setFlipX(false);
    }

    // Check for walls
    if (body.blocked.right) {
      this.moveDirection = -1;
      this.setFlipX(true);
    } else if (body.blocked.left) {
      this.moveDirection = 1;
      this.setFlipX(false);
    }
  }

  protected recover(): void {
    super.recover();
    this.setTint(0x88ff88); // Restore green tint
  }
}
