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

const ARMOR_RESISTANCE_TIERS = {
    tier1_chestplates: {
        items: ["linked:kooa", "linked:mka3"],
        fullSetAmplifier: 3,
        chestAmplifier: 2,  // Chestplate alone = Resistance III
        helmetAmplifier: 1,
        leggingsAmplifier: 1,
        bootsAmplifier: 1
    },

    tier2_chestplates: {
        items: [
            "linked:vota", "linked:aoaa", "linked:koda", "linked:ca",
            "linked:mka2", "linked:mka", "linked:dswa", "linked:ika2",
            "linked:vga", "linked:hta", "linked:sotua", "linked:aga",
            "linked:wka2", "linked:nka", "linked:ika", "linked:wka", "linked:mka2_1", "linked:mka3_1"
        ],
        fullSetAmplifier: 2,
        chestAmplifier: 1,
        helmetAmplifier: 0,
        leggingsAmplifier: 1,
        bootsAmplifier: 0
    },
    
    tier3_chestplates: {
        items: ["linked:ia"],
        fullSetAmplifier: 4,
        chestAmplifier: 3,
        helmetAmplifier: 2,
        leggingsAmplifier: 2,
        bootsAmplifier: 1
    },
    
    tier4_chestplates: {
        items: ["linked:ia2"],
        fullSetAmplifier: 5,
        chestAmplifier: 4,
        helmetAmplifier: 2,
        leggingsAmplifier: 2,
        bootsAmplifier: 2
    },
    
    tier5_chestplates: {
        items: ["linked:ba", "linked:sa3", "linked:tsa"],
        fullSetAmplifier: 1,
        chestAmplifier: 1,
        helmetAmplifier: 0,
        leggingsAmplifier: 0,
        bootsAmplifier: 0
    }
};

const ARMOR_SET_MAPPING = {
    "linked:kooa": { h: "linked:koogh", l: "linked:kool", b: "linked:koog" },
    "linked:mka3": { h: "linked:mkh3", l: "linked:mkl3", b: "linked:mkcg3" },
    
    "linked:vota": { h: "linked:voth", l: "linked:votl", b: "linked:votg" },
    "linked:aoaa": { h: "linked:aoah", l: "linked:aoal", b: "linked:aoag" },
    "linked:koda": { h: "linked:kodh", l: "linked:kodl", b: "linked:kodg" },
    "linked:ca": { h: "linked:ch", l: "linked:cl", b: "linked:cb" },
    "linked:mka2": { h: "linked:mkh2", l: "linked:mkl2", b: "linked:mkcg2" },
    "linked:mka": { h: "linked:mkh", l: "linked:mkl", b: "linked:mkcg" },
    "linked:dswa": { h: "linked:dsws", l: "linked:dsl", b: "linked:dswg" },
    "linked:ika2": { h: "linked:ih2", l: "linked:il2", b: "linked:ig2" },
    "linked:vga": { h: "linked:vgh", l: "linked:vgl", b: "linked:vgg" },
    "linked:hta": { h: "linked:htgh", l: "linked:htl", b: "linked:htg" },
    "linked:sotua": { h: "linked:sotuh", l: "linked:sotul", b: "linked:sotug" },
    "linked:aga": { h: "linked:agh", l: "linked:agl", b: "linked:agg" },
    "linked:wka2": { h: "linked:wka", l: "linked:wkl", b: "linked:wkg" },
    "linked:nka": { h: "linked:nkh", l: "linked:nkl", b: "linked:nkg" },
    "linked:ika2": { h: "linked:ika", l: "linked:ikl", b: "linked:ikg" },
    "linked:mka3_1": { h: "linked:mkh3_1", l: "linked:mkl3_1", b: "linked:mkcg3_1" },
    "linked:mka2_1": { h: "linked:mkh2_1", l: "linked:mkl2_1", b: "linked:mkcg2_1" },
    
    "linked:ia": { h: "linked:ih", l: "linked:il", b: "linked:ig" },
    
    "linked:ia2": { h: "linked:ih2", l: "linked:il2", b: "linked:ig2" },
    
    "linked:ba": { h: "linked:bh", l: "linked:bl", b: "linked:bb" },
    "linked:sa3": { h: "linked:shb", l: "linked:sl2", b: "linked:sg" },
    "linked:tsa": { h: "linked:tsh", l: "linked:tsl", b: "linked:tsg" }
};

function getArmorTier(chestplateId) {
    for (const [tierKey, tierData] of Object.entries(ARMOR_RESISTANCE_TIERS)) {
        if (tierData.items.includes(chestplateId)) {
            return tierData;
        }
    }
    return null;
}

function calculateResistanceAmplifier(player) {
    const equipment = player.getComponent("minecraft:equippable");
    if (!equipment) return 0;
    
    const chestplate = equipment.getEquipment(EquipmentSlot.Chest);
    if (!chestplate) return 0;
    
    const chestId = chestplate.typeId;
    const tier = getArmorTier(chestId);
    if (!tier) return 0;
    
    const armorSet = ARMOR_SET_MAPPING[chestId];
    if (!armorSet) return tier.chestAmplifier;
    const helmet = equipment.getEquipment(EquipmentSlot.Head);
    const leggings = equipment.getEquipment(EquipmentSlot.Legs);
    const boots = equipment.getEquipment(EquipmentSlot.Feet);
    
    const hasHelmet = helmet && helmet.typeId === armorSet.h;
    const hasLeggings = leggings && leggings.typeId === armorSet.l;
    const hasBoots = boots && boots.typeId === armorSet.b;
    
    if (hasHelmet && hasLeggings && hasBoots) {
        return tier.fullSetAmplifier;
    }
    
    return Math.max(0, tier.chestAmplifier - 1);
}

system.runInterval(() => {
    for (const player of world.getAllPlayers()) {
        try {
            const amplifier = calculateResistanceAmplifier(player);
            
            if (amplifier > 0) {
                player.addEffect("resistance", 2, {
                    amplifier: amplifier,
                    showParticles: false
                });
            } else {
                const hasResistance = player.getEffect("resistance");
                if (!hasResistance) {
                    player.removeEffect("resistance");
                }
            }
            
        } catch (error) {
            console.warn("Error applying resistance:", error);
        }
    }
}, 1);