package com.zivilon.cinder_loe.mixins;

import com.zivilon.cinder_loe.enchants.LOTREnchantmentWeakProtectionRanged;
import lotr.common.enchant.LOTREnchantment;
import lotr.common.enchant.LOTREnchantmentDamage;
import lotr.common.enchant.LOTREnchantmentProtectionRanged;
import lotr.common.enchant.LOTREnchantmentRangedDamage;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

import java.lang.reflect.Field;
import java.util.Map;

@Mixin(LOTREnchantment.class)
public class MixinLOTREnchantment {
    @Inject(method = "<clinit>", at = @At("TAIL"))
    private static void onStaticInit(CallbackInfo ci) {
        // Add your new enchantments here
        try {
            LOTREnchantment protectRangedWeak1 = new LOTREnchantmentWeakProtectionRanged("protectRangedWeak1", -1).setEnchantWeight(0);
            LOTREnchantment protectRangedWeak2 = new LOTREnchantmentWeakProtectionRanged("protectRangedWeak2", -2).setEnchantWeight(0);
            LOTREnchantment rangedWeak3 = new LOTREnchantmentRangedDamage("rangedWeak3", 0.25f);
            LOTREnchantment weak4 = new LOTREnchantmentDamage("weak4", -3.0f).setEnchantWeight(0);

            LOTREnchantment.allEnchantments.add(protectRangedWeak1);
            LOTREnchantment.allEnchantments.add(protectRangedWeak2);
            rangedWeak3.allEnchantments.add(rangedWeak3);
            LOTREnchantment.allEnchantments.add(weak4);

            Field enchantsByNameField = LOTREnchantment.class.getDeclaredField("enchantsByName");
            enchantsByNameField.setAccessible(true);
            @SuppressWarnings("unchecked")
            Map<String, LOTREnchantment> enchantsByName = (Map<String, LOTREnchantment>) enchantsByNameField.get(null);

            enchantsByName.put(protectRangedWeak1.enchantName, protectRangedWeak1);
            enchantsByName.put(protectRangedWeak2.enchantName, protectRangedWeak2);
            enchantsByName.put(rangedWeak3.enchantName, rangedWeak3);
            enchantsByName.put(weak4.enchantName, weak4);

        } catch (NoSuchFieldException | IllegalAccessException e) {
            e.printStackTrace();
        }
    }

}

