import { world, system, EquipmentSlot } from "@minecraft/server";
import * as utils from "./utils";
import * as boss_hoglin_warrior_axe from "./boss_hoglin_warrior_axe";
import * as boss_bone_blade from "./boss_bone_blade";
import * as arcanite_armor from "./arcanite_armor";
import * as attack_animations from "./attack_animations";
import * as dungeons from "./dungeons";
import * as dungeon_particles from "./dungeon_particles";
import { onTick as skeleton_king } from "./skeleton_king";
import * as spider_web from "./spider_web";
import * as chest from "./chest";
import * as wither_projectile from "./wither_projectile";
import * as portal from "./portal";
import * as unholy_dagger from "./weapons/unholy_dagger";
import * as spider_blade from "./weapons/spider_blade";
import * as wraith_reaper from "./weapons/wraith_reaper";
import * as necromancers_staff from "./weapons/necromancers_staff";
import * as frostblizzard_scepter from "./weapons/frostblizzard_scepter";
import * as wand_of_void from "./weapons/wand_of_void";
import * as hit_effects from "./hit_effects";
import * as book from "./book";
import * as despawn from "./despawn";
import * as rewards from "./rewards";
import * as necromancer from "./necromancer";

export async function onLoadFirstTime() {
	await utils.runCommand("function goe/bos/create_objectives");
	await utils.runCommand("function goe/bos/set_scores");
}

export async function onLoad() {
}

export async function onTick() {
    await boss_hoglin_warrior_axe.onTick();
    await arcanite_armor.onTick();
	await skeleton_king();
	await dungeons.onTick();
	await spider_web.onTick();
	await portal.onTick();
	await dungeon_particles.onTick();

	const dungeonEntity = utils.player0.dimension.getEntities({
		type: "goe_bos:dungeon",
		location: utils.player0.location,
		closest: 1
	})[0];

	if (!dungeonEntity) return;

	const offset = {
		x: utils.player0.location.x - dungeonEntity.location.x,
		y: utils.player0.location.y - dungeonEntity.location.y,
		z: utils.player0.location.z - dungeonEntity.location.z
	};

	// utils.debug(offset.x.toFixed(2) + " " + offset.y.toFixed(2) + " " + offset.z.toFixed(2) + " " + utils.player0.getRotation().y.toFixed(2));
}

export async function onPlayerSpawn(event) {
	utils.runCommand("function goe/bos/set_scores");
	dungeons.onPlayerSpawn(event);
	portal.onPlayerSpawn(event);
}

export async function onPlayerJoin(event) {
}

export async function onEntitySpawn(event) {
	dungeons.onEntitySpawn(event);
}

export async function onEntityHealthChanged(event) {
}

export async function onEntityHurt(event) {
	const damageDealer = event.damageSource.damagingEntity;
	if(damageDealer === undefined)
		return;

	const hurtEntity = event.hurtEntity;

    if (event.damageSource.cause === "entityAttack") {
		const equipment = event.damageSource.damagingEntity.getComponent("minecraft:equippable");
		if (equipment !== undefined) {
            const itemStack = equipment.getEquipment(EquipmentSlot.Mainhand);
			if (itemStack !== undefined) {
				if (attack_animations.weapons.includes(itemStack.typeId)) {
					attack_animations.onEntityHurt(event, itemStack.typeId);
				}
			}
		}
	}

	if (damageDealer.typeId === "minecraft:player" && hurtEntity.typeId.startsWith("goe_bos") && hurtEntity.getComponent("minecraft:type_family")?.hasTypeFamily("monster"))
		hit_effects.onEntityHurt(event);

	if (damageDealer.typeId === "goe_bos:frost_projectile")
		frostblizzard_scepter.onEntityHurt(event);
}

export async function onProjectileHitEntity(event) {
}

export async function onEntityDie(event) {
	const deadEntity = event.deadEntity.typeId;
	if (deadEntity === "minecraft:player") {
		boss_hoglin_warrior_axe.onEntityDie(event);
		dungeons.onEntityDie(event);
	}
}

export async function onItemStartUse(event) {
    const item = event.itemStack.typeId;
    if(item === "goe_bos:boss_bone_blade"){
        boss_bone_blade.onItemUse(event);
    } 
    else if(item === "goe_bos:boss_hoglin_warrior_axe") {
        boss_hoglin_warrior_axe.onItemUse(event)
    }
}

export async function onItemUse(event) {
	const item = event.itemStack.typeId;
	if(item === "goe_bos:spider_nest" || 
	   item === "goe_bos:piglins_lair" ||
	   item === "goe_bos:haunted_tomb" ||
	   item === "goe_bos:wardens_chamber" ||
	   item === "goe_bos:necromancer_ruins" ||
	   item === "goe_bos:frostbite_cavern" ||
	   item === "goe_bos:forbidden_crypt" ||
	   item === "goe_bos:void_sanctum")
	{
		portal.onItemUse(event);
	}

	if (item === "goe_bos:necromancer_dagger")
		unholy_dagger.onItemUse(event);

	if (item === "goe_bos:spider_blade")
		spider_blade.onItemUse(event);

	if (item === "goe_bos:necromancers_staff")
		necromancers_staff.onItemUse(event);
	
	if (item === "goe_bos:frostblizzard_scepter")
		frostblizzard_scepter.onItemUse(event);

	if (item === "goe_bos:wraith_reaper")
		wraith_reaper.onItemUse(event);

	if (item === "goe_bos:wand_of_void")
		wand_of_void.onItemUse(event);

	if (item === "goe_bos:book")
		book.onItemUse(event);
}

export async function onItemUseBefore(event) {
}

export async function onItemStopUse(event) {
}

export async function onItemReleaseUse(event) {
}


export async function onEntityHitBlock(event) {
}

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

export async function onPlayerBreakBlock(event) {
	if (event.itemStackAfterBreak === undefined)
		return;

	if (event.brokenBlockPermutation.type.id === "goe_bos:spider_web")
		await spider_web.onPlayerBreakBlock(event);
}
export async function onScriptEventReceive(event) {
	if (event.id === "goe_bos:portal")
		await portal.onScriptEventReceive(event);

	if (event.id === "goe_bos:chest")
		await chest.onScriptEventReceive(event);

	if (event.id === "goe_bos:wither_projectile")
		await wither_projectile.onScriptEventReceive(event);

	if (event.id === "goe_bos:dungeon")
		await dungeons.onScriptEventReceive(event);

	if (event.id === "goe_bos:despawn")
		await despawn.onScriptEventReceive(event);

	if (event.id === "goe_bos:reward")
		await rewards.onScriptEventReceive(event);

	if (event.id === "goe_bos:necromancer")
		await necromancer.onScriptEventReceive(event);
}

export async function onPlayerInteractWithEntity(event) {
	//utils.debug(`onPlayerInteractWithEntity: `);
	//let player = event.player;
	//let entity = event.target;
	//if (event.target === undefined)
	//	utils.debug(`onPlayerInteractWithEntity: location ${player.location.x} ${player.location.z} entity undefined `);
	//else
	//	utils.debug(`onPlayerInteractWithEntity: location ${player.location.x} ${player.location.z} entity ${entity.typeId} `);
}