package crazypants.enderio.conduit.item.filter;

import net.minecraft.inventory.InventoryCrafting;
import net.minecraft.item.ItemStack;
import net.minecraft.item.crafting.IRecipe;
import net.minecraft.world.World;
import net.minecraftforge.oredict.RecipeSorter;
import net.minecraftforge.oredict.RecipeSorter.Category;
import crazypants.enderio.conduit.item.FilterRegister;

public class CopyFilterRecipe implements IRecipe{
  
  static {
    RecipeSorter.register("EnderIO:copyFilter", CopyFilterRecipe.class, Category.SHAPELESS, "after:minecraft:shapeless");
  }

  private ItemStack output;
  
  @Override
  public boolean matches(InventoryCrafting inv, World world) {
    
    ItemStack blankInput = null;
    ItemStack filterInput = null;
    for (int i = 0; i < inv.getSizeInventory(); i++) {
      ItemStack checkStack = inv.getStackInSlot(i);
      if (checkStack != null && checkStack.getItem() instanceof IItemFilterUpgrade) {
        if(FilterRegister.isFilterSet(checkStack)) {
          if(filterInput != null || !isSameTypeOrNull(blankInput, checkStack)) {
            return false;
          }          
          filterInput = checkStack;
        } else {
          if(blankInput != null || !isSameTypeOrNull(filterInput, checkStack)) {
            return false;
          }
          blankInput = checkStack;
        }
      }
    }
    
    if(blankInput == null || filterInput == null) {      
      return false;
    }
    output = filterInput.copy();
    output.stackSize = 2;
    return true;

  }

  private boolean isSameTypeOrNull(ItemStack matchOrNull, ItemStack checkStack) {
    return matchOrNull == null || (matchOrNull.getItem() == checkStack.getItem() && matchOrNull.getItemDamage() == checkStack.getItemDamage());
  }

  @Override
  public ItemStack getCraftingResult(InventoryCrafting inv) {
    return output.copy();
  }

  @Override
  public int getRecipeSize() {
    return 1;
  }

  @Override
  public ItemStack getRecipeOutput() {
    return output;
  }

}

@@ -678,35 +678,26 @@
     
-ItemStack blankInput = null
+int blankCount = 0
 ;%0A  
@@ -1010,53 +1010,8 @@
 null
- %7C%7C !isSameTypeOrNull(blankInput, checkStack)
 ) %7B%0A
@@ -1047,26 +1047,16 @@
        %7D
-          
 %0A       
@@ -1118,30 +1118,8 @@
  if(
-blankInput != null %7C%7C 
 !isS
@@ -1213,34 +1213,23 @@
    blank
-Input = checkStack
+Count++
 ;%0A      
@@ -1263,29 +1263,26 @@
 if(blank
-Inpu
+Coun
 t == 
-null
+0
  %7C%7C filt
@@ -1392,9 +1392,22 @@
 e = 
-2
+blankCount + 1
 ;%0A  

Allow copying of any amount of filters