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

const armorType = {
    "lore_hrc:dark_matter": "fire_resistance",
    "lore_hrc:lumen" : "speed",
}



export function armorEffect(player) {
    const equippable = player.getComponent("minecraft:equippable")
    const armor = {
        helmet : equippable.getEquipment(EquipmentSlot.Head) ? equippable.getEquipment(EquipmentSlot.Head).typeId : undefined,
        boots :  equippable.getEquipment(EquipmentSlot.Feet) ? equippable.getEquipment(EquipmentSlot.Feet).typeId : undefined,
        chestplate :  equippable.getEquipment(EquipmentSlot.Chest) ? equippable.getEquipment(EquipmentSlot.Chest).typeId : undefined,
        leggings : equippable.getEquipment(EquipmentSlot.Legs) ? equippable.getEquipment(EquipmentSlot.Legs).typeId : undefined,
    }

    const preHelmet = player.getDynamicProperty("helmet") ? player.getDynamicProperty("helmet") : armor.helmet
    const preChestplate = player.getDynamicProperty("chestplate") ? player.getDynamicProperty("chestplate") : armor.chestplate
    const preLeggings = player.getDynamicProperty("leggings") ? player.getDynamicProperty("leggings") : armor.leggings
    const preBoots = player.getDynamicProperty("boots") ? player.getDynamicProperty("boots") : armor.boots
    
    for (const key in armorType) {
        if (!armor.helmet || !armor.chestplate || !armor.boots || !armor.leggings) {
            player.setDynamicProperty("lore_hrc:dark_set", false)
            player.setDynamicProperty("lore_hrc:lumen_set", false)
            return
        }
        const armorSet =
        armor.helmet.includes(key) &&
        armor.chestplate.includes(key) &&
        armor.leggings.includes(key) &&
        armor.boots.includes(key)
        
        if (preHelmet != armor.helmet && preHelmet.includes(key)) {
            player.setDynamicProperty("lore_hrc:dark_set", false)
            player.setDynamicProperty("lore_hrc:lumen_set", false)
            return
        }
        if (preChestplate != armor.chestplate && preChestplate.includes(key)) {
            player.setDynamicProperty("lore_hrc:dark_set", false)
            player.setDynamicProperty("lore_hrc:lumen_set", false)
            return
        }
        if (preLeggings != armor.leggings && preLeggings.includes(key)) {
            player.setDynamicProperty("lore_hrc:dark_set", false)
            player.setDynamicProperty("lore_hrc:lumen_set", false)
            return
        }
        if (preBoots != armor.boots && preBoots.includes(key)) {
            player.setDynamicProperty("lore_hrc:dark_set", false)
            player.setDynamicProperty("lore_hrc:lumen_set", false)
            return
        }
        
        if (armorSet && key.includes("dark_matter")) {
            player.setDynamicProperty("lore_hrc:dark_set", true)
        }
        else if (armorSet && key.includes("lumen")) {
            player.setDynamicProperty("lore_hrc:lumen_set", true)
        }
    }

    player.setDynamicProperty("helmet", armor.helmet) 
    player.setDynamicProperty("chestplate", armor.chestplate) 
    player.setDynamicProperty("leggings", armor.leggings) 
    player.setDynamicProperty("boots", armor.boots) 
}