import * as mc from "@minecraft/server";
import * as effects from "./effects";
import * as vector from "./utils/vector";
import * as frostmaw from "./entities/frostmaw";
import * as frostmawProjectile from "./entities/frostmawProjectile";
import * as wroughtnaut from "./entities/wroughtnaut";
import * as umvuthana from "./entities/umvuthana";
import * as umvuthanaSpawn from "./entities/umvuthanaSpawn";
import * as umvuthanaCrane from "./entities/umvuthanaCrane";
import * as umvuthanaRaptor from "./entities/umvuthanaRaptor";
import * as umvuthi from "./entities/umvuthi";
import * as umvuthanaFootstep from "./entities/umvuthanaFootstep";
import * as sunstrike from "./entities/sunstrike";
import * as lantern from "./entities/lantern";
import * as foliaath from "./entities/foliaath";
import * as structureUmvuthanaVillage from "./entities/structureUmvuthanaVillage";
import * as grottol from "./entities/grottol";
import * as naga from "./entities/naga";

import * as sculptor from "./entities/sculptor";
import * as stonePillar from "./entities/stonePillar";
import * as boulder from "./entities/boulderProjectile";
import * as geomancy from "./items/geomancerArmor";
import * as fallingRakedSand from "./entities/fallingRakedSand";
import * as bluff from "./entities/bluff";
import * as bluffProjectile from "./entities/bluffProjectile";
import * as bluffBoulder from "./entities/bluffBoulder";
import * as bluffSpawner from "./entities/bluffSpawner";

//#region - REGISTER AND CLEAR
let e = [];
function clear(entity) {
  for (let i = 0; i < e.length; i++) {
    if (e[i] !== entity) continue;
    e.splice(i, 1);
    return;
  }
}
function register(entity) {
  clear(entity);
  e.push(entity);
}
export function entityDeath(event) {
  if (event.deadEntity !== undefined && getIsMowzieMob(event.deadEntity) && event.deadEntity.typeId !== "hf_mzs:naga") {
    clear(event.deadEntity);
    return;
  }
}
//#endregion

export function eventBehavior(event) {
  if (event.eventId === "ding") {
    if (
      event.entity.getDynamicProperty("hf_mzs:ding_cooldown") !== undefined &&
      event.entity.getDynamicProperty("hf_mzs:ding_cooldown") > 0
    )
      return;
    event.entity.setDynamicProperty("hf_mzs:ding_cooldown", 8);

    event.entity.runCommand("/playsound random.anvil_land @a[r=16] ~~~ 1 2");
  }

  if (event.eventId === "register" && getIsMowzieMob(event.entity)) {
    register(event.entity);
  }
  if (event.eventId === "death" && getIsMowzieMob(event.entity) && event.entity.typeId !== "hf_mzs:naga") {
    clear(event.entity);
  }
  if (event.entity.typeId === "hf_mzs:umvuthana_spawn") umvuthanaSpawn.eventBehavior(event);
  if (event.entity.typeId === "hf_mzs:frostmaw") frostmaw.eventBehavior(event);
  if (event.entity.typeId === "hf_mzs:wroughtnaut") wroughtnaut.eventBehavior(event);
  if (event.entity.typeId === "hf_mzs:umvuthana") umvuthana.eventBehavior(event);
  if (event.entity.typeId === "hf_mzs:umvuthana_crane") umvuthanaCrane.eventBehavior(event);
  if (event.entity.typeId === "hf_mzs:umvuthana_raptor") umvuthanaRaptor.eventBehavior(event);
  if (event.entity.typeId === "hf_mzs:structure_umvuthana_village") structureUmvuthanaVillage.eventBehavior(event);
  if (event.entity.typeId === "hf_mzs:umvuthi") umvuthi.eventBehavior(event);
  if (event.entity.typeId === "hf_mzs:sunstrike") sunstrike.eventBehavior(event);
  if (event.entity.typeId === "hf_mzs:lantern") lantern.eventBehavior(event);
  if (event.entity.typeId === "hf_mzs:grottol") grottol.eventBehavior(event);
  if (event.entity.typeId === "hf_mzs:naga") naga.eventBehavior(event);
  if (event.entity.typeId === "hf_mzs:bluff") bluff.eventBehavior(event);
  if (event.entity.typeId === "hf_mzs:bluff_spawner") bluffSpawner.eventBehavior(event);
  if (event.entity.typeId === "hf_mzs:bluff_boulder") bluffBoulder.eventBehavior(event);
  if (event.entity.typeId === "hf_mzs:sculptor") sculptor.eventBehavior(event);
  if (event.entity.typeId === "hf_mzs:stone_pillar") stonePillar.eventBehavior(event);
  if (event.entity.typeId === "hf_mzs:foliaath") foliaath.eventBehavior(event);
  if (event.entity.typeId === "hf_mzs:lantern") lantern.eventBehavior(event);
}
export function hurtBehavior(event) {
  if (event.hurtEntity === undefined || !event.hurtEntity.isValid()) return;
  if (event.hurtEntity.typeId === "hf_mzs:frostmaw") frostmaw.hurtBehavior(event);
  if (event.hurtEntity.typeId === "hf_mzs:umvuthi") umvuthi.hurtBehavior(event);
  if (event.hurtEntity.typeId === "hf_mzs:grottol") grottol.hurtBehavior(event);
  if (event.hurtEntity.typeId === "hf_mzs:naga") naga.hurtBehavior(event);
  if (event.hurtEntity.typeId === "hf_mzs:sculptor") sculptor.hurtBehavior(event);

  geomancy.robeEffect(event, true);
}
export function hitBehavior(event) {
  if (event.damagingEntity === undefined || !event.damagingEntity.isValid()) return;
  if (event.damagingEntity.typeId === "hf_mzs:foliaath") foliaath.hitBehavior(event);

  boulder.launchBoulder(event);
  geomancy.robeEffect(event, false);
}
export function blockUpdateBehavior(event) {
  sculptor.detectCheating(event);
}

