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

world.afterEvents.itemUse.subscribe((data) => {
	const item = data.itemStack;
	const player = data.source;
	//Check item and cooldown
	if (
		item.typeId === `manalabs_opw:mystic_axe` &&
		item.getComponent("minecraft:cooldown").getCooldownTicksRemaining(player) >
			38
	) {
		//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.mysticaxe_effect @a[r=20] ~~~ 1 1`,
		);
		//Delay commands to match animation
		system.runTimeout(() => {
			//Play sound and particles
			player.runCommandAsync(
				`playsound manalabs_opw.mysticaxe_effect_strike @a[r=20] ~~~ 0.9 0.6`,
			);
			player.runCommandAsync(
				`particle minecraft:critical_hit_emitter ~ ~0.5 ~`,
			);
			const spell = player.dimension.spawnEntity(
				`manalabs_opw:spell_amethyst`,
				{
					x: player.location.x + player.getViewDirection().x * 4,
					y: player.location.y,
					z: player.location.z + player.getViewDirection().z * 4,
				},
			);
			const interval = system.runInterval(() => {
				if (spell.isValid() == true) {
					spell.runCommandAsync(
						`damage @e[type=!item,family=!dummy,name=!"${player.name}",r=2] 4 contact`,
					);
				} else {
					system.clearRun(interval);
				}
			}, 1);
		}, 8);

		system.runTimeout(() => {
			//Play sound and particles
			player.runCommandAsync(
				`playsound manalabs_opw.mysticaxe_effect_strike @a[r=20] ~~~ 0.7 0.8`,
			);

			const spell = player.dimension.spawnEntity(
				`manalabs_opw:spell_amethyst`,
				{
					x: player.location.x + player.getViewDirection().x * 6,
					y: player.location.y,
					z: player.location.z + player.getViewDirection().z * 6,
				},
			);
			const interval = system.runInterval(() => {
				if (spell.isValid() == true) {
					spell.runCommandAsync(
						`damage @e[type=!item,family=!dummy,name=!"${player.name}",r=2] 4 contact`,
					);
				} else {
					system.clearRun(interval);
				}
			}, 1);
		}, 12);

		system.runTimeout(() => {
			//Play sound and particles
			player.runCommandAsync(
				`playsound manalabs_opw.mysticaxe_effect_strike @a[r=20] ~~~ 0.5 1`,
			);
			player.runCommandAsync(
				`particle minecraft:critical_hit_emitter ~ ~0.5 ~`,
			);
			player.runCommandAsync(
				`titleraw @p actionbar { \"rawtext\": [ { \"translate\" : \"actionbar.manalabs_opw:mystic_axe\" } ] }`,
			);
			const spell = player.dimension.spawnEntity(
				`manalabs_opw:spell_amethyst`,
				{
					x: player.location.x + player.getViewDirection().x * 8,
					y: player.location.y,
					z: player.location.z + player.getViewDirection().z * 8,
				},
			);
			const interval = system.runInterval(() => {
				if (spell.isValid() == true) {
					spell.runCommandAsync(
						`damage @e[type=!item,family=!dummy,name=!"${player.name}",r=2] 4 contact`,
					);
				} else {
					system.clearRun(interval);
				}
			}, 1);
		}, 16);
	}
});
