package com.zivilon.cinder_loe.mixins;

import com.zivilon.cinder_loe.enchants.LOTREnchantmentExtraSpecial;
import com.zivilon.cinder_loe.enchants.LOTREnchantmentWeakProtectionRanged;
import com.zivilon.cinder_loe.enchants.LOTREnchantmentArmorSpecial;
import com.zivilon.cinder_loe.util.Utilities;
import lotr.common.enchant.*;
import lotr.common.entity.npc.LOTREntityNPC;
import net.minecraft.item.ItemStack;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Overwrite;
import org.spongepowered.asm.mixin.Shadow;
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 {
    @Shadow(remap = false)
    private boolean bypassAnvilLimit;

    @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 chill = new LOTREnchantmentExtraSpecial("strong5", 1.0f).setEnchantWeight(0);
            LOTREnchantment chill = new LOTREnchantmentDamage("strong5", 3.25f).setEnchantWeight(0);
            LOTREnchantment rangedStrong4 = new LOTREnchantmentRangedDamage("rangedStrong4", 1.4f).setEnchantWeight(0).setSkilful();
            LOTREnchantment meleeReach2 = new LOTREnchantmentMeleeReach("meleeReach2", 1.33f).setEnchantWeight(0).setSkilful();
            LOTREnchantment meleeSpeed2 = new LOTREnchantmentMeleeSpeed("meleeSpeed2", 1.33f).setEnchantWeight(0).setSkilful();
            LOTREnchantment swiftness = new LOTREnchantmentArmorSpecial("swiftness").setEnchantWeight(0).setSkilful();
            LOTREnchantment fireRepair = new LOTREnchantmentArmorSpecial("fireRepair").setEnchantWeight(0).setSkilful();
            LOTREnchantment mountArmor = new LOTREnchantmentArmorSpecial("mountArmor").setEnchantWeight(0).setSkilful();
            LOTREnchantment stealth = new LOTREnchantmentArmorSpecial("stealth").setEnchantWeight(0).setSkilful();
            LOTREnchantment meleeSturdy = new LOTREnchantmentWeaponSpecial("meleeSturdy").setEnchantWeight(0).setValueModifier(1);
            LOTREnchantment armorSturdy = new LOTREnchantmentArmorSpecial("armorSturdy").setEnchantWeight(0).setValueModifier(1);
            LOTREnchantment baneNPC = new LOTREnchantmentBane("baneNPC", 4.0f, LOTREntityNPC.class).setUnachievable().setEnchantWeight(0);
            LOTREnchantment wrath = new LOTREnchantmentWeaponSpecial("wrath").setEnchantWeight(0);


            LOTREnchantment.allEnchantments.add(baneNPC);
            LOTREnchantment.allEnchantments.add(protectRangedWeak1);
            LOTREnchantment.allEnchantments.add(protectRangedWeak2);
            rangedWeak3.allEnchantments.add(rangedWeak3);
            LOTREnchantment.allEnchantments.add(weak4);
            LOTREnchantment.allEnchantments.add(chill);
            LOTREnchantment.allEnchantments.add(rangedStrong4);
            LOTREnchantment.allEnchantments.add(meleeReach2);
            LOTREnchantment.allEnchantments.add(meleeSpeed2);
            LOTREnchantment.allEnchantments.add(meleeSturdy);
            LOTREnchantment.allEnchantments.add(armorSturdy);
            LOTREnchantment.allEnchantments.add(swiftness);
            LOTREnchantment.allEnchantments.add(fireRepair);
            LOTREnchantment.allEnchantments.add(mountArmor);
            LOTREnchantment.allEnchantments.add(stealth);
            LOTREnchantment.allEnchantments.add(wrath);

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

            enchantsByName.put(baneNPC.enchantName, baneNPC);
            enchantsByName.put(protectRangedWeak1.enchantName, protectRangedWeak1);
            enchantsByName.put(protectRangedWeak2.enchantName, protectRangedWeak2);
            enchantsByName.put(rangedWeak3.enchantName, rangedWeak3);
            enchantsByName.put(weak4.enchantName, weak4);
            enchantsByName.put(chill.enchantName, chill);
            enchantsByName.put(rangedStrong4.enchantName, rangedStrong4);
            enchantsByName.put(meleeReach2.enchantName, meleeReach2);
            enchantsByName.put(meleeSpeed2.enchantName, meleeSpeed2);
            enchantsByName.put(meleeSturdy.enchantName, meleeSturdy);
            enchantsByName.put(armorSturdy.enchantName, armorSturdy);
            enchantsByName.put(swiftness.enchantName, swiftness);
            enchantsByName.put(fireRepair.enchantName, fireRepair);
            enchantsByName.put(mountArmor.enchantName, mountArmor);
            enchantsByName.put(stealth.enchantName, stealth);
            enchantsByName.put(wrath.enchantName, wrath);

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

    /**
    * @author MrJeep20
    * @reason Bad enchantments will not count towards 3 modifier limit now
    **/
    @Overwrite(remap = false)
    public boolean bypassAnvilLimit() {
        if (Utilities.isBadEnch((LOTREnchantment)(Object)this))
            return true;
        return this.bypassAnvilLimit;
    }

}

