import { run } from "./utilities";

run((players) => {
  players.forEach((player) => {
    const helmet = player
      .getComponent("minecraft:equippable")
      .getEquipmentSlot("Head")
      .getItem();
    const chestplate = player
      .getComponent("minecraft:equippable")
      .getEquipmentSlot("Chest")
      .getItem();
    const leggings = player
      .getComponent("minecraft:equippable")
      .getEquipmentSlot("Legs")
      .getItem();
    const boots = player
      .getComponent("minecraft:equippable")
      .getEquipmentSlot("Feet")
      .getItem();

    const hasSpectralCloakHelmet = player.hasTag(
      "chronicoverride_llc:has_spectral_cloak_helmet"
    );
    const hasReduceDetection = player.hasTag(
      "chronicoverride_llc:has_reduce_detection"
    );
    const hasEvilEyePendant = player.hasTag(
      "chronicoverride_llc:has_evil_eye_pendant"
    );

    const hasWraithskinHelmet = player.hasTag(
      "chronicoverride_llc:has_wraithskin_helmet"
    );
    const hasWraithskinLeggings = player.hasTag(
      "chronicoverride_llc:has_wraithskin_leggings"
    );
    const hasWraithskinBoots = player.hasTag(
      "chronicoverride_llc:has_wraithskin_boots"
    );

    const hasNightstalkerHelmet = player.hasTag(
      "chronicoverride_llc:has_nightstalker_helmet"
    );
    const hasNightstalkerLeggings = player.hasTag(
      "chronicoverride_llc:has_nightstalker_leggings"
    );
    const hasNightstalkerBoots = player.hasTag(
      "chronicoverride_llc:has_nightstalker_boots"
    );

    const hasWidowsilkLeggings = player.hasTag(
      "chronicoverride_llc:has_widowsilk_leggings"
    );

    if (helmet && chestplate && leggings && boots) {
      if (
        helmet?.typeId === "chronicoverride_llc:widowsilk_helmet" &&
        chestplate?.typeId === "chronicoverride_llc:widowsilk_chestplate" &&
        leggings?.typeId === "chronicoverride_llc:widowsilk_leggings" &&
        boots?.typeId === "chronicoverride_llc:widowsilk_boots"
      ) {
        player.runCommand(`fill ~1~1~1 ~-1~-1~-1 air replace web`);
      }
    }

    if (helmet) {
      if (
        !hasSpectralCloakHelmet &&
        helmet?.typeId === "chronicoverride_llc:spectral_cloak_helmet"
      ) {
        player.addTag("chronicoverride_llc:has_spectral_cloak_helmet");
        player.runCommand(`effect @s invisibility infinite 255 true`);
      }
      if (
        hasSpectralCloakHelmet &&
        helmet?.typeId !== "chronicoverride_llc:spectral_cloak_helmet"
      ) {
        player.removeTag("chronicoverride_llc:has_spectral_cloak_helmet");
        player.runCommand(`effect @s clear invisibility`);
      }

      if (
        !hasWraithskinHelmet &&
        helmet?.typeId === "chronicoverride_llc:wraithskin_helmet"
      ) {
        player.addTag("chronicoverride_llc:has_wraithskin_helmet");
        player.runCommand(`effect @s night_vision infinite 0 true`);
      }
      if (
        hasWraithskinHelmet &&
        helmet?.typeId !== "chronicoverride_llc:wraithskin_helmet"
      ) {
        player.removeTag("chronicoverride_llc:has_wraithskin_helmet");
        player.runCommand(`effect @s clear night_vision`);
      }

      if (
        !hasNightstalkerHelmet &&
        helmet?.typeId === "chronicoverride_llc:nightstalker_helmet"
      ) {
        player.addTag("chronicoverride_llc:has_nightstalker_helmet");
        player.runCommand(`effect @s night_vision infinite 0 true`);
      }
      if (
        hasNightstalkerHelmet &&
        helmet?.typeId !== "chronicoverride_llc:nightstalker_helmet"
      ) {
        player.removeTag("chronicoverride_llc:has_nightstalker_helmet");
        player.runCommand(`effect @s clear night_vision`);
      }
    }
    if (!helmet) {
      if (hasSpectralCloakHelmet) {
        player.removeTag("chronicoverride_llc:has_spectral_cloak_helmet");
        player.runCommand(`effect @s clear invisibility`);
      }
      if (hasWraithskinHelmet) {
        player.removeTag("chronicoverride_llc:has_wraithskin_helmet");
        player.runCommand(`effect @s clear night_vision`);
      }
      if (hasNightstalkerHelmet) {
        player.removeTag("chronicoverride_llc:has_nightstalker_helmet");
        player.runCommand(`effect @s clear night_vision`);
      }
    }

    if (chestplate) {
      if (
        (!hasReduceDetection &&
          chestplate?.typeId ===
          "chronicoverride_llc:spectral_cloak_chestplate") ||
        chestplate?.typeId === "chronicoverride_llc:nightstalker_chestplate"
      ) {
        player.addTag("chronicoverride_llc:has_reduce_detection");
      }
      if (
        hasReduceDetection &&
        chestplate?.typeId !==
        "chronicoverride_llc:spectral_cloak_chestplate" &&
        chestplate?.typeId !== "chronicoverride_llc:nightstalker_chestplate"
      ) {
        player.removeTag("chronicoverride_llc:has_reduce_detection");
      }

      if (
        !hasEvilEyePendant &&
        chestplate?.typeId === "chronicoverride_llc:evil_eye_pendant"
      ) {
        player.addTag("chronicoverride_llc:has_evil_eye_pendant");
        player.runCommand("playsound beacon.power @s"),
          player.runCommand(`effect @s night_vision infinite 255 true`);
      }
      if (
        hasEvilEyePendant &&
        chestplate?.typeId !== "chronicoverride_llc:evil_eye_pendant"
      ) {
        player.removeTag("chronicoverride_llc:has_evil_eye_pendant");
        player.runCommand(`effect @s clear night_vision`);
      }
    }
    if (!chestplate) {
      if (hasEvilEyePendant) {
        player.removeTag("chronicoverride_llc:has_evil_eye_pendant");
        player.runCommand(`effect @s clear night_vision`);
      }
      if (hasReduceDetection) {
        player.removeTag("chronicoverride_llc:has_reduce_detection");
      }
    }

    if (leggings) {
      if (
        !hasWraithskinLeggings &&
        leggings?.typeId === "chronicoverride_llc:wraithskin_leggings"
      ) {
        player.addTag("chronicoverride_llc:has_wraithskin_leggings");
        player.runCommand(`effect @s speed infinite 0 true`);
      }
      if (
        hasWraithskinLeggings &&
        leggings?.typeId !== "chronicoverride_llc:wraithskin_leggings"
      ) {
        player.removeTag("chronicoverride_llc:has_wraithskin_leggings");
        player.runCommand(`effect @s clear speed`);
      }

      if (
        !hasNightstalkerLeggings &&
        leggings?.typeId === "chronicoverride_llc:nightstalker_leggings"
      ) {
        player.addTag("chronicoverride_llc:has_nightstalker_leggings");
        player.runCommand(`effect @s speed infinite 0 true`);
      }
      if (
        hasNightstalkerLeggings &&
        leggings?.typeId !== "chronicoverride_llc:nightstalker_leggings"
      ) {
        player.removeTag("chronicoverride_llc:has_nightstalker_leggings");
        player.runCommand(`effect @s clear speed`);
      }

      if (
        !hasWidowsilkLeggings &&
        leggings?.typeId === "chronicoverride_llc:widowsilk_leggings"
      ) {
        player.addTag("chronicoverride_llc:has_widowsilk_leggings");
        player.runCommand(`effect @s speed infinite 0 true`);
      }
      if (
        hasWidowsilkLeggings &&
        leggings?.typeId !== "chronicoverride_llc:widowsilk_leggings"
      ) {
        player.removeTag("chronicoverride_llc:has_widowsilk_leggings");
        player.runCommand(`effect @s clear speed`);
      }
    }
    if (!leggings) {
      if (hasWraithskinLeggings) {
        player.removeTag("chronicoverride_llc:has_wraithskin_leggings");
        player.runCommand(`effect @s clear speed`);
      }
      if (hasNightstalkerLeggings) {
        player.removeTag("chronicoverride_llc:has_nightstalker_leggings");
        player.runCommand(`effect @s clear speed`);
      }
      if (hasWidowsilkLeggings) {
        player.removeTag("chronicoverride_llc:has_widowsilk_leggings");
        player.runCommand(`effect @s clear speed`);
      }
    }

    if (boots) {
      if (
        !hasWraithskinBoots &&
        boots?.typeId === "chronicoverride_llc:wraithskin_boots"
      ) {
        player.addTag("chronicoverride_llc:has_wraithskin_boots");
        player.runCommand(`effect @s slow_falling infinite 3 true`);
      }
      if (
        hasWraithskinBoots &&
        boots?.typeId !== "chronicoverride_llc:wraithskin_boots"
      ) {
        player.removeTag("chronicoverride_llc:has_wraithskin_boots");
        player.runCommand(`effect @s clear slow_falling`);
      }

      if (
        !hasNightstalkerBoots &&
        boots?.typeId === "chronicoverride_llc:nightstalker_boots"
      ) {
        player.addTag("chronicoverride_llc:has_nightstalker_boots");
        player.runCommand(`effect @s slow_falling infinite 2 true`);
      }
      if (
        hasNightstalkerBoots &&
        boots?.typeId !== "chronicoverride_llc:nightstalker_boots"
      ) {
        player.removeTag("chronicoverride_llc:has_nightstalker_boots");
        player.runCommand(`effect @s clear slow_falling`);
      }
    }
    if (!boots) {
      if (hasWraithskinBoots) {
        player.removeTag("chronicoverride_llc:has_wraithskin_boots");
        player.runCommand(`effect @s clear slow_falling`);
      }
      if (hasNightstalkerBoots) {
        player.removeTag("chronicoverride_llc:has_nightstalker_boots");
        player.runCommand(`effect @s clear slow_falling`);
      }
    }
  });
});
