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:skull_sword` &&
		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.skullsword_effect @a[r=20] ~~~ 1 1`,
		);
		//Delay commands to match animation

		system.runTimeout(() => {
			player.runCommandAsync(`playsound skullsword_explosion @a[r=20] ~~~ 1 1`);
			player.runCommandAsync(
				`titleraw @p actionbar { \"rawtext\": [ { \"translate\" : \"actionbar.manalabs_opw:skull_sword\" } ] }`,
			);
			shoot_skull_1(player);
		}, 8);
		system.runTimeout(() => {
			player.runCommandAsync(`playsound skullsword_explosion @a[r=20] ~~~ 1 1`);
			shoot_skull_2(player);
		}, 12);
		system.runTimeout(() => {
			player.runCommandAsync(`playsound skullsword_explosion @a[r=20] ~~~ 1 1`);
			shoot_skull_3(player);
		}, 16);
	}

	function shoot_skull_1(player) {
		const viewDirection = player.getViewDirection();
		const HeadLocation = player.getHeadLocation();
		const projectile = player.dimension.spawnEntity(
			"manalabs_opw:spell_skull",
			HeadLocation,
		);
		const impulse = {
			x: viewDirection.x + viewDirection.x * 1.2,
			y: viewDirection.y,
			z: viewDirection.z,
		};
		projectile.applyImpulse(impulse);

		system.runInterval(() => {
			if (projectile.isValid() == true) {
				projectile.runCommandAsync(
					`effect @e[type=!item,family=!dummy,name=!"${player.name}",r=5] wither 10 1 false`,
				);
			}
		}, 1);
	}

	function shoot_skull_2(player) {
		const viewDirection = player.getViewDirection();
		const HeadLocation = player.getHeadLocation();
		const projectile = player.dimension.spawnEntity(
			"manalabs_opw:spell_skull",
			HeadLocation,
		);
		const impulse = {
			x: viewDirection.x,
			y: viewDirection.y,
			z: viewDirection.z,
		};
		projectile.applyImpulse(impulse);

		system.runInterval(() => {
			if (projectile.isValid() == true) {
				projectile.runCommandAsync(
					`effect @e[type=!item,family=!dummy,name=!"${player.name}",r=5] wither 10 1 false`,
				);
			}
		}, 1);
	}

	function shoot_skull_3(player) {
		const viewDirection = player.getViewDirection();
		const HeadLocation = player.getHeadLocation();
		const projectile = player.dimension.spawnEntity(
			"manalabs_opw:spell_skull",
			HeadLocation,
		);
		const impulse = {
			x: viewDirection.x,
			y: viewDirection.y,
			z: viewDirection.z + viewDirection.z * 1.2,
		};
		projectile.applyImpulse(impulse);

		system.runInterval(() => {
			if (projectile.isValid() == true) {
				projectile.runCommandAsync(
					`effect @e[type=!item,family=!dummy,name=!"${player.name}",r=5] wither 10 1 false`,
				);
			}
		}, 1);
	}
});
