package org.wyldmods.toolutilities.common.recipe;

import java.util.ArrayList;
import java.util.List;

import net.minecraft.item.ItemStack;
import net.minecraft.item.ItemTool;
import net.minecraftforge.oredict.OreDictionary;

import org.wyldmods.toolutilities.common.ToolUpgrade;

public class ToolUpgradeRecipe
{
    public final Class<? extends ItemTool> input;
    public final ItemStack modifier;
    public final ToolUpgrade upgrade;
    public final int cost;
    
    private ToolUpgradeRecipe(Class<? extends ItemTool> input, ItemStack modifier, ToolUpgrade upgrade, int cost)
    {
        this.input = input;
        this.modifier = modifier;
        this.upgrade = upgrade;
        this.cost = cost;
    }

    private static List<ToolUpgradeRecipe> recipes = new ArrayList<ToolUpgradeRecipe>();

    public static void addUpgradeRecipe(Class<? extends ItemTool> input, ItemStack modifier, ToolUpgrade upgrade)
    {
        addUpgradeRecipe(input, modifier, upgrade, 5);
    }
    
    public static void addUpgradeRecipe(Class<? extends ItemTool> input, ItemStack modifier, ToolUpgrade upgrade, int cost)
    {
        recipes.add(new ToolUpgradeRecipe(input, modifier, upgrade, cost));
    }

    public static boolean isValidInput(ItemStack input, ItemStack modifier)
    {
        if (input == null || modifier == null)
        {
            return false;
        }

        for (ToolUpgradeRecipe recipe : recipes)
        {
            if (recipe.input.isAssignableFrom(input.getItem().getClass()))
            {
                int damage = recipe.modifier.getItemDamage();
                if ((damage == OreDictionary.WILDCARD_VALUE || damage == modifier.getItemDamage()) && recipe.modifier.getItem() == modifier.getItem())
                {
                    return true; 
                }
            }
        }
        
        return false;
    }
    
    public static ToolUpgradeRecipe getOutputFor(ItemStack input, ItemStack modifier)
    {
        if (input == null || modifier == null)
        {
            return null;
        }

        for (ToolUpgradeRecipe recipe : recipes)
        {
            if (recipe.input.isAssignableFrom(input.getItem().getClass()))
            {
                int damage = recipe.modifier.getItemDamage();
                if ((damage == OreDictionary.WILDCARD_VALUE || damage == modifier.getItemDamage()) && recipe.modifier.getItem() == modifier.getItem())
                {
                    return recipe;
                }
            }
        }
        
        return null;
    }
}

@@ -1545,201 +1545,42 @@
    i
-nt damage = recipe.modifier.getItemDamage();%0A                if ((damage == OreDictionary.WILDCARD_VALUE %7C%7C damage == modifier.getItemDamage()) && recipe.modifier.getItem() == modifier.getItem(
+f (matches(recipe, input, modifier
 ))%0A 
@@ -1954,32 +1954,292 @@
 )%0A        %7B%0A    
+        if (matches(recipe, input, modifier))%0A            %7B%0A                return recipe;%0A            %7D%0A        %7D%0A        %0A        return null;%0A    %7D%0A    %0A    private static boolean matches(ToolUpgradeRecipe recipe, ItemStack input, ItemStack modifier)%0A    %7B%0A
         if (reci
@@ -2297,32 +2297,26 @@
 )))%0A        
-    %7B%0A  
+%7B%0A
             
@@ -2315,18 +2315,16 @@
         
-  
 int dama
@@ -2373,24 +2373,23 @@
         
-    
-if (
+return 
 (damage 
@@ -2513,113 +2513,70 @@
 em()
-)%0A                %7B%0A                    return recipe;%0A                %7D%0A            %7D%0A        %7D%0A        
+ && recipe.modifier.stackSize == modifier.stackSize;%0A        %7D
 %0A   
@@ -2579,30 +2579,31 @@
 %0A        return 
-null
+false
 ;%0A    %7D%0A%7D%0A

Fix anvil recipes taking whole stacks instead of strict amounts