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

world.afterEvents.itemUse.subscribe((data) => {
	const item = data.itemStack;
	const player = data.source;
	const dir = player.getViewDirection();

	//Check item and cooldown
	if (
		item.typeId === `manalabs_opw:titan_sword` &&
		item.getComponent("minecraft:cooldown").getCooldownTicksRemaining(player) >
			38
	) {
		player.runCommandAsync(
			`playsound manalabs_opw.titanblade_effect @a[r=20] ~~~ 1 1`,
		);
		//Play animations
		player.playAnimation(`animation.manalabs_opw.titan_spin`, {
			nextState: `none`,
			blendOutTime: 1,
			stopExpression: `query.is_on_ground`,
			controller: `controller_name1`,
		});
		player.playAnimation(`animation.manalabs_opw.titan_spin`, {
			nextState: `none`,
			blendOutTime: 1,
			stopExpression: `query.is_on_ground`,
			controller: `controller_name2`,
		});
		//Delay commands to match animation
		system.runTimeout(() => {
			//Play sound and particles
			player.runCommandAsync(
				`playsound manalabs_opw.titanblade_attack @a[r=20] ~~~ 1 1`,
			);
			player.runCommandAsync(
				`titleraw @p actionbar { \"rawtext\": [ { \"translate\" : \"actionbar.manalabs_opw:titan_sword\" } ] }`,
			);
			player.spinning = true;
			//Commands to run
			player.runCommandAsync(`particle manalabs_opw:titan_explode ~ ~ ~`);
			player.runCommandAsync(`particle manalabs_opw:titan_dust ~ ~ ~`);
			player.runCommandAsync(`effect @s resistance 2 3 true`);
			player.applyKnockback(dir.x, dir.z, 9, dir.y + 0.5);
		}, 8);
	}
});

system.runInterval(() => {
	const players = world.getPlayers({});
	for (let t = 0; t < players.length; t++) {
		const player = players[t];
		if (player.spinning == true) {
			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 = player.name;
			const entities = player.dimension.getEntities({
				location: {
					x: player.location.x,
					y: player.location.y,
					z: player.location.z,
				},
				maxDistance: 4,
			});
			//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 8 contact`);
				entity.applyKnockback(
					entity.location.x - player.location.x,
					entity.location.z - player.location.z,
					1.2,
					0.3,
				);
				entity.runCommandAsync(
					`particle minecraft:critical_hit_emitter ~ ~0.5 ~`,
				);
			});
			player.runCommandAsync(`particle manalabs_opw:titan_trail ~ ~0.1 ~`);
		}
		if (player.spinning == true && player.isOnGround) {
			system.runTimeout(() => {
				player.spinning = false;
			}, 5);
		}
	}
});
