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",
	];
	//Check item and cooldown
	if (
		item.typeId === `manalabs_opw:cosmic_rings` &&
		item.getComponent("minecraft:cooldown").getCooldownTicksRemaining(player) >
			78
	) {
		//Play animations
		player.playAnimation(`animation.manalabs_opw.cosmic_rings_attack`, {
			nextState: `none`,
			blendOutTime: 1,
			stopExpression: `query.any_animation_finished`,
			controller: `controller_name1`,
		});
		player.runCommandAsync(`particle manalabs_opw:cosmic_rings_bubble ~ ~ ~`);
		player.runCommandAsync(`playsound manalabs_opw.bubble_rings @a ~~~`);
		//Delay commands to match animation
		//Bubble 1
		system.runTimeout(() => {
			//Play sound and particles

			player.runCommandAsync(
				`particle manalabs_opw:cosmic_rings_bubble ~ ~0.5 ~`
			);
			player.runCommandAsync(
				`titleraw @p actionbar { \"rawtext\": [ { \"translate\" : \"actionbar.manalabs_opw:bubble_beam\" } ] }`
			);
			const spell1 = player.dimension.spawnEntity(`manalabs_opw:spell_bubble`, {
				x: player.location.x + player.getViewDirection().x,
				y: player.location.y,
				z: player.location.z + player.getViewDirection().z,
			});
			spell1.setRotation({
				x: player.getRotation().x,
				y: player.getRotation().y,
			});
			const viewDirection = player.getViewDirection();
			const interval = system.runInterval(() => {
				if (spell1.isValid() == true) {
					// This need optimization, but this will pass CR when it comes to player name and spaces in it
					spell1.runCommandAsync(
						`damage @e[type=!item,family=!dummy,name=!"${player.name}",r=3] 8 magic`
					);
					spell1.runCommandAsync(
						`effect @e[type=!item,family=!dummy,name=!"${player.name}",r=3] levitation 3 3`
					);
					const impulse = {
						x: viewDirection.x * 0.5,
						y: viewDirection.y * 0.5,
						z: viewDirection.z * 0.5,
					};
					spell1.setRotation(viewDirection);
					spell1.applyImpulse(impulse);
					const entities = player.dimension.getEntities({
						location: {
							x: spell1.location.x,
							y: spell1.location.y,
							z: spell1.location.z,
						},
						maxDistance: 1,
					});
					//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(`playsound bubble.up @a[r=8] ~~~ 2 1`);
						entity.runCommandAsync(
							`particle manalabs_opw:cosmic_rings_bubble ~ ~2 ~`
						);
					});
				} else {
					system.clearRun(interval);
				}
			}, 1);
		}, 8);

		//Bubble 2
		system.runTimeout(() => {
			//Play sound and particles
			const spell2 = player.dimension.spawnEntity(`manalabs_opw:spell_bubble`, {
				x: player.location.x + player.getViewDirection().x,
				y: player.location.y,
				z: player.location.z + player.getViewDirection().z,
			});
			spell2.setRotation({
				x: player.getRotation().x,
				y: player.getRotation().y,
			});
			const viewDirection = player.getViewDirection();
			const interval = system.runInterval(() => {
				if (spell2.isValid() == true) {
					// This need optimization, but this will pass CR when it comes to player name and spaces in it
					spell2.runCommandAsync(
						`damage @e[type=!item,family=!dummy,name=!"${player.name}",r=3] 8 magic`
					);
					spell2.runCommandAsync(
						`effect @e[type=!item,family=!dummy,name=!"${player.name}",r=3] levitation 3 3`
					);
					const impulse = {
						x: viewDirection.x * 0.5,
						y: viewDirection.y * 0.5,
						z: viewDirection.z * 0.5,
					};
					spell2.setRotation(viewDirection);
					spell2.applyImpulse(impulse);
					const entities = player.dimension.getEntities({
						location: {
							x: spell2.location.x,
							y: spell2.location.y,
							z: spell2.location.z,
						},
						maxDistance: 1,
					});
					//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(`playsound bubble.up @a[r=8] ~~~ 2 1`);
						entity.runCommandAsync(
							`particle manalabs_opw:cosmic_rings_bubble ~ ~2 ~`
						);
					});
				} else {
					system.clearRun(interval);
				}
			}, 1);
		}, 16);
		//Bubble 3
		system.runTimeout(() => {
			//Play sound and particles
			const spell3 = player.dimension.spawnEntity(`manalabs_opw:spell_bubble`, {
				x: player.location.x + player.getViewDirection().x,
				y: player.location.y,
				z: player.location.z + player.getViewDirection().z,
			});
			spell3.setRotation({
				x: player.getRotation().x,
				y: player.getRotation().y,
			});
			const viewDirection = player.getViewDirection();
			const interval = system.runInterval(() => {
				if (spell3.isValid() == true) {
					// This need optimization, but this will pass CR when it comes to player name and spaces in it
					spell3.runCommandAsync(
						`damage @e[type=!item,family=!dummy,name=!"${player.name}",r=3] 8 magic`
					);
					spell3.runCommandAsync(
						`effect @e[type=!item,family=!dummy,name=!"${player.name}",r=3] levitation 3 3`
					);

					const impulse = {
						x: viewDirection.x * 0.5,
						y: viewDirection.y * 0.5,
						z: viewDirection.z * 0.5,
					};
					spell3.setRotation(viewDirection);
					spell3.applyImpulse(impulse);
					const entities = player.dimension.getEntities({
						location: {
							x: spell3.location.x,
							y: spell3.location.y,
							z: spell3.location.z,
						},
						maxDistance: 1,
					});
					//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(`playsound bubble.up @a[r=8] ~~~ 2 1`);
						entity.runCommandAsync(
							`particle manalabs_opw:cosmic_rings_bubble ~ ~2 ~`
						);
					});
				} else {
					system.clearRun(interval);
				}
			}, 1);
		}, 25);
	}
});
