package com.zivilon.cinder_loe;

import lotr.common.LOTRMod;
import net.minecraft.init.Items;
import net.minecraft.item.Item;

import lotr.common.item.LOTRMaterial;

import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.lang.reflect.Field;

public class Materials {
    public static void registerMaterials() {
        modifyMaterial("RED_DWARF", 700, 3.0F, 0.7F, 3, 7.0F, 10, CinderLoE.redDwarfSteel);
        modifyMaterial("WIZARD", 1000, 3.0F, 0.7F, 3, 7.0F, 10, null);
        modifyMaterial("BONEMOLD", 450, 2.5F, 0.6F, 2, 6.0F, 10, CinderLoE.bonemold); //Original Durability = 350 | Add 81 | 162 if Upgraded Armory | 2/4 (Unupgraded)
        modifyMaterial("LIMWAITH_WOOD", 350, 1.5F, 0.5F, 2, 5.0F, 10, Item.getItemFromBlock(LOTRMod.driedReeds));
        modifyMaterial("EVENT", 2400, 5.0F, 0.8F, 0, 9.0F, 10, LOTRMod.emerald);
        modifyMaterial("BREE", 350, 2.5F, 0.6F, 2, 6.0F, 10, Items.iron_ingot);
        modifyMaterial("RHUDAUR", 450, 2.5F, 0.6F, 2, 6.0F, 10, LOTRMod.fur);
        modifyMaterial("BATTLENUN", 450, 3F, 0.6F, 2, 6.0F, 10, Items.iron_ingot);
        modifyMaterial("ASH", 2000, 0.0F, 0.6F, 2, 7.0F, 10, CinderLoE.ingotAsh);
        modifyMaterial("SERPENT", 700, 2.0F, 0.6F, 2, 6.0F, 10, LOTRMod.bronze);
        modifyMaterial("USURPER",700, 2.0F, 0.6F, 2, 6.0F, 10, Items.iron_ingot);
        modifyMaterial("WARLORD", 700, 2.0F, 0.6F, 2, 6.0F, 10, LOTRMod.bronze);
        modifyMaterial("DORWINIONELITE", 700, 2.0F, 0.6F, 2, 6.0F, 10, LOTRMod.elfSteel);
        modifyMaterial("ESGAROTH",700, 2.0F, 0.6F, 2, 6.0F, 10, Items.iron_ingot);
        modifyMaterial("JADE",1000, 5.0F, 0.8F, 0, 9.0F, 10, LOTRMod.emerald);
        modifyMaterial("GILDEDGALVORN",700, 3.0F, 0.6F, 2, 7.0F, 15, LOTRMod.galvorn);
        modifyMaterial("ARTIFACT",2000, 0.0F, 0.8F, 2, 7.0F, 15, LOTRMod.galvorn);
        modifyMaterial("SUNLIT_WARDEN",1500, 5.0F, 0.7F, 2, 7.0F, 15, LOTRMod.mithril);
        modifyMaterial("ORTHANC_GUARD",700, 2.0F, 0.7F, 2, 6.0F, 10, LOTRMod.urukSteel);
    }

    public static void modifyMaterial(String fieldName, int uses, float weapon_damage, float protection, int harvest_level, float speed, int enchantability, Item crafting_item) {
        try {
            Class<?> lotrMaterialClass = Class.forName("lotr.common.item.LOTRMaterial");
            Field materialField = lotrMaterialClass.getField(fieldName);

            // Reflection to access and instantiate the private constructor of LOTRMaterial
            Constructor<?> constructor = lotrMaterialClass.getDeclaredConstructor(String.class);
            constructor.setAccessible(true);
            Object materialLocal = constructor.newInstance(fieldName);

            // Reflection to call the private methods on the new instance
            Method setUses = lotrMaterialClass.getDeclaredMethod("setUses", int.class);
            Method setDamage = lotrMaterialClass.getDeclaredMethod("setDamage", float.class);
            Method setProtection = lotrMaterialClass.getDeclaredMethod("setProtection", float.class);
            Method setHarvestLevel = lotrMaterialClass.getDeclaredMethod("setHarvestLevel", int.class);
            Method setSpeed = lotrMaterialClass.getDeclaredMethod("setSpeed", float.class);
            Method setEnchantability = lotrMaterialClass.getDeclaredMethod("setEnchantability", int.class);
            Method setCraftingItem = lotrMaterialClass.getDeclaredMethod("setCraftingItem", Item.class);
          //  Method setManFlesh = lotrMaterialClass.getDeclaredMethod("canHarvestManFlesh", boolean.class);

            setUses.setAccessible(true);
            setDamage.setAccessible(true);
            setProtection.setAccessible(true);
            setHarvestLevel.setAccessible(true);
            setSpeed.setAccessible(true);
            setEnchantability.setAccessible(true);
            setCraftingItem.setAccessible(true);
           // setManFlesh.setAccessible(true);

            setUses.invoke(materialLocal, uses);
            setDamage.invoke(materialLocal, weapon_damage);
            setProtection.invoke(materialLocal, protection);
            setHarvestLevel.invoke(materialLocal, harvest_level);
            setSpeed.invoke(materialLocal, speed);
            setEnchantability.invoke(materialLocal, enchantability);
           // setManFlesh.invoke(materialLocal, canHarvestManFlesh);
            if (crafting_item != null) {
                setCraftingItem.invoke(materialLocal, crafting_item);
            }

            materialField.set(null, materialLocal);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
