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

world.afterEvents.itemUse.subscribe((data) => {
	const item = data.itemStack;
	const player = data.source;
	//Get player name to filter out when ability triggers
	const playerName = player.name;
	//Array of entities to ignore
	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",
	];
	//Check item and cooldown
	if (
		item.typeId === `manalabs_opw:shadow_sword` &&
		item.getComponent("minecraft:cooldown").getCooldownTicksRemaining(player) >
			78
	) {
		//Play animations
		player.playAnimation(`animation.manalabs_opw.first_person_sword_attack`, {
			nextState: `none`,
			blendOutTime: 1,
			stopExpression: `query.any_animation_finished`,
			controller: `controller_name1`,
		});
		player.playAnimation(`animation.manalabs_opw.third_person_sword_attack`, {
			nextState: `none`,
			blendOutTime: 1,
			stopExpression: `query.any_animation_finished`,
			controller: `controller_name2`,
		});
		player.runCommandAsync(
			`playsound manalabs_opw.shadow_effect @a[r=12] ~~~ 1 1`,
		);
		//Delay commands to match animation
		system.runTimeout(() => {
			//Play sound and particles
			player.runCommandAsync(
				`titleraw @p actionbar { \"rawtext\": [ { \"translate\" : \"actionbar.manalabs_opw:shadow_sword\" } ] }`,
			);
			const spell = player.dimension.spawnEntity(`manalabs_opw:spell_shadow`, {
				x: player.location.x + player.getViewDirection().x * 4,
				y: player.location.y,
				z: player.location.z + player.getViewDirection().z * 4,
			});
			player.runCommandAsync(
				`playsound manalabs_opw.shadow_attack @a[r=12] ~~~ 1 1`,
			);
			spell.runCommandAsync(`particle manalabs_opw:shadow_accretion ~ ~0.1 ~`);
			spell.runCommandAsync(`particle manalabs_opw:shadow_ring ~ ~0.1 ~`);
			spell.runCommandAsync(`particle manalabs_opw:shadow_dust ~ ~0.1 ~`);
			const interval = system.runInterval(() => {
				if (spell.isValid() == true && spell.isOnGround) {
					const entities = spell.dimension.getEntities({
						location: {
							x: spell.location.x,
							y: spell.location.y,
							z: spell.location.z,
						},
						maxDistance: 8,
					});
					spell.runCommandAsync(
						`damage @e[type=!item,family=!dummy,name=!${player.name},r=2] 6 magic`,
					);
					//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;
						if (entity == spell) return;
						const tpLocation = {
							x: (spell.location.x + entity.location.x) / 2,
							y: (spell.location.y + entity.location.y) / 2,
							z: (spell.location.z + entity.location.z) / 2,
						};
						//Commands to run
						entity.teleport(tpLocation);
					});
				} else {
					system.clearRun(interval);
				}
			}, 10);
		}, 10);
	}
});
