package net.minecraft.client;

import org.apache.logging.log4j.LogManager;
import net.minecraft.core.Registry;
import java.util.Collections;
import net.minecraft.world.inventory.SmokerMenu;
import net.minecraft.world.inventory.BlastFurnaceMenu;
import net.minecraft.world.inventory.FurnaceMenu;
import net.minecraft.world.inventory.InventoryMenu;
import net.minecraft.world.inventory.CraftingMenu;
import net.minecraft.world.inventory.RecipeBookMenu;
import net.minecraft.world.item.ItemStack;
import org.apache.logging.log4j.util.Supplier;
import net.minecraft.world.item.BlockItem;
import net.minecraft.world.item.CreativeModeTab;
import net.minecraft.world.item.crafting.RecipeType;
import java.util.Iterator;
import com.google.common.collect.Table;
import net.minecraft.world.item.crafting.Recipe;
import com.google.common.collect.HashBasedTable;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import net.minecraft.client.gui.screens.recipebook.RecipeCollection;
import java.util.List;
import java.util.Map;
import net.minecraft.world.item.crafting.RecipeManager;
import org.apache.logging.log4j.Logger;
import net.minecraft.stats.RecipeBook;

public class ClientRecipeBook extends RecipeBook {
    private static final Logger LOGGER;
    private final RecipeManager recipes;
    private final Map<RecipeBookCategories, List<RecipeCollection>> collectionsByTab;
    private final List<RecipeCollection> collections;
    
    public ClientRecipeBook(final RecipeManager bmv) {
        this.collectionsByTab = Maps.newHashMap();
        this.collections = Lists.newArrayList();
        this.recipes = bmv;
    }
    
    public void setupCollections() {
        this.collections.clear();
        this.collectionsByTab.clear();
        final Table<RecipeBookCategories, String, RecipeCollection> table2 = HashBasedTable.create();
        for (final Recipe<?> bmu4 : this.recipes.getRecipes()) {
            if (bmu4.isSpecial()) {
                continue;
            }
            final RecipeBookCategories dme5 = getCategory(bmu4);
            final String string6 = bmu4.getGroup();
            RecipeCollection dtr7;
            if (string6.isEmpty()) {
                dtr7 = this.createCollection(dme5);
            }
            else {
                dtr7 = table2.get(dme5, string6);
                if (dtr7 == null) {
                    dtr7 = this.createCollection(dme5);
                    table2.put(dme5, string6, dtr7);
                }
            }
            dtr7.add(bmu4);
        }
    }
    
    private RecipeCollection createCollection(final RecipeBookCategories dme) {
        final RecipeCollection dtr3 = new RecipeCollection();
        this.collections.add(dtr3);
        this.collectionsByTab.computeIfAbsent(dme, dme -> Lists.newArrayList()).add(dtr3);
        if (dme == RecipeBookCategories.FURNACE_BLOCKS || dme == RecipeBookCategories.FURNACE_FOOD || dme == RecipeBookCategories.FURNACE_MISC) {
            this.addToCollection(RecipeBookCategories.FURNACE_SEARCH, dtr3);
        }
        else if (dme == RecipeBookCategories.BLAST_FURNACE_BLOCKS || dme == RecipeBookCategories.BLAST_FURNACE_MISC) {
            this.addToCollection(RecipeBookCategories.BLAST_FURNACE_SEARCH, dtr3);
        }
        else if (dme == RecipeBookCategories.SMOKER_FOOD) {
            this.addToCollection(RecipeBookCategories.SMOKER_SEARCH, dtr3);
        }
        else if (dme == RecipeBookCategories.CRAFTING_BUILDING_BLOCKS || dme == RecipeBookCategories.CRAFTING_REDSTONE || dme == RecipeBookCategories.CRAFTING_EQUIPMENT || dme == RecipeBookCategories.CRAFTING_MISC) {
            this.addToCollection(RecipeBookCategories.SEARCH, dtr3);
        }
        return dtr3;
    }
    
    private void addToCollection(final RecipeBookCategories dme, final RecipeCollection dtr) {
        this.collectionsByTab.computeIfAbsent(dme, dme -> Lists.newArrayList()).add(dtr);
    }
    
