import { EquipmentSlot, system, world } from "@minecraft/server";
import { CAMOUFLAGE_POTION } from "../items/specific/camouflagePotion";
import { CACTUS_SPIKE_TRAPS } from "../blocks/common/cactusSpikeTraps";
import { VENOMITE_ARMOR } from "../items/common/venomiteArmor";
import { COBALTIUM_ARMOR } from "../items/common/cobaltiumArmor";
import { DUSKITE_ARMOR } from "../items/common/duskiteArmor";
import { IGUANA_BOOTS } from "../items/common/iguanaBoots";
import { HEALTH_ESSENCE } from "../items/specific/healthEssense";
import { CUSTOM_TORCHES } from "../blocks/common/customTorches";
import { REPLACED_ITEMS } from "../items/common/replacedItems";
import { EntityComponents as EComponents } from "../entities/utils/entityComponents";
import { WorldHelper as WHelper } from "./utils/worldHelper";
import { EntityTypeIds } from "../entities/utils/entityTypesIds";

// No Delay
system.runInterval(() => { 
    const players = world.getAllPlayers()
    for (const player of players) {
        const equippable = EComponents.getEquippable(player);
        const inventory = EComponents.getInventory(player);

        const offhandItem = equippable.getEquipment(EquipmentSlot.Offhand);
        const bootsArmor = equippable.getEquipment(EquipmentSlot.Feet)

        const offhandItemId = offhandItem?.typeId
        
        // Camouflage Potion
        if (CAMOUFLAGE_POTION.isCamouflaging(player)) CAMOUFLAGE_POTION.applyConsumeEffect(player)
        // Health Essense
        if (HEALTH_ESSENCE.isOffhandEquipped(offhandItemId)) HEALTH_ESSENCE.applyOffhandEffect(player)
        // Venomite Armor
        if (VENOMITE_ARMOR.isArmorActive(equippable, false)) VENOMITE_ARMOR.poisonInvulnerableEffect(player)
        // Cobaltium Armor
        if (COBALTIUM_ARMOR.isFullArmorEquipped(equippable)) COBALTIUM_ARMOR.applyFullEquipEffect(player)
        // Duskite Armor
        if (DUSKITE_ARMOR.shouldApplyArmorEffect(equippable)) DUSKITE_ARMOR.applyEquippedEffect(player)
        // Iguana Boots
        if (IGUANA_BOOTS.isFrostWalkTrying(player, bootsArmor)) IGUANA_BOOTS.frostWalk(player, bootsArmor)
        // Cactus Spike Trap
        if (CACTUS_SPIKE_TRAPS.shouldRunDamage()) CACTUS_SPIKE_TRAPS.steppedDamage(player)
        // Custom Torches
        if (CUSTOM_TORCHES.shouldLightUp()) CUSTOM_TORCHES.lightUp(player)
        // Custom Placers
        REPLACED_ITEMS.runReplace(inventory, equippable)
    }

    const dummies = WHelper.getAllEntities({type: EntityTypeIds.dummy})
    for (const dummy of dummies) {
        // Block Ager
        if (IGUANA_BOOTS.shouldAgeBlock(dummy)) IGUANA_BOOTS.ageBlock(dummy)
    }
})
