import { world, system } from "@minecraft/server";

world.afterEvents.itemCompleteUse.subscribe((event) => {
  if (event.itemStack.typeId === 'm:item_bandage') {
    let heal = event.source.getComponent('health');
    heal.setCurrentValue(heal.currentValue + 10);
    event.source.runCommandAsync('clear @s m:item_bandage 0 1');
    event.source.addEffect("speed", 20, { amplifier: 0, showParticles: true });
    event.source.runCommandAsync('playsound player.heal @a ~~~ 1');
  }
});

const m_item_infectedFlesh = [
  'm:item_infected_flesh'
]

const m_item_nightwalkerFelsh = [
  'm:item_nightwalker_felsh'
]

world.afterEvents.itemCompleteUse.subscribe(event => {
  const { source: entity, itemStack } = event
  if (entity.typeId !== "minecraft:player" || !itemStack || !m_item_infectedFlesh.includes(itemStack.typeId)) return;
  entity.addEffect('nausea', 200, { amplifier: 1, showParticles: true })
})

world.afterEvents.itemCompleteUse.subscribe(event => {
  const { source: entity, itemStack } = event
  if (entity.typeId !== "minecraft:player" || !itemStack || !m_item_nightwalkerFelsh.includes(itemStack.typeId)) return;
  entity.addEffect('haste', 200, { amplifier: 1, showParticles: true })
})

world.afterEvents.entityHitEntity.subscribe(({ hitEntity, damagingEntity }) => {
  if (damagingEntity.typeId == 'entity:bio_zombie_host') {
    if (hitEntity.getEffect("slowness")) {
    } else {
      hitEntity.addEffect("slowness", 20, { amplifier: 3, showParticles: true });
    }
  }
  if (damagingEntity.typeId == 'entity:bio_zombie_host') {
    if (hitEntity.getEffect("poison")) {
    } else {
      hitEntity.addEffect("poison", 40, { amplifier: 0, showParticles: true });
    }
  }
  if (damagingEntity.typeId == 'entity:bio_zombie') {
    if (hitEntity.getEffect("slowness")) {
    } else {
      hitEntity.addEffect("slowness", 20, { amplifier: 3, showParticles: true });
    }
  }
  if (damagingEntity.typeId == 'entity:climber') {
    if (hitEntity.getEffect("slowness")) {
    } else {
      hitEntity.addEffect("slowness", 120, { amplifier: 3, showParticles: true });
    }
  }
  if (damagingEntity.typeId == 'entity:burrower_off') {
    if (hitEntity.getEffect("slowness")) {
    } else {
      hitEntity.addEffect("slowness", 20, { amplifier: 3, showParticles: true });
    }
  }
  if (damagingEntity.typeId == 'entity:burrower_off') {
    if (hitEntity.getEffect("poison")) {
    } else {
      hitEntity.addEffect("poison", 40, { amplifier: 0, showParticles: true });
    }
  }
  if (damagingEntity.typeId == 'entity:bio_wolf') {
    if (hitEntity.getEffect("slowness")) {
    } else {
      hitEntity.addEffect("slowness", 20, { amplifier: 3, showParticles: true });
    }
  }
  if (damagingEntity.typeId == 'entity:armless') {
    if (hitEntity.getEffect("slowness")) {
    } else {
      hitEntity.addEffect("slowness", 20, { amplifier: 3, showParticles: true });
    }
  }
});

world.afterEvents.projectileHitEntity.subscribe((event) => {
  const [source, projectile, target] = [event.source, event.projectile, event.getEntityHit().entity];
  if (target?.typeId != "entity:nightwalker" && projectile?.typeId == "entity:darkmatter") {
    target?.getComponent('health').setCurrentValue(target?.getComponent('health').currentValue - 1);
    target.runCommandAsync('playsound zombie.nightwalker.darkmatter.hit @a ~~~ 1');
  }
  if (target?.typeId != "entity:nightwalker" && projectile?.typeId == "entity:dark_arrow") {
    target?.getComponent('health').setCurrentValue(target?.getComponent('health').currentValue - 5);
    target.runCommandAsync('playsound zombie.nightwalker.darkmatter.hit @a ~~~ 1');
  }
  if (target?.typeId == "entity:nightwalker" && projectile?.typeId == "entity:darkmatter") {
    target.runCommandAsync('playsound zombie.nightwalker.darkmatter.hit @a ~~~ 1');
  }
  if (target?.typeId == "entity:nightwalker" && projectile?.typeId == "entity:dark_arrow") {
    target.runCommandAsync('playsound zombie.nightwalker.darkmatter.hit @a ~~~ 1');
  }
});