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

export async function onItemUse(event)
{
	const player = event.source;
	if(!player.hasTag("goe_bos_has_boots_effect"))
		utils.runPlayerCommand(player, "effect @s speed 30 0 true");
	if(!player.hasTag("goe_bos_has_chestplate_effect"))
		utils.runPlayerCommand(player, "effect @s strength 30 0 true");
	utils.runPlayerCommand(player, "effect @s resistance 30 0 true");

	const frontLocation = {
		x: player.location.x + player.getViewDirection().x * 2,
		y: player.location.y,
		z: player.location.z + player.getViewDirection().z * 2
	};
	player.dimension.spawnParticle(`goe_bos:hoglin_head`, frontLocation);

	utils.runPlayerCommand(player, "/title @s title ＀");
	utils.runPlayerCommand(player, "/title @s subtitle §l§4   Unleash the Fury!＀");

	let playerLocation = new Vector3.Vector3(player.location);
	player.dimension.spawnEntity("goe_bos:axe_glowing_circle", playerLocation);
	utils.runPlayerCommand(player, "ride @e[type=goe_bos:axe_glowing_circle,r=1,c=1] start_riding @s");
	utils.runPlayerCommand(player, "tag @e[type=goe_bos:axe_glowing_circle,c=1] add invunerable");
	
	utils.runPlayerCommand(player, "scoreboard players set @s furyTimer 0");
	player.addTag("hasFury");

	// Play third person animation
	utils.runPlayerCommand(player, `playanimation @s animation.goe_bos.boss_hoglin_warrior_axe.activate animation.goe_bos.player.fp_attack_v2 0 v.is_first_person`);
	utils.runPlayerCommand(player, `playanimation @s animation.goe_bos.player.fp_attack_v2 animation.goe_bos.boss_hoglin_warrior_axe.activate 0 "!v.is_first_person"`);
	utils.runPlayerCommand(player, `playanimation @s animation.goe_bos.boss_hoglin_warrior_axe.activate animation.goe_bos.player.empty 0.15 "q.all_animations_finished"`);
    utils.runPlayerCommand(player, `playanimation @s animation.goe_bos.player.fp_attack_v2 animation.goe_bos.player.empty 0.15 "q.all_animations_finished"`);
	utils.runPlayerCommand(player, "playsound goe_bos:boss_hoglin_warrior_axe_activate_ability @s")
	
}

export async function onEntityDie(event) {
	const player = event.deadEntity;
	const deathLocation = event.deadEntity.location;
	utils.runCommand(`event entity @e[x=${deathLocation.x},y=${deathLocation.y},z=${deathLocation.z},type=goe_bos:axe_glowing_circle] goe_bos:despawn`);
	player.removeTag("hasFury");
}

export async function onTick() {
	for (const player of world.getAllPlayers())
		utils.runPlayerCommand(player, "execute as @s[tag=hasFury] run function goe/bos/fury_countdown");
}