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

world.afterEvents.entityHurt.subscribe((data) => {
	try {
		const { damageSource, hurtEntity: victim } = data;
		const attacker = damageSource.damagingEntity;
		const notTarget = [
			"minecraft:item",
			"minecraft:armor_stand",
			"minecraft:xp_orb",
			"minecraft:arrow",
			"manalabs_opw:mini_snow_golem",
			"manalabs_opw:shadow_vex",
			"manalabs_opw:spell_amethyst",
			"manalabs_opw:spell_bubble",
			"manalabs_opw:spell_cactus",
			"manalabs_opw:spell_ice",
			"manalabs_opw:spell_laser",
			"manalabs_opw:spell_poison",
			"manalabs_opw:spell_shadow",
			"manalabs_opw:spell_skull",
		];
		const playerName = attacker.name;

		if (!attacker) return;
		const playerEquippableComp = attacker.getComponent("equippable");
		if (!playerEquippableComp) return;
		const heldSword = playerEquippableComp.getEquipment("Mainhand");
		if (!heldSword) return;
		//Celestial Sword - Launches target upwards
		if (heldSword.typeId === `manalabs_opw:celestial_sword`) {
			const { x, y, z } = victim.location;
			victim.dimension.spawnParticle("minecraft:critical_hit_emitter", {
				x,
				y: y + 1,
				z,
			});
			if (notTarget.includes(victim.typeId)) return;
			victim.runCommandAsync(`damage @s 3 contact`);
			victim.runCommandAsync(
				`particle manalabs_opw:celestial_windswept_target ~ ~ ~`,
			);
			victim.runCommandAsync(
				`playsound manalabs_opw.celestial_attack @a[r=20] ~~~ 1 1`,
			);
			victim.applyKnockback(0, 0, 0, 2);
		}
		//Cosmic Sword
		if (heldSword.typeId === `manalabs_opw:cosmic_sword`) {
			const { x, y, z } = victim.location;
			victim.dimension.spawnParticle("minecraft:critical_hit_emitter", {
				x,
				y: y + 1,
				z,
			});
			victim.runCommandAsync(
				`playsound manalabs_opw.cosmic_effect @a[r=20] ~~~ 1 1`,
			);
			victim.runCommandAsync(`spreadplayers ~ ~ 10 16 @s`);
			victim.runCommandAsync(
				`playsound mob.endermen.portal @a[r=20] ~~~ 1 1.2`,
			);
			victim.runCommandAsync(`particle minecraft:mob_portal ~ ~0.5 ~`);
		}
		//Hand Sword
		if (heldSword.typeId === `manalabs_opw:hand_sword`) {
			const { x, y, z } = victim.location;
			victim.runCommandAsync(
				`playsound manalabs_opw.handsword_attack @a[r=20] ~~~ 1 1`,
			);
			victim.dimension.spawnParticle("minecraft:critical_hit_emitter", {
				x,
				y: y + 1,
				z,
			});
			victim.setOnFire(5, true);
		}
		//Ice Sword
		if (heldSword.typeId === `manalabs_opw:ice_sword`) {
			const { x, y, z } = victim.location;
			victim.runCommandAsync(
				`playsound manalabs_opw.icesword_attack @a[r=20] ~~~ 1 1`,
			);
			victim.dimension.spawnParticle("minecraft:critical_hit_emitter", {
				x,
				y: y + 1,
				z,
			});
			victim.runCommandAsync(`effect @s slowness 5 10 false`);
		}
		//Kyro Sword
		if (heldSword.typeId === `manalabs_opw:kyro_sword`) {
			const { x, y, z } = victim.location;
			victim.runCommandAsync(
				`playsound manalabs_opw.kyro_attack @a[r=20] ~~~ 1 1`,
			);
			victim.dimension.spawnParticle("minecraft:critical_hit_emitter", {
				x,
				y: y + 1,
				z,
			});
		}
		//Laser Sword
		if (heldSword.typeId === `manalabs_opw:laser_sword`) {
			const { x, y, z } = victim.location;
			victim.runCommandAsync(
				`playsound manalabs_opw.laser_attack @a[r=20] ~~~ 1 1`,
			);
			victim.dimension.spawnParticle("minecraft:critical_hit_emitter", {
				x,
				y: y + 1,
				z,
			});
		}
		//Mystic Axe
		if (heldSword.typeId === `manalabs_opw:mystic_axe`) {
			const { x, y, z } = victim.location;
			victim.runCommandAsync(
				`playsound manalabs_opw.mysticaxe_attack @a[r=20] ~~~ 1 1`,
			);
			victim.dimension.spawnParticle("minecraft:critical_hit_emitter", {
				x,
				y: y + 1,
				z,
			});
		}
		//Poison Sword
		if (heldSword.typeId === `manalabs_opw:poison_sword`) {
			const { x, y, z } = victim.location;
			victim.runCommandAsync(
				`playsound manalabs_opw.poison_effect @a[r=20] ~~~ 1 1`,
			);
			victim.dimension.spawnParticle("minecraft:critical_hit_emitter", {
				x,
				y: y + 1,
				z,
			});
			victim.runCommandAsync(`effect @s fatal_poison 5 1 false`);
		}
		//Shadow Sword
		if (heldSword.typeId === `manalabs_opw:shadow_sword`) {
			const { x, y, z } = victim.location;
			victim.runCommandAsync(
				`playsound manalabs_opw.shadow_attack @a[r=20] ~~~ 1 1`,
			);
			victim.dimension.spawnParticle("minecraft:critical_hit_emitter", {
				x,
				y: y + 1,
				z,
			});
			victim.runCommandAsync(`effect @s fatal_poison 5 1 false`);
		}
		//Skull Sword
		if (heldSword.typeId === `manalabs_opw:skull_sword`) {
			const { x, y, z } = victim.location;
			victim.runCommandAsync(
				`playsound manalabs_opw.skullsword_attack @a[r=20] ~~~ 1 1`,
			);
			victim.dimension.spawnParticle("minecraft:critical_hit_emitter", {
				x,
				y: y + 1,
				z,
			});
			victim.runCommandAsync(`effect @s wither 5 1 false`);
			victim.applyKnockback(
				victim.location.x - attacker.location.x,
				victim.location.z - attacker.location.z,
				4,
				0,
			);
		}
		//Vampire Sword
		if (heldSword.typeId === `manalabs_opw:vampire_sword`) {
			const { x, y, z } = victim.location;
			victim.runCommandAsync(
				`playsound manalabs_opw.vampire_attack @a[r=20] ~~~ 1 1`,
			);
			victim.dimension.spawnParticle("minecraft:critical_hit_emitter", {
				x,
				y: y + 1,
				z,
			});
			attacker.runCommandAsync(`effect @s regeneration 5 1 false`);
		}
		//Cosmic Rings
		if (heldSword.typeId === `manalabs_opw:cosmic_rings`) {
			const { x, y, z } = victim.location;
			victim.dimension.spawnParticle("minecraft:critical_hit_emitter", {
				x,
				y: y + 1,
				z,
			});
		}
		//Skull Crusher
		if (heldSword.typeId === `manalabs_opw:skull_crusher`) {
			const { x, y, z } = victim.location;
			victim.runCommandAsync(
				`playsound manalabs_opw.skullcrusher_effect_strike @a[r=20] ~~~ 1 1`,
			);
			victim.dimension.spawnParticle("minecraft:critical_hit_emitter", {
				x,
				y: y + 1,
				z,
			});
			victim.runCommandAsync(`effect @s wither 5 1 false`);
			victim.applyKnockback(
				victim.location.x - attacker.location.x,
				victim.location.z - attacker.location.z,
				3,
				0.3,
			);
		}
		//Titan Blade
		if (heldSword.typeId === `manalabs_opw:titan_sword`) {
			const { x, y, z } = victim.location;
			victim.runCommandAsync(
				`playsound manalabs_opw.titanblade_attack @a[r=20] ~~~ 1 1`,
			);
			victim.dimension.spawnParticle("minecraft:critical_hit_emitter", {
				x,
				y: y + 1,
				z,
			});
			const entities = victim.dimension.getEntities({
				location: {
					x: victim.location.x,
					y: victim.location.y,
					z: victim.location.z,
				},
				maxDistance: 3,
			});
			//Run code for each entity
			entities.forEach((entity) => {
				//Ignore source player and not target array
				if (entity.name == playerName) return;
				if (notTarget.includes(entity.typeId)) return;
				//Commands to run
				entity.runCommandAsync(`damage @s 6 contact`);
				entity.applyKnockback(
					entity.location.x - victim.location.x,
					entity.location.z - victim.location.z,
					1,
					0.3,
				);
				entity.runCommandAsync(
					`particle minecraft:egg_destroy_emitter ~ ~0.5 ~`,
				);
			});
		}
	} catch (error) {}
});

