package minestrapteam.mods.minestrappolation.crafting.recipes;

import com.google.common.collect.Maps;
import net.minecraft.block.Block;
import net.minecraft.inventory.InventoryCrafting;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.item.crafting.CraftingManager;
import net.minecraft.item.crafting.ShapedRecipes;
import net.minecraft.world.World;

import java.util.HashMap;

public class MShapedRecipe extends ShapedRecipes
{

	public MShapedRecipe(int width, int height, ItemStack[] stack, ItemStack output)
	{
		super(width, height, stack, output);
	}

	@Override
	public boolean matches(InventoryCrafting inv, World worldIn)
	{
		for (int i = 0; i <= 3 - this.recipeWidth; ++i)
		{
			for (int j = 0; j <= 3 - this.recipeHeight; ++j)
			{
				if (this.checkMatch(inv, i, j, false))
				{
					return true;
				}
			}
		}

		return false;
	}

	private boolean checkMatch(InventoryCrafting p_77573_1_, int p_77573_2_, int p_77573_3_, boolean p_77573_4_)
	{
		for (int k = 0; k < 3; ++k)
		{
			for (int l = 0; l < 3; ++l)
			{
				int i1 = k - p_77573_2_;
				int j1 = l - p_77573_3_;
				ItemStack itemstack = null;

				if (i1 >= 0 && j1 >= 0 && i1 < this.recipeWidth && j1 < this.recipeHeight)
				{
					if (p_77573_4_)
					{
						itemstack = this.recipeItems[this.recipeWidth - i1 - 1 + j1 * this.recipeWidth];
					}
					else
					{
						itemstack = this.recipeItems[i1 + j1 * this.recipeWidth];
					}
				}

				ItemStack itemstack1 = p_77573_1_.getStackInRowAndColumn(k, l);

				if (itemstack1 != null || itemstack != null)
				{
					if (itemstack1 == null || itemstack == null)
					{
						return false;
					}

					if (itemstack.getItem() != itemstack1.getItem())
					{
						return false;
					}

					if (itemstack.getMetadata() != 32767 && itemstack.getMetadata() != itemstack1.getMetadata())
					{
						return false;
					}
				}
			}
		}

		return true;
	}

	public static ShapedRecipes addRecipe(ItemStack stack, Object... recipeComponents)
	{
		String s = "";
		int i = 0;
		int j = 0;
		int k = 0;

		if (recipeComponents[i] instanceof String[])
		{
			String[] astring = (String[]) recipeComponents[i++];

			for (String s1 : astring)
			{
				++k;
				j = s1.length();
				s = s + s1;
			}
		}
		else
		{
			while (recipeComponents[i] instanceof String)
			{
				String s2 = (String) recipeComponents[i++];
				++k;
				j = s2.length();
				s = s + s2;
			}
		}

		HashMap<Character, ItemStack> hashmap;

		for (hashmap = Maps.newHashMap(); i < recipeComponents.length; i += 2)
		{
			Character character = (Character) recipeComponents[i];
			ItemStack itemstack1 = null;

			if (recipeComponents[i + 1] instanceof Item)
			{
				itemstack1 = new ItemStack((Item) recipeComponents[i + 1]);
			}
			else if (recipeComponents[i + 1] instanceof Block)
			{
				itemstack1 = new ItemStack((Block) recipeComponents[i + 1], 1, 32767);
			}
			else if (recipeComponents[i + 1] instanceof ItemStack)
			{
				itemstack1 = (ItemStack) recipeComponents[i + 1];
			}

			hashmap.put(character, itemstack1);
		}

		ItemStack[] aitemstack = new ItemStack[j * k];

		for (int i1 = 0; i1 < j * k; ++i1)
		{
			char c0 = s.charAt(i1);

			if (hashmap.containsKey(c0))
			{
				aitemstack[i1] = hashmap.get(c0).copy();
			}
			else
			{
				aitemstack[i1] = null;
			}
		}

		MShapedRecipe shapedrecipes = new MShapedRecipe(j, k, aitemstack, stack);
		CraftingManager.getInstance().addRecipe(shapedrecipes);
		return shapedrecipes;
	}
}
