import { world, system, ItemStack } from "@minecraft/server";
import * as utils from "./utils";


export async function ItemUse(event)
{
	const player = event.source;

	const inventory = player.getComponent("minecraft:inventory").container;
	const cooldown = event.itemStack.getComponent("minecraft:cooldown");
	let itemCount = 0;
	let slotsWithArrows = [];
	let amountOfArrowsPerSlot = [];
	for (let i = 0; i < inventory.size; i++) {
		let item = inventory.getItem(i);
		if (item !== undefined && item.typeId === "minecraft:arrow"){
			itemCount += item.amount;
			slotsWithArrows.push(i);
			amountOfArrowsPerSlot.push(inventory.getItem(i).amount);
		}
	}

	if(itemCount > 0){
		if(amountOfArrowsPerSlot[0] > 1) {
			let currentArrowsAmount = amountOfArrowsPerSlot[0] - 1;
			let newArrowsAmount = new ItemStack("minecraft:arrow", currentArrowsAmount);
			inventory.setItem(slotsWithArrows[0], newArrowsAmount);
		}
		else {
			inventory.setItem(slotsWithArrows[0], undefined);
		}
		if (player.getViewDirection().x > 0 && player.getViewDirection().z > 0){
			const location = { x: player.location.x + 1, y: player.location.y + 1, z: player.location.z + 1 };
			let arrowentity = world.getDimension('overworld').spawnEntity("minecraft:arrow", location);
			let arrowProjectileComp = arrowentity.getComponent('minecraft:projectile');
			let velocity = {x: player.getViewDirection().x, y: player.getViewDirection().y + 0.25, z: player.getViewDirection().z};

			arrowProjectileComp.shoot( velocity );
			utils.runPlayerCommand(player, `playsound pu_se:skeleton_shield_use @a[r=5] ~ ~ ~ 1.00 1.00 1.00`);
		}
		if (player.getViewDirection().x <= 0 && player.getViewDirection().z > 0){
			const location = { x: player.location.x - 1, y: player.location.y + 1, z: player.location.z + 1 };
			let arrowentity = world.getDimension('overworld').spawnEntity("minecraft:arrow", location);
			let arrowProjectileComp = arrowentity.getComponent('minecraft:projectile');
			let velocity = {x: player.getViewDirection().x, y: player.getViewDirection().y + 0.25, z: player.getViewDirection().z};

			arrowProjectileComp.shoot( velocity );
			utils.runPlayerCommand(player, `playsound pu_se:skeleton_shield_use @a[r=5] ~ ~ ~ 1.00 1.00 1.00`);
		}
		if (player.getViewDirection().x > 0 && player.getViewDirection().z <= 0){
			const location = { x: player.location.x + 1, y: player.location.y + 1, z: player.location.z - 1 };
			let arrowentity = world.getDimension('overworld').spawnEntity("minecraft:arrow", location);
			let arrowProjectileComp = arrowentity.getComponent('minecraft:projectile');
			let velocity = {x: player.getViewDirection().x, y: player.getViewDirection().y + 0.25, z: player.getViewDirection().z};

			arrowProjectileComp.shoot( velocity );
			utils.runPlayerCommand(player, `playsound pu_se:skeleton_shield_use @a[r=5] ~ ~ ~ 1.00 1.00 1.00`);
		}
		if (player.getViewDirection().x <= 0 && player.getViewDirection().z <= 0){
			const location = { x: player.location.x - 1, y: player.location.y + 1, z: player.location.z - 1 };
			let arrowentity = world.getDimension('overworld').spawnEntity("minecraft:arrow", location);
			let arrowProjectileComp = arrowentity.getComponent('minecraft:projectile');
			let velocity = {x: player.getViewDirection().x, y: player.getViewDirection().y + 0.25, z: player.getViewDirection().z};

			arrowProjectileComp.shoot( velocity );
			utils.runPlayerCommand(player, `playsound pu_se:skeleton_shield_use @a[r=5] ~ ~ ~ 1.00 1.00 1.00`);
		}
		cooldown.startCooldown(event.source)
	}
	else {
		utils.runPlayerCommand(player, "titleraw @s actionbar {\"rawtext\":[{\"translate\":\"sheleton_shield.message\"}]}");
	}
}