import { system, world, EntityEquippableComponent, EquipmentSlot } from "@minecraft/server";

const TAG_ARMOR_EFFECT_JUMP = 'oct_plev_samurai_armor_first_jump'
const TAG_ARMOR_EFFECT_AIR = 'oct_plev_samurai_armor_in_air'
const TAG_ARMOR_EFFECT_DOUBLE_JUMP = 'oct_plev_samurai_armor_double_jumped'
const TAG_ARMOR_SWIFTSNEAK = 'oct_plev_samurai_armor_swiftsneak'

// Armor effect on samurai armor
function main() {	
	for (let player of world.getAllPlayers()) {
		const armorEquipped = player.getComponent(EntityEquippableComponent.componentId)

		const hasHelmet = armorEquipped.getEquipment(EquipmentSlot.Head)?.typeId == "oct_plev:samurai_helmet";
		const hasChestplate = armorEquipped.getEquipment(EquipmentSlot.Chest)?.typeId == "oct_plev:samurai_chestplate";
		const hasLeggings = armorEquipped.getEquipment(EquipmentSlot.Legs)?.typeId == "oct_plev:samurai_leggings";
		const hasBoots = armorEquipped.getEquipment(EquipmentSlot.Feet)?.typeId == "oct_plev:samurai_boots";

		// const hasFullArmor = hasHelmet && hasChestplate && hasLeggings && hasBoots
		// const hasAnyArmor = hasHelmet || hasChestplate || hasLeggings || hasBoots
		const hasThreeParts = ((hasHelmet && hasChestplate && hasLeggings) || (hasHelmet && hasChestplate && hasBoots) || (hasHelmet && hasLeggings && hasBoots) || (hasChestplate && hasLeggings && hasBoots))
		
		if(hasThreeParts && player.isSneaking && !player.hasTag(TAG_ARMOR_SWIFTSNEAK)) {
			player.addTag(TAG_ARMOR_SWIFTSNEAK);
			player.runCommand("/effect @s speed 10000 10 true");
		}
		else if ((!hasThreeParts || !player.isSneaking) && player.hasTag(TAG_ARMOR_SWIFTSNEAK)) {
			player.removeTag(TAG_ARMOR_SWIFTSNEAK);
			player.runCommand("/effect @s speed 0");
		}

		if(hasThreeParts && player.isJumping && !player.hasTag(TAG_ARMOR_EFFECT_JUMP)) {
			player.addTag(TAG_ARMOR_EFFECT_JUMP);
		}
		else if (!player.isOnGround && !player.isJumping && !player.isInWater && player.hasTag(TAG_ARMOR_EFFECT_JUMP) && !player.hasTag(TAG_ARMOR_EFFECT_AIR)) {
			player.addTag(TAG_ARMOR_EFFECT_AIR);
		}
		else if (player.hasTag(TAG_ARMOR_EFFECT_AIR) && (player.isOnGround || player.isInWater)) {
			player.removeTag(TAG_ARMOR_EFFECT_AIR);
			player.removeTag(TAG_ARMOR_EFFECT_DOUBLE_JUMP);
			player.removeTag(TAG_ARMOR_EFFECT_JUMP);
		}
	   	// Double Jump
	   	else if (player.isJumping && !player.isOnGround && player.hasTag(TAG_ARMOR_EFFECT_AIR) && !player.hasTag(TAG_ARMOR_EFFECT_DOUBLE_JUMP)) {
			player.addTag(TAG_ARMOR_EFFECT_DOUBLE_JUMP);
			player.runCommand("/summon oct_plev:double_jump_knockback ~ ~-0.1 ~");
	   	}
	   	else if (player.hasTag(TAG_ARMOR_EFFECT_DOUBLE_JUMP) && player.isOnGround) {
			player.removeTag(TAG_ARMOR_EFFECT_AIR);
			player.removeTag(TAG_ARMOR_EFFECT_DOUBLE_JUMP);
			player.removeTag(TAG_ARMOR_EFFECT_JUMP);
	   	}
	}
}

system.runInterval(main, 1);