world.afterEvents.entityDie.subscribe((data) => {
	const { damageSource, deadEntity: victim } = data;
	const attacker = damageSource.damagingEntity;
	const target = [
		"minecraft:cave_spider",
		"minecraft:drowned",
		"minecraft:endermite",
		"minecraft:husk",
		"minecraft:magma_cube",
		"minecraft:silverfish",
		"minecraft:skeleton",
		"minecraft:slime",
		"minecraft:spider",
		"minecraft:stray",
		"minecraft:zombie",
		"minecraft:zombie_pigman",
		"minecraft:zombie_villager",
		"minecraft:zombie_villager_v2",
		"minecraft:blaze",
		"minecraft:creeper",
		"minecraft:enderman",
		"minecraft:ghast",
		"minecraft:guardian",
		"minecraft:hoglin",
		"minecraft:phantom",
		"minecraft:shulker",
		"minecraft:vex",
		"minecraft:witch",
		"minecraft:zoglin",
		"minecraft:elder_guardian",
		"minecraft:evocation_illager",
		"minecraft:piglin",
		"minecraft:piglin_brute",
		"minecraft:pillager",
		"minecraft:ravager",
		"minecraft:vindicator",
		"minecraft:witch",
		"minecraft:wither_skeleton",
	];
	if (target.includes(victim.typeId)) {
		if (!attacker) return;
		const playerEquippableComp = attacker.getComponent("equippable");
		if (!playerEquippableComp) return;
		const heldSword = playerEquippableComp.getEquipment("Mainhand");
		if (!heldSword) return;
		//Ice Sword
		if (heldSword.typeId === `manalabs_opw:ice_sword`) {
			const { x, y, z } = victim.location;
			victim.dimension.spawnParticle("minecraft:critical_hit_emitter", {
				x,
				y: y + 1,
				z,
			});
			victim.runCommandAsync(`summon manalabs_opw:mini_snow_golem ~~~`);
		}
		//Shadow Sword
		if (heldSword.typeId === `manalabs_opw:shadow_sword`) {
			const { x, y, z } = victim.location;
			victim.dimension.spawnParticle("minecraft:critical_hit_emitter", {
				x,
				y: y + 1,
				z,
			});
			victim.runCommandAsync(
				`playsound manalabs_opw.shadow_effect @a[r=20] ~~~ 1 1`,
			);
			victim.runCommandAsync(`summon manalabs_opw:shadow_vex ~~1~`);
		}
	}
});
