import * as mc from "@minecraft/server";
import * as selectors from "../utils/selectors";

const DAMAGE = 3;
const RANGE = 2;

const FIRE_TIME = 2;
//const FIRE_RANGE = 1.3;
//const FIRE_TIME_TICK = 1.5;

export function eventBehavior(event) {
  if (event.eventId === "minecraft:entity_spawned") {
    event.entity.runCommand("/playsound hf_mzs:misc.sunstrike @a[r=16] ~~~ 1 1");
  }
  if (event.eventId === "damage") {
    event.entity.runCommand("/camerashake add @a[r=4] 1 0.3 positional");
    selectors
      .union(
        {
          location: event.entity.location,
          maxDistance: RANGE,
          excludeFamilies: ["inanimate"],
          //excludeTags: [ownerId], <---- this makes it deal no damage at all
        },
        ["mob", "player"],
        event.entity.dimension,
        event.entity.getDynamicProperty("hf_mzs:caster_id")
      )
      .forEach((entity) => {
        if (entity === undefined || !entity.isValid()) 
          return;
        const caster = mc.world.getEntity(event.entity.getDynamicProperty("hf_mzs:caster_id"));
        if (entity.typeId.includes("umvuthana") && (
          (entity.getProperty("hf_mzs:tamed") && entity.getComponent("tameable").tamedToPlayer.id === caster.id) || 
          (!entity.getProperty("hf_mzs:tamed") && caster.typeId==="hf_mzs:umvuthi"))
        )
          return;
        entity.applyKnockback(1, 0, 1, 0.4);
        entity.applyDamage(DAMAGE, {
          cause: "entityAttack",
        });
        entity.setOnFire(FIRE_TIME);
      });
  }
}
