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

export async function onTick() {
	let containers = await game.getEntities("tcs_ohs:dark_oak_wardrobe");
	for(let container of containers)
		await tryToCloseContainer(container);
	containers = await game.getEntities("tcs_ohs:spruce_wardrobe");
	for(let container of containers)
		await tryToCloseContainer(container);
	containers = await game.getEntities("tcs_ohs:treasure_chest");
	for(let container of containers)
		await tryToCloseContainer(container);
	containers = await game.getEntities("tcs_ohs:reward_chest");
	for(let container of containers)
		await tryToCloseContainer(container);
}
export async function tryToCloseContainer(container) {
	if (container === undefined)
		return;
	let interactionState = container.getProperty("tcs_ohs:interact_animation");
	if (interactionState != 2)
		return;
	let nearbyPlayers = await game.getPlayersInRange(container.dimension, container.location, 0, 5);
	if (nearbyPlayers === undefined || nearbyPlayers.length == 0)
		await game.runMobCommand(container, "event entity @s tcs_ohs:interact_by_player");
}

export async function onScriptEventReceive(event) {
	
	let furniture = event.sourceEntity;
	let message = event.message;

	if (message === "fix_rotate")
		{
			let rot = furniture.getRotation();
			rot.y = vector3.rotationToFixedRotation(rot);
	
			game.runMobCommand(furniture, `tp @s ~ ~0.1 ~ ${rot.y} ${rot.x}`);
			//furniture.setRotation(rot);
			
			if (furniture.typeId === "tcs_ohs:treasure_chest")
				furniture.nameTag = "";
		}
		
	if (message === "hit")
	{
		let hits = furniture.getDynamicProperty("tcs_ohs_hits");
		hits = hits === undefined ? 1 : hits + 1;
		furniture.setDynamicProperty("tcs_ohs_hits", hits);
		game.playSound(furniture, "@a", "break.azalea", hits);

		if (hits == 3)
			system.runTimeout(() => { removeFurniture(furniture); }, 8);
	}
	
}
export async function removeFurniture(furniture)
{
	await game.playSound(furniture, "@a", "block.itemframe.break");
	await game.runMobCommand(furniture, "fill ~ ~ ~ ~ ~1 ~ air replace light_block");

	const inventory = furniture.getComponent("minecraft:inventory");
	if (inventory !== undefined)
	{
		const container = inventory.container;
		for(let slot=0; slot<inventory.inventorySize; slot++)
		{
			let item = container.getItem(slot);
			if (item !== undefined)
				furniture.dimension.spawnItem(item, furniture.location);
		}
	}

	furniture.triggerEvent("tcs_ohs:remove");
}

export async function onPlayerInteractWithEntity(event) {

	
	if (event.target.typeId === "tcs_ohs:spruce_wardrobe" || event.target.typeId === "tcs_ohs:dark_oak_wardrobe" ||
		event.target.typeId === "tcs_ohs:treasure_chest" || event.target.typeId === "tcs_ohs:reward_chest" )
	{
		let interactionState = event.target.getProperty("tcs_ohs:interact_animation");
		if (interactionState === 0)
		{
			game.runMobCommand(event.target, "event entity @s tcs_ohs:interact_by_player");
			if (event.target.typeId === "tcs_ohs:treasure_chest" && event.target.nameTag === "Treasure chest")
			{
				system.runTimeout(() => { game.runMobCommand(event.target, "playsound tcs_ohs:hint @a[r=10]"); }, 8);
			}
			else if (event.target.typeId === "tcs_ohs:reward_chest")
			{
				system.runTimeout(() => { game.runMobCommand(event.target, "playsound tcs_ohs:hint @a[r=10]"); }, 8);
			}
		}
	}
	/*if (event.target.typeId === "tcs_ohs:red_villagers_bed" || event.target.typeId === "tcs_ohs:yellow_villagers_bed" ||
		event.target.typeId === "tcs_ohs:green_villagers_bed" || event.target.typeId === "tcs_ohs:red_kings_bed" || 
		event.target.typeId === "tcs_ohs:yellow_kings_bed" || event.target.typeId === "tcs_ohs:green_kings_bed" )
	{
		//game.playSound(event.target, "@a[r=10]", "tcs_ohs:bed");
		system.run(() => { event.player.runCommand(`playanimation @s animation.tcs_ohs.sleeping none 0 "!query.is_riding"`)});
	}*/
	/*system.runTimeout(() => { 
		game.runMobCommand(event.target, "playanimation @s animation.tcs_ohs.furniture.hit1");
		game.runMobCommand(event.target, "playsound tcs_ohs:bed @a[r=10]"); 
		game.runMobCommand(event.target, "particle tcs_ohs:grave ^ ^0.5 ^1"); 
	}, 1);*/
	    
}