import { world, system, EquipmentSlot } from "@minecraft/server";
import * as furniture from "./furniture";
import * as structure from "./structure";
import * as mob_spawner from "./mob_spawner";
import * as furniture_spawner from "./furniture_spawner";
import * as book from "./book";
import * as warriors_armor from "./warriors_armor";
import * as weapons from "./weapons";
import * as quest from "./quest";

export async function onLoad() {
}

export async function onTick() {
	await structure.onTick();
	await mob_spawner.onTick();
	await warriors_armor.onTick();
	await furniture_spawner.onTick()
	await furniture.onTick();
	await quest.onTick();
}

export async function onEntityLoad(event) {
}

export async function onPlayerSpawn(event) {
}

export async function onEntitySpawn(event) {
}

export async function onEntityHealthChanged(event) {
}

export async function onEntityHurt(event) {

	if (event.hurtEntity.typeId === "minecraft:player") 
		warriors_armor.onEntityHurt(event);
	weapons.onEntityHurt(event);
}

export async function onEntityHitEntity(event) {
	weapons.onEntityHitEntity(event);
}

export async function onEntityDie(event) {
	await quest.onEntityDie(event);
}

export async function onItemUse(event) {
	
	/*if (event.itemStack !== undefined && event.itemStack.typeId === "minecraft:iron_sword")
	{
		let dir = event.source.getViewDirection();
		event.source.applyKnockback(dir.x, dir.z, Math.sqrt(dir.x ** 2 + dir.z ** 2)*28, dir.y*6);
	}*/
	weapons.onItemUse(event);
	
}

export async function onItemStartUse(event) {
	if (event.itemStack === undefined)
		return;
}

export async function onItemStopUse(event) {
	if (event.itemStack === undefined) {
		// empty hand events go here
		return;
	}

}

export async function onItemUseOn(event) {
	if (event.itemStack === undefined)
        return;

}

export async function onEntityHitBlock(event) {

}


export async function onPlayerBreakBlock(event) {
	await quest.onPlayerBreakBlock(event);
}

export async function onPlayerInteractWithEntity(event) {
	await furniture.onPlayerInteractWithEntity(event);
	if (event.target.typeId === "tcs_ohs:quest_giver") {
		await quest.onPlayerInteractWithEntity(event);
	}
}

export async function onScriptEventReceive(event) {
	if (event.id === "tcs_ohs:hit_book" || event.id === "tcs_ohs:open_book")
       book.onScriptEventReceive(event);

	if (event.id === "tcs_ohs:furniture_action")
		furniture.onScriptEventReceive(event);
	
	if (event.id === "tcs_ohs:structure_load")
		structure.onScriptEventReceive(event);

	if (event.sourceEntity !== undefined && event.sourceEntity.typeId === "tcs_ohs:quest_giver")
		quest.onScriptEventReceive(event);
	
	if (event.id === "tcs_ohs:mob_spawner_load")
		mob_spawner.onScriptEventReceive(event);

	if (event.id === "tcs_ohs:furniture_load")
		furniture_spawner.onScriptEventReceive(event);
}
