import * as mc from "@minecraft/server";
import * as vector from "../utils/vector";
import * as gauntlet from "./earthrendGauntlet";

export function robeEffect(event, hurtEvent) {
  let entity = hurtEvent ? event.hurtEntity : event.damagingEntity;
  if (entity.typeId === "minecraft:player" && entity.hasTag("hf_mzs_geomancer_robe")) {
    if (!hurtEvent && event.hitEntity.typeId === "hf_mzs:boulder") return;
    if (Math.random() < 0.5) {
      let xOffset = Math.floor(Math.random() * 1 + 2);
      let yOffset = 1;
      let zOffset = Math.floor(Math.random() * 1 + 2);

      if (Math.random() < 0.5) xOffset *= -1;
      if (Math.random() < 0.5) zOffset *= -1;

      let pos = entity.location;
      pos.x = Math.floor(pos.x) + 0.5 + xOffset;
      pos.y = Math.floor(pos.y) + yOffset;
      pos.z = Math.floor(pos.z) + 0.5 + zOffset;

      const block = entity.dimension.getBlock(pos);
      if (!block || !block.isValid() || (!block.isAir && !block.isLiquid) || !gauntlet.GeomancyWhitelist.includes(block.below().below().typeId)) return;

      let boulder = entity.dimension.spawnEntity("hf_mzs:boulder<hf_mzs:from_armor>", pos);
      mc.system.runTimeout(() => {
        boulder.runCommand(`setblock ~ ~ ~ barrier [] replace`);
      }, 4);
    }
  }
}

export function tick1() {
  mc.system.run(() => {
    for (const player of mc.world.getAllPlayers()) {
      const mainHand = player.getComponent("equippable").getEquipment(mc.EquipmentSlot.Mainhand);

      if (mainHand && mainHand.typeId != "hf_mzs:earthrend_gauntlet") player.removeTag("hf_mzs_empty_hand");
      else if (mainHand && mainHand.typeId === "hf_mzs:earthrend_gauntlet") player.addTag("hf_mzs_empty_hand");
      else player.addTag("hf_mzs_empty_hand");

      const helmet = player.getComponent("equippable").getEquipment(mc.EquipmentSlot.Head);
      const chestplate = player.getComponent("equippable").getEquipment(mc.EquipmentSlot.Chest);
      const leggings = player.getComponent("equippable").getEquipment(mc.EquipmentSlot.Legs);
      const boots = player.getComponent("equippable").getEquipment(mc.EquipmentSlot.Feet);

      if (helmet && helmet.typeId === "hf_mzs:geomancer_beads") {
        if (player.hasTag("hf_mzs_empty_hand")) {
          player.addEffect("strength", 5, { amplifier: 1, showParticles: false });
        }
      }

      if (chestplate && chestplate.typeId === "hf_mzs:geomancer_robe") {
        player.addTag("hf_mzs_geomancer_robe");
      } else if (player.hasTag("hf_mzs_geomancer_robe")) {
        player.removeTag("hf_mzs_geomancer_robe");
      }

      if (leggings && (leggings.typeId === "hf_mzs:geomancer_belt" || leggings.typeId === "hf_mzs:geomancer_belt_boost")) {
        const slowness = player.getEffect("slowness");
        if (slowness) {
          const newLeggings = new mc.ItemStack("hf_mzs:geomancer_belt_boost", 1);
          newLeggings.getComponent("minecraft:durability").damage = leggings.getComponent("minecraft:durability").damage;
          if (leggings.nameTag) newLeggings.nameTag = leggings.nameTag;
          newLeggings.setLore(leggings.getLore());
          player.getComponent("equippable").setEquipment(mc.EquipmentSlot.Legs, newLeggings);
          player.addTag("hf_mzs_belt_boost");
        } else if (!slowness && player.hasTag("hf_mzs_belt_boost")) {
          const newLeggings = new mc.ItemStack("hf_mzs:geomancer_belt", 1);
          newLeggings.getComponent("minecraft:durability").damage = leggings.getComponent("minecraft:durability").damage;
          if (leggings.nameTag) newLeggings.nameTag = leggings.nameTag;
          newLeggings.setLore(leggings.getLore());
          player.getComponent("equippable").setEquipment(mc.EquipmentSlot.Legs, newLeggings);
          player.removeTag("hf_mzs_belt_boost");
        }
      }

      if (boots && boots.typeId === "hf_mzs:geomancer_sandals") {
        if (player.isFalling) {
          let downRay = { x: 0, y: -1, z: 0 };
          let bl = player.dimension.getBlockFromRay(player.location, downRay);
          if (bl) {
            let block = bl.block;

            let dist = vector.distance(block.location, player.location);
            if (dist > 4) {
              player.setDynamicProperty("hf_mzs:is_long_fall", true);
            }
          }
        }

        if (player.getDynamicProperty("hf_mzs:is_long_fall") && player.isOnGround) {
          player.runCommand("/playsound hf_mzs:boulder.small.hit @a[r=32] ~ ~ ~ 100 1.25 1");
          player.setDynamicProperty("hf_mzs:is_long_fall", false);
          player.addEffect("speed", 60, { amplifier: 1, showParticles: false });

          let map = new mc.MolangVariableMap();
          map.setFloat("variable.lifetime", 0.4);
          map.setFloat("variable.size", 1.0);
          let parLoc = vector.add(player.location, { x: 0, y: 0.1, z: 0 });

          player.dimension.spawnParticle("hf_mzs:ring_grow_ground", parLoc, map);
        }
      }
    }
  });
}
