import { world, system } from "@minecraft/server";
import { get_PlayerGroups, CurrentEventProp } from "./utilities";

const eventId = "bees_attack";
function swarm_of_bees_MAIN(dimension) {
  system.runInterval(() => {
    const event = CurrentEventProp.getProp();
    if (!(event.id == eventId && event.status == "is_happening")) return;
    if (event.timer < event.maxDuration) {
      if (event.timer == 1) {
        world.sendMessage([
          {
            translate: `tvf_randev.event.start.${event.id}`,
          },
        ]);
      }
      get_PlayerGroups(dimension, 12).forEach((group) => {
        summonBee(dimension, group.location);
      });
      event.timer += 1;
    }
    if (event.forcedEnd == true || event.timer >= event.maxDuration) {
      event.status = "is_end";
    }
    CurrentEventProp.update(event);
  });
}
function summonBee(dimension, location) {
  if (system.currentTick % 10 != 0) return;
  let spawnLocation = {
    x: location.x + randomOneOrMinusOne() * (5 + Math.floor(Math.random() * 5)),
    y: location.y,
    z: location.z + randomOneOrMinusOne() * (5 + Math.floor(Math.random() * 5)),
  };
  spawnLocation.y =
    dimension.getTopmostBlock({
      x: spawnLocation.x,
      z: spawnLocation.z,
    }).location.y + 2;
  dimension
    .spawnEntity("minecraft:bee", spawnLocation)
    .triggerEvent("hive_destroyed");
}
function randomOneOrMinusOne() {
  let mod;
  if (Math.floor(Math.random() * 2) == 1) mod = 1;
  else mod = -1;
  return mod;
}
export { swarm_of_bees_MAIN };
