import { Entity, EntityDieAfterEvent, Player } from "@minecraft/server";
import { BossSkill } from "./bossSkill";

/**
 * Define a boss entity.
 * @category Need Registry
 * @beta
 */
export class Boss {
  constructor(
    /**
     * Identifier of the type of the entity.
     */
    public typeId: string,
    /**
     * The boss skill(s)
     */
    public skills: BossSkill[],
    /**
     * The boss music.
     */
    public music?: BossMusicOptions,
    /**
     * Trigger event when the boss die.
     */
    public dieEvent?: (arg: EntityDieAfterEvent) => void
  ) {}
  /**
   * Play the boss music to player(s).
   * @param player
   */
  playMusic(player: Player | Player[]): void {
    if (Array.isArray(player)) {
      player.forEach((pl) => {
        if (this.music) pl.playMusic(this.music.trackId, { loop: true });
      });
    } else if (this.music) {
      player.playMusic(this.music.trackId, { loop: true });
    }
  }
  /**
   * Unleash all the skills.
   * @param entity
   */
  unleashSkill(entity: Entity, boss: Entity): void {
    this.skills.forEach((skill) => {
      skill.unleash(entity, boss);
    });
  }
}

/**
 * Options of boss music.
 */
export interface BossMusicOptions {
  /**
   * Identifier of the music track to play.
   */
  trackId: string;
  /**
   * The radius within which player can hear the music.
   */
  radius: number;
}