let unloadedEntities = new Map();

export function tick20() {
  if (e.length < 1) return;
  for (let i = 0; i < e.length; i++) {
    const entity = e[i];
    //mc.world.sendMessage(`${entity.typeId}   ${entity.isValid()}`);
    if (!entity.isValid() && !unloadedEntities.has(entity.id)) {
      unloadedEntities.set(entity.id, true);
      continue;
    }
    if (!entity.isValid() && unloadedEntities.has(entity.id)) {
      continue;
    }
    if (entity.isValid() && unloadedEntities.has(entity.id) && getIsMowzieMob(entity)) {
      //mc.world.sendMessage("reg");
      unloadedEntities.delete(entity.id);
      entity.triggerEvent("register");
    }

    if (entity === undefined) continue;
    //mc.world.sendMessage(entity.typeId+"");
    if (entity.typeId === "hf_mzs:frostmaw") frostmaw.tick20(entity);
    else if (entity.typeId === "hf_mzs:wroughtnaut") wroughtnaut.tick20(entity);
    else if (entity.typeId === "hf_mzs:umvuthana_footstep") umvuthanaFootstep.tick20(entity);
    else if (entity.typeId === "hf_mzs:naga") naga.tick20(entity);
  }
}
export function tick5() {
  if (e.length < 1) return;
  for (let i = 0; i < e.length; i++) {
    var entity = e[i];
    if (entity === undefined || !entity.isValid()) continue;

    if (entity.typeId === "hf_mzs:frostmaw") frostmaw.tick5(entity);
    else if (entity.typeId === "hf_mzs:wroughtnaut") wroughtnaut.tick5(entity);
    else if (entity.typeId === "hf_mzs:umvuthana_raptor") umvuthanaRaptor.tick5(entity);
    else if (entity.typeId === "hf_mzs:umvuthana_crane") umvuthanaCrane.tick5(entity);
    else if (entity.typeId === "hf_mzs:umvuthi") umvuthi.tick5(entity);
    else if (entity.typeId === "hf_mzs:bluff") bluff.tick5(entity);
    else if (entity.typeId === "hf_mzs:lantern") lantern.tick5(entity);
  }
}
export function tick1() {
  if (e.length < 1) return;
  for (let i = 0; i < e.length; i++) {
    var entity = e[i];
    if (entity === undefined || !entity.isValid()) continue;

    if (entity.getDynamicProperty("hf_mzs:ding_cooldown") > 0)
      entity.setDynamicProperty("hf_mzs:ding_cooldown", entity.getDynamicProperty("hf_mzs:ding_cooldown") - 1);

    if (entity.typeId === "hf_mzs:umvuthi") {
      umvuthi.tick1(entity);
      umvuthi.tick1alt(entity);
    }
    if (entity.typeId === "hf_mzs:naga") naga.tick1(entity);
    if (entity.typeId === "hf_mzs:umvuthana_crane") umvuthanaCrane.tick1(entity);
    if (entity.typeId === "hf_mzs:wroughtnaut") wroughtnaut.tick1(entity);
    if (entity.typeId === "hf_mzs:frostmaw") frostmaw.tick1(entity);
    if (entity.typeId === "hf_mzs:frostmaw_projectile") frostmawProjectile.tick1(entity);
    if (entity.typeId === "hf_mzs:falling_raked_sand") fallingRakedSand.tick1(entity);
    if (entity.typeId === "hf_mzs:bluff_projectile") bluffProjectile.tick1(entity);
    //if (entity.typeId === "hf_mzs:bluff") bluff.tick1(entity);
  }
}

