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

const EFFECT_DURATION = 200000;

export async function onTick() {
    const players = world.getAllPlayers();
    for (const player of players) {
		const equipment = player.getComponent("equippable");
        const hasHelmet     = await isWearingItem(equipment, "Head", "goe_bos:boss_helmet");
        const hasChestplate = await isWearingItem(equipment, "Chest", "goe_bos:boss_chestplate");
        const hasLeggings   = await isWearingItem(equipment, "Legs", "goe_bos:boss_leggings");
        const hasBoots      = await isWearingItem(equipment, "Feet", "goe_bos:boss_boots");
		const hasFullArmor  = hasHelmet && hasChestplate && hasLeggings && hasBoots;
		
		await applyNightVisionEffect(player, hasHelmet);

		await applyStrengthEffect(player, hasChestplate);
		
		await applyHealthBoostEffect(player, hasFullArmor);
		
		await applySpeedEffect(player, hasBoots);

        await applyFireResistanceEffect(player, hasLeggings);
    }
}

async function applyHealthBoostEffect(player, hasFullArmor) {
	let hasArmorEffect = await player.hasTag("goe_bos_has_armor_effect");
	let hasHealthBoost = await player.getEffect("health_boost");
	
	if (hasFullArmor && (!hasArmorEffect || !hasHealthBoost)) {

		//armor effect
		await player.addEffect("health_boost", EFFECT_DURATION, {amplifier:1, showParticles:false});
		utils.runPlayerCommand(player,"effect @s instant_health 1 1 true");
		utils.runPlayerCommand(player,"playsound goe_bos:armor_effect_activate @s");
		await player.addTag("goe_bos_has_armor_effect");

		//visual effect
		// let playerLocation = new Vector3.Vector3(player.location);
		// player.dimension.spawnEntity("goe_bos:armor_glowing_circle", playerLocation);
		// utils.runPlayerCommand(player, "ride @e[type=goe_bos:armor_glowing_circle,r=1,c=1] start_riding @s");
	}
	if (!hasFullArmor && hasArmorEffect) {
		await player.removeEffect("health_boost");
		await player.removeTag("goe_bos_has_armor_effect");
		await player.removeTag("goe_bos_first_equip_sound");
		//utils.runPlayerCommand(player, "event entity @e[type=goe_bos:armor_glowing_circle,rm=1,c=1] goe_bos:despawn");
	}
}

async function applyNightVisionEffect(player, hasHelmet) {
	let hasHelmetEffect = await player.hasTag("goe_bos_has_helmet_effect");
	let hasNightVision = await player.getEffect("night_vision");
	
	if (hasHelmet && (!hasHelmetEffect || !hasNightVision)) {
		await player.addEffect("night_vision", EFFECT_DURATION, {amplifier:0, showParticles:false});
		await player.addTag("goe_bos_has_helmet_effect");
	}
	if (!hasHelmet && hasHelmetEffect) {
		await player.removeEffect("night_vision");
		await player.removeTag("goe_bos_has_helmet_effect");
	}
}

async function applyStrengthEffect(player, hasChestplate) {
	let hasChestplateEffect = await player.hasTag("goe_bos_has_chestplate_effect");
	let hasStrenght = await player.getEffect("strength");
	
	if (hasChestplate && (!hasChestplateEffect || !hasStrenght)) {
		await player.addEffect("strength", EFFECT_DURATION, {amplifier:0, showParticles:false});
		await player.addTag("goe_bos_has_chestplate_effect");
	}
	if (!hasChestplate && hasChestplateEffect) {
		await player.removeEffect("strength");
		await player.removeTag("goe_bos_has_chestplate_effect");
	}
}

async function applySpeedEffect(player, hasBoots) {
	
	let hasBootsEffect = await player.hasTag("goe_bos_has_boots_effect");
	let hasSpeed = player.getEffect("speed");
	
	if (hasBoots && (!hasBootsEffect || !hasSpeed))
	{
		await player.addEffect("speed", EFFECT_DURATION, {amplifier: 0, showParticles:false});
		await player.addTag("goe_bos_has_boots_effect");
	}
	
	if (!hasBoots && hasBootsEffect)
	{
		await player.removeEffect("speed");
		await player.removeTag("goe_bos_has_boots_effect");
	}
}


async function applyFireResistanceEffect(player, hasLeggings) {
	let hasLeggingsEffect = await player.hasTag("goe_bos_has_leggings_effect");
	
	if (hasLeggings && !hasLeggingsEffect)
	{
		await player.addEffect("fire_resistance", EFFECT_DURATION, {amplifier: 0, showParticles:false});
		await player.addTag("goe_bos_has_leggings_effect");
	}
	
	if (!hasLeggings && hasLeggingsEffect)
	{
		await player.removeEffect("fire_resistance");
		await player.removeTag("goe_bos_has_leggings_effect");
	}
}


async function isWearingItem(equipment, slotName, itemName)
{
	let item = await equipment.getEquipment(slotName);
	if (item === undefined) return false;
	if (item.typeId === itemName) return true;
	return false;
}