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

const eventId = "skeleton_attack";
const skeletonsTypeIds = [
  "minecraft:skeleton",
  "minecraft:stray",
  "minecraft:bogged",
  "minecraft:wither_skeleton",
];
const weaponList = ["minecraft:bow", "golden_sword", "golden_axe"];
const equipmentList = [
  "golden_boots",
  "golden_leggings",
  "golden_chestplate",
  "golden_helmet",
  "none",
];
export function skeleton_attack_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) => {
        summonSkeleton(dimension, group.location);
        visualEffects(group);
      });
      event.timer += 1;
    }
    if (event.forcedEnd == true || event.timer >= event.maxDuration) {
      event.status = "is_end";
      dimension.runCommand("/fog @a remove tvf_randev:fogsID");
      world.sendMessage([
        {
          translate: `tvf_randev.event.end.${event.id}`,
        },
      ]);
    }
    CurrentEventProp.update(event);
  });
}
function summonSkeleton(dimension, location) {
  if (system.currentTick % 60 != 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 + 1;
  const mobType =
    skeletonsTypeIds[Math.floor(Math.random() * skeletonsTypeIds.length)];
  let isRiding = false;
  if (Math.floor(Math.random() * 2) == 1) {
    isRiding = true;
  }
  const weapon = weaponList[Math.floor(Math.random() * weaponList.length)];
  const mob = dimension.spawnEntity(mobType, spawnLocation);
  if (isRiding) {
    mob.runCommand(
      "/ride @s summon_ride minecraft:skeleton_horse reassign_rides is_adult"
    );
  }
  mob.runCommand("/replaceitem entity @s slot.weapon.mainhand 0 " + weapon);
  giveArmor(mob, equipmentList, 2);
}
function visualEffects(group) {
  if (system.currentTick % 60 != 0) return;

  group.players.forEach((player) => {
    if (player.graphicsMode == "Deferred") {
      player.runCommand("/fog @s push tvf_randev:white_vol tvf_randev:fogsID");
    } else {
      player.runCommand("/fog @s push tvf_randev:white_fog tvf_randev:fogsID");
    }
  });
}
function giveArmor(mob, armorList, count) {
  for (let i = 0; i < count; i++) {
    const piace = armorList[Math.floor(Math.random() * armorList.length)];
    if (piace.endsWith("boots")) {
      mob.runCommand("/replaceitem entity @s slot.armor.feet 0 " + piace);
    }
    if (piace.endsWith("leggings")) {
      mob.runCommand("/replaceitem entity @s slot.armor.legs 0 " + piace);
    }
    if (piace.endsWith("chestplate")) {
      mob.runCommand("/replaceitem entity @s slot.armor.chest 0 " + piace);
    }
    if (piace.endsWith("helmet")) {
      mob.runCommand("/replaceitem entity @s slot.armor.head 0 " + piace);
    }
  }
}
function randomOneOrMinusOne() {
  let mod;
  if (Math.floor(Math.random() * 2) == 1) mod = 1;
  else mod = -1;
  return mod;
}