// projectile hit entity
export function projectileBehavior(event) {
  try {
    if (event.getEntityHit().entity.isValid() && event.getEntityHit().entity.typeId === "hf_mzs:naga")
      naga.hitByProjectile(event.getEntityHit().entity, event.projectile);
    if (
      event.projectile.typeId === "hf_mzs:dart" &&
      event.getEntityHit().entity.typeId !== "hf_mzs:frostmaw" &&
      event.getEntityHit().entity.typeId !== "minecraft:player"
    ) {
      event.getEntityHit().entity.applyDamage(1);
      event.getEntityHit().entity.addEffect("poison", 40, {
        amplifier: 1,
        showParticles: true,
      });
      const dir = event.projectile.getVelocity();
      event.getEntityHit().entity.applyKnockback(dir.x, dir.z, 1, 0.2);
      event.projectile.remove();
      //event.projectile.clearVelocity();
      //event.projectile.applyImpulse(vector.multiply(dir, -0.2));
    }
    if (!event.projectile.isValid()) return;
    if (event.projectile.typeId === "hf_mzs:poison_ball" && !event.projectile.hasTag("hf_mzs_hit")) {
      event.projectile.runCommand("/playsound hf_mzs:misc.acid_hit @a[r=16] ~~~ 1 1");
      event.projectile.runCommand("/particle hf_mzs:naga_projectile_explode_smoke");
      event.projectile.runCommand("/particle hf_mzs:naga_projectile_explode_drops");
      event.projectile.runCommand("/effect @e[r=3, type=!hf_mzs:naga] poison 4 2");
      event.projectile.runCommand("/tp @s ~~-5~");
      event.projectile.addTag("hf_mzs_hit");
    }
    boulder.boulderHit(event);
    sculptor.interceptProjectile(event);
  } catch {}
}

// projectile hit block
export function projectileBlockBehavior(event) {
  boulder.boulderHit(event);
  if (!event.projectile.isValid()) return;
  /*
    if (event.projectile.typeId === 'hf_mzs:frostmaw_projectile' && !event.projectile.hasTag("hf_mzs_hit")) {
        event.projectile.runCommand("/tp @s ~~-5~");
        event.projectile.addTag("hf_mzs_hit");
    }
        */
  if (event.projectile.typeId === "hf_mzs:dart") {
    if (!event.projectile.getProperty("hf_mzs:hit"))
      event.projectile.runCommand("/playsound block.bowhit @a[r=16] ~ ~ ~ 0.5 1.2 0.5");
    event.projectile.setProperty("hf_mzs:hit", true);
  }
  if (event.projectile.typeId === "hf_mzs:poison_ball" && !event.projectile.hasTag("hf_mzs_hit")) {
    event.projectile.runCommand("/playsound hf_mzs:misc.acid_hit @a[r=16] ~~~ 1 1");
    event.projectile.runCommand("/particle hf_mzs:naga_projectile_explode_smoke");
    event.projectile.runCommand("/particle hf_mzs:naga_projectile_explode_drops");
    event.projectile.runCommand("/effect @e[r=3, type=!hf_mzs:naga] poison 4 3");
    event.projectile.runCommand("/tp @s ~~-5~");
    event.projectile.addTag("hf_mzs_hit");
  }
}

function getIsMowzieMob(entity) {
  return (
    entity.getComponent("minecraft:type_family") !== undefined &&
    entity.getComponent("minecraft:type_family").hasTypeFamily("hf_mzs_mob")
  );
}
