package wanion.unidict.integration;

import forestry.api.recipes.ICarpenterRecipe;
import forestry.api.recipes.ICentrifugeRecipe;
import forestry.core.recipes.ShapedRecipeCustom;
import forestry.factory.recipes.CarpenterRecipe;
import forestry.factory.recipes.CarpenterRecipeManager;
import forestry.factory.recipes.CentrifugeRecipe;
import forestry.factory.recipes.CentrifugeRecipeManager;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.util.ResourceLocation;
import wanion.lib.common.MetaItem;
import wanion.lib.common.Util;
import wanion.unidict.resource.UniResourceContainer;

import javax.annotation.Nonnull;
import java.util.*;
import java.util.stream.Collectors;

final class ForestryIntegration extends AbstractIntegrationThread
{
	private Set<ICarpenterRecipe> carpenterRecipes = Util.getField(CarpenterRecipeManager.class, "recipes", null, Set.class);

	ForestryIntegration()
	{
		super("Forestry");
	}

	@Override
	public String call()
	{
		try {
			removeBadCarpenterOutputs(carpenterRecipes);
			/*
			final UniResourceContainer ingotBronze = resourceHandler.getContainer("Bronze", "ingot");
			if (ingotBronze != null)
				bronzeThings(ingotBronze);
			 */
			fixCentrifugeRecipes();
		} catch (Exception e) { logger.error(threadName + e); }
		return threadName + "All these bees... they can hurt, you know?";
	}

	private void removeBadCarpenterOutputs(@Nonnull final Set<ICarpenterRecipe> carpenterRecipes)
	{
		carpenterRecipes.removeIf(carpenterRecipe -> carpenterRecipe != null && resourceHandler.exists(MetaItem.get(carpenterRecipe.getCraftingGridRecipe().getOutput())));
	}

	private void bronzeThings(@Nonnull final UniResourceContainer ingotBronze)
	{
		/*
		final Item brokenBronzePickaxe = Item.REGISTRY.getObject(new ResourceLocation("forestry", "brokenBronzePickaxe"));
		final Item brokenBronzeShovel = Item.REGISTRY.getObject(new ResourceLocation("forestry", "brokenBronzeShovel"));
		if (brokenBronzePickaxe != null)
			CarpenterRecipeManager.carpenterRecipes.add(new CarpenterRecipe(5, null, ItemStack.EMPTY, new ShapedRecipeCustom(ingotBronze.getMainEntry(2), "X  ", "   ", "   ", 'X', new ItemStack(brokenBronzePickaxe))));
		if (brokenBronzeShovel != null)
			carpenterRecipes.add(new CarpenterRecipe(5, null, ItemStack.EMPTY, new ShapedRecipeCustom(ingotBronze.getMainEntry(), "X  ", "   ", "   ", 'X', new ItemStack(brokenBronzeShovel))));
		*/
	}

	private void fixCentrifugeRecipes()
	{
		final Set<ICentrifugeRecipe> centrifugeRecipes = Util.getField(CentrifugeRecipeManager.class, "recipes", null, Set.class);
		if (centrifugeRecipes == null)
			return;
		final List<ICentrifugeRecipe> newRecipes = new ArrayList<>();
		for (final Iterator<ICentrifugeRecipe> centrifugeRecipeIterator = centrifugeRecipes.iterator(); centrifugeRecipeIterator.hasNext(); centrifugeRecipeIterator.remove())
		{
			final ICentrifugeRecipe centrifugeRecipe = centrifugeRecipeIterator.next();
			newRecipes.add(new CentrifugeRecipe(centrifugeRecipe.getProcessingTime(), centrifugeRecipe.getInput(), correctCentrifugeOutput(centrifugeRecipe.getAllProducts())));

		}
		centrifugeRecipes.addAll(newRecipes);
	}

	@Nonnull
	private Map<ItemStack, Float> correctCentrifugeOutput(@Nonnull final Map<ItemStack, Float> outputMap)
	{
		return outputMap.entrySet().stream().collect(Collectors.toMap(entry -> resourceHandler.getMainItemStack(entry.getKey()), Map.Entry::getValue));
	}
}
@@ -1046,13 +1046,8 @@
 s);%0A
-%09%09%09/*
 %0A%09%09%09
@@ -1199,14 +1199,8 @@
 e);%0A
-%09%09%09 */
 %0A%09%09%09
@@ -1706,19 +1706,8 @@
 %7B%0A%09%09
-/*%0A%09%09final 
 Item
@@ -1813,22 +1813,161 @@
 e%22));%0A%09%09
-final 
+if (brokenBronzePickaxe == null)%0A%09%09%09brokenBronzePickaxe = Item.REGISTRY.getObject(new ResourceLocation(%22forestry%22, %22broken_bronze_pickaxe%22));%0A%0A%09%09
 Item bro
@@ -2087,17 +2087,16 @@
 onze
-Pickaxe !
+Shovel =
 = nu
@@ -2102,39 +2102,159 @@
 ull)%0A%09%09%09
-CarpenterRecipeManager.
+brokenBronzeShovel = Item.REGISTRY.getObject(new ResourceLocation(%22forestry%22, %22broken_bronze_shovel%22));%0A%0A%09%09if (brokenBronzePickaxe != null)%0A%09%09%09
 carpente
@@ -2652,13 +2652,8 @@
 ));%0A
-%09%09*/%0A
 %09%7D%0A%0A

Fix Forestry bronze tool recipes