import { world, Entity, system, EntityComponentTypes } from "@minecraft/server";
import { healFromItem } from "healFromItem";
import { playerHungerHeal } from "playerHungerHeal";
import { throwBy } from "mob/throwBy";
import { rollWebAttack } from "mob/shootWeb";
import { ogreLaugh } from "mob/ogreLaugh";
import { useAmethystStaff } from "item/amethyst_staff";
import { replaceMinecart } from "item/replace_minecart";
import { angerEndermen } from "mob/angerEndermen";
import { enderTeleport } from "mob/enderTeleport";
import { handleEndSpawn } from "mob/endSpawns";
import { rollCastFire } from "mob/castFire";

export const DEFAULT_TICK = 20;

world.afterEvents.itemCompleteUse.subscribe(function (data) {
  healFromItem(data);
});

world.afterEvents.entityHealthChanged.subscribe(function (data) {
  playerHungerHeal(data);
});

world.beforeEvents.entityRemove.subscribe(function (data) {
  angerEndermen(data);
  replaceMinecart(data);
});

world.beforeEvents.itemUse.subscribe((data) => {
  useAmethystStaff(data);
});

world.afterEvents.entityDie.subscribe(function (data) {
  ogreLaugh(data?.damageSource?.damagingEntity);
});

world.afterEvents.entitySpawn.subscribe(function (data) {
  if (data?.entity == null) {
    return;
  }
  if (data.entity.typeId == "minere:bomb") {
    world.playSound("random.fuse", data.entity.location);
  }

  handleEndSpawn(data.entity);
});

world.afterEvents.entityHurt.subscribe(function (data) {
  if (data?.hurtEntity === null) {
    return;
  }
  if (data?.damageSource === null) {
    return;
  }
  if (!data.hurtEntity.hasComponent(EntityComponentTypes.Health)) {
    return;
  }

  const projectile = data.damageSource?.damagingEntity;
  const attacker = data.damageSource?.damagingEntity;
  const target = data.hurtEntity;
  const dimension = world.getDimension(target.dimension.id);
  if (!dimension) {
    return;
  }

  // throwing
  if (
    attacker?.typeId === "minere:yeti" ||
    attacker?.typeId === "minere:walker"
  ) {
    throwBy(attacker, target, 5, 10);
  }

  // web spider shooting webs
  if (attacker?.typeId === "minere:web_spider") {
    rollWebAttack(attacker, target);
  }
  if (target?.typeId === "minere:web_spider") {
    rollWebAttack(target, attacker);
  }

  // demon shooting fire
  if (attacker?.typeId === "minere:demon") {
    rollCastFire(attacker, target);
  }
  if (target?.typeId === "minere:demon") {
    rollCastFire(target, attacker);
  }

  // ender phantom teleport target
  if (attacker?.typeId === "minere:ender_phantom") {
    enderTeleport(target, 10, 0.5, 0);
  }

  // ender phantom teleport self
  if (target?.typeId === "minere:ender_phantom") {
    enderTeleport(target, 40, 0.35, 4);
  }

  // enderman teleport target
  if (attacker?.typeId === "minecraft:enderman") {
    enderTeleport(target, 8, 0.5, 0);
  }

  // ender dragon teleport target
  if (attacker?.typeId === "minecraft:ender_dragon" && !projectile) {
    enderTeleport(target, 20, 0.5, 0);
  }

  // DISABLED because it's buggy
  // ender dragon teleport self
  // if (target?.typeId === "minecraft:ender_dragon") {
  //   if (target.hasComponent("minecraft:behavior.dragonlanding")) {
  //     enderTeleport(target, 100, 0.1, 8);
  //   } else {
  //     enderTeleport(target, 100, 0.25, 8);
  //   }
  // }

  // anger endermen
  if (
    target.typeId === "minecraft:ender_crystal" &&
    dimension.id == "minecraft:the_end"
  ) {
    const endermen = dimension.getEntities({
      type: "enderman",
      closest: 4,
      location: target.location,
      maxDistance: 100,
    }) as Entity[];
    endermen.forEach((enderman: Entity) => {
      enderman.triggerEvent("minecraft:become_angry");
      world.playSound("mob.endermen.scream", enderman.location, {
        volume: 5.0,
      });
    });
  }
});