    private static RecipeBookCategories getCategory(final Recipe<?> bmu) {
        final RecipeType<?> bmx2 = bmu.getType();
        if (bmx2 == RecipeType.CRAFTING) {
            final ItemStack bki3 = bmu.getResultItem();
            final CreativeModeTab biy4 = bki3.getItem().getItemCategory();
            if (biy4 == CreativeModeTab.TAB_BUILDING_BLOCKS) {
                return RecipeBookCategories.CRAFTING_BUILDING_BLOCKS;
            }
            if (biy4 == CreativeModeTab.TAB_TOOLS || biy4 == CreativeModeTab.TAB_COMBAT) {
                return RecipeBookCategories.CRAFTING_EQUIPMENT;
            }
            if (biy4 == CreativeModeTab.TAB_REDSTONE) {
                return RecipeBookCategories.CRAFTING_REDSTONE;
            }
            return RecipeBookCategories.CRAFTING_MISC;
        }
        else if (bmx2 == RecipeType.SMELTING) {
            if (bmu.getResultItem().getItem().isEdible()) {
                return RecipeBookCategories.FURNACE_FOOD;
            }
            if (bmu.getResultItem().getItem() instanceof BlockItem) {
                return RecipeBookCategories.FURNACE_BLOCKS;
            }
            return RecipeBookCategories.FURNACE_MISC;
        }
        else if (bmx2 == RecipeType.BLASTING) {
            if (bmu.getResultItem().getItem() instanceof BlockItem) {
                return RecipeBookCategories.BLAST_FURNACE_BLOCKS;
            }
            return RecipeBookCategories.BLAST_FURNACE_MISC;
        }
        else {
            if (bmx2 == RecipeType.SMOKING) {
                return RecipeBookCategories.SMOKER_FOOD;
            }
            if (bmx2 == RecipeType.STONECUTTING) {
                return RecipeBookCategories.STONECUTTER;
            }
            if (bmx2 == RecipeType.CAMPFIRE_COOKING) {
                return RecipeBookCategories.CAMPFIRE;
            }
            if (bmx2 == RecipeType.SMITHING) {
                return RecipeBookCategories.SMITHING;
            }
            ClientRecipeBook.LOGGER.warn("Unknown recipe category: {}/{}", new Supplier[] { () -> Registry.RECIPE_TYPE.getKey(bmu.getType()), bmu::getId });
            return RecipeBookCategories.UNKNOWN;
        }
    }
    
    public static List<RecipeBookCategories> getCategories(final RecipeBookMenu<?> bhp) {
        if (bhp instanceof CraftingMenu || bhp instanceof InventoryMenu) {
            return Lists.<RecipeBookCategories>newArrayList(RecipeBookCategories.SEARCH, RecipeBookCategories.CRAFTING_EQUIPMENT, RecipeBookCategories.CRAFTING_BUILDING_BLOCKS, RecipeBookCategories.CRAFTING_MISC, RecipeBookCategories.CRAFTING_REDSTONE);
        }
        if (bhp instanceof FurnaceMenu) {
            return Lists.<RecipeBookCategories>newArrayList(RecipeBookCategories.FURNACE_SEARCH, RecipeBookCategories.FURNACE_FOOD, RecipeBookCategories.FURNACE_BLOCKS, RecipeBookCategories.FURNACE_MISC);
        }
        if (bhp instanceof BlastFurnaceMenu) {
            return Lists.<RecipeBookCategories>newArrayList(RecipeBookCategories.BLAST_FURNACE_SEARCH, RecipeBookCategories.BLAST_FURNACE_BLOCKS, RecipeBookCategories.BLAST_FURNACE_MISC);
        }
        if (bhp instanceof SmokerMenu) {
            return Lists.<RecipeBookCategories>newArrayList(RecipeBookCategories.SMOKER_SEARCH, RecipeBookCategories.SMOKER_FOOD);
        }
        return Lists.newArrayList();
    }
    
    public List<RecipeCollection> getCollections() {
        return this.collections;
    }
    
    public List<RecipeCollection> getCollection(final RecipeBookCategories dme) {
        return this.collectionsByTab.getOrDefault(dme, Collections.<RecipeCollection>emptyList());
    }
    
    static {
        LOGGER = LogManager.getLogger();
    }
}
