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


export async function giveEnglishBook()
{
	for (const player of world.getPlayers()) {
		const inventory = player.getComponent("minecraft:inventory").container;
		let slotsWithBook = [];
		for (let i = 0; i < inventory.size; i++) {
			let item = inventory.getItem(i);
			if (item !== undefined && item.typeId === "pu_sg:book"){
				slotsWithBook.push(i);
			}
		}

		if(slotsWithBook.length > 0){
			for (let i = 0; i < slotsWithBook.length; i++){
				let englishBook = new ItemStack("pu_sg:book_english");
				inventory.setItem(slotsWithBook[i], englishBook);
			}
		}
	}
}
export async function giveFrenchBook()
{
	for (const player of world.getPlayers()) {
		const inventory = player.getComponent("minecraft:inventory").container;
		let slotsWithBook = [];
		for (let i = 0; i < inventory.size; i++) {
			let item = inventory.getItem(i);
			if (item !== undefined && item.typeId === "pu_sg:book"){
				slotsWithBook.push(i);
			}
		}

		if(slotsWithBook.length > 0){
			for (let i = 0; i < slotsWithBook.length; i++){
				let frenchBook = new ItemStack("pu_sg:book_french");
				inventory.setItem(slotsWithBook[i], frenchBook);
			}
		}
	}
}
export async function giveGermanBook()
{
	for (const player of world.getPlayers()) {
		const inventory = player.getComponent("minecraft:inventory").container;
		let slotsWithBook = [];
		for (let i = 0; i < inventory.size; i++) {
			let item = inventory.getItem(i);
			if (item !== undefined && item.typeId === "pu_sg:book"){
				slotsWithBook.push(i);
			}
		}

		if(slotsWithBook.length > 0){
			for (let i = 0; i < slotsWithBook.length; i++){
				let germanBook = new ItemStack("pu_sg:book_german");
				inventory.setItem(slotsWithBook[i], germanBook);
			}
		}
	}
}
export async function giveSpanishBook()
{
	for (const player of world.getPlayers()) {
		const inventory = player.getComponent("minecraft:inventory").container;
		let slotsWithBook = [];
		for (let i = 0; i < inventory.size; i++) {
			let item = inventory.getItem(i);
			if (item !== undefined && item.typeId === "pu_sg:book"){
				slotsWithBook.push(i);
			}
		}

		if(slotsWithBook.length > 0){
			for (let i = 0; i < slotsWithBook.length; i++){
				let spanishBook = new ItemStack("pu_sg:book_spanish");
				inventory.setItem(slotsWithBook[i], spanishBook);
			}
		}
	}
}
export async function givePortugueseBook()
{
	for (const player of world.getPlayers()) {
		const inventory = player.getComponent("minecraft:inventory").container;
		let slotsWithBook = [];
		for (let i = 0; i < inventory.size; i++) {
			let item = inventory.getItem(i);
			if (item !== undefined && item.typeId === "pu_sg:book"){
				slotsWithBook.push(i);
			}
		}

		if(slotsWithBook.length > 0){
			for (let i = 0; i < slotsWithBook.length; i++){
				let portugeseBook = new ItemStack("pu_sg:book_portuguese");
				inventory.setItem(slotsWithBook[i], portugeseBook);
			}
		}
	}
}

export async function onTick() {
    for(let player of world.getAllPlayers()) {
        if(await isHoldingBook(player)) {
            if (!player.hasTag("pu_sg_guidebook")) {
                player.addTag("pu_sg_guidebook");
                utils.playSound(player, "@s", "pu_sg:guide_book_open", 1.0, 1.0, 1.0);
            }
        } else if (player.hasTag("pu_sg_guidebook")) {
            player.removeTag("pu_sg_guidebook");
            utils.playSound(player, "@s", "pu_sg:guide_book_close", 1.0, 1.0, 1.0);
        }
    }
}

async function isHoldingBook(player){
	const equipment = player.getComponent("equippable");
	let itemInHand = equipment.getEquipment(EquipmentSlot.Mainhand);
	if(itemInHand === undefined) return false;
    if(itemInHand.typeId === "pu_sg:book_english" || itemInHand.typeId === "pu_sg:book_spanish" || itemInHand.typeId === "pu_sg:book_portuguese" || itemInHand.typeId === "pu_sg:book_german" || itemInHand.typeId === "pu_sg:book_french" ) {
        return true;
    }
}