import { BOSS_DEFINITIONS } from "./bossCatalog";
import {
  advanceMechanicLoadout,
  BOSS_MECHANIC_REGISTRY,
  BULL_CHARGE,
  BULL_POUNCE,
  handleMechanicDispel,
  upcomingLoadoutMechanic,
} from "./bosses/mechanicPool";
import { createBaseMotion } from "./bosses/shared";
import type { BossMechanicContext, BossMechanicResult } from "./bosses/types";
import type { BossId, BossMotionState, BossState, Debuff, MemberId, WorldPosition } from "./types";

export { BOSS_MECHANIC_REGISTRY, BULL_CHARGE, BULL_POUNCE };

export function createBossState(bossId: BossId = "bulldrome"): BossState {
  const definition = BOSS_DEFINITIONS[bossId];
  return {
    id: bossId,
    name: definition.name,
    maxHp: definition.maxHp,
    hp: definition.maxHp,
    nextMeleeAt: 2,
  };
}

export function createBossMotionState(bossId: BossId = "bulldrome"): BossMotionState {
  return {
    ...createBaseMotion(bossId),
    position: [0, -6.8],
    chargeStart: [0, -6.8],
    nextMechanicAt: 5,
  };
}

export function advanceBossMechanics(context: BossMechanicContext): BossMechanicResult {
  return advanceMechanicLoadout(context, BOSS_DEFINITIONS[context.boss.id].mechanicIds);
}

export function handleBossDispel(
  bossId: BossId,
  motion: BossMotionState,
  memberId: MemberId,
  position: WorldPosition,
  time: number,
  debuffs: readonly Debuff[],
) {
  if (!BOSS_DEFINITIONS[bossId].mechanicIds.includes("venom-purge")) {
    return { motion, message: "Harmful magic removed." };
  }
  return handleMechanicDispel(motion, memberId, position, time, debuffs);
}

export function upcomingMechanic(boss: BossState, motion: BossMotionState, time: number) {
  return upcomingLoadoutMechanic(BOSS_DEFINITIONS[boss.id].mechanicIds, motion, time);
}
