package net.minecraft.client;

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 net.minecraft.world.item.CreativeModeTab;
import net.minecraft.world.item.BlockItem;
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 net.minecraft.stats.RecipeBook;

public class ClientRecipeBook extends RecipeBook {
    private final RecipeManager recipes;
    private final Map<RecipeBookCategories, List<RecipeCollection>> collectionsByTab;
    private final List<RecipeCollection> collections;
    
    public ClientRecipeBook(final RecipeManager bgx) {
        this.collectionsByTab = Maps.newHashMap();
        this.collections = Lists.newArrayList();
        this.recipes = bgx;
    }
    
    public void setupCollections() {
        this.collections.clear();
        this.collectionsByTab.clear();
        final Table<RecipeBookCategories, String, RecipeCollection> table2 = HashBasedTable.create();
        for (final Recipe<?> bgw4 : this.recipes.getRecipes()) {
            if (bgw4.isSpecial()) {
                continue;
            }
            final RecipeBookCategories dbu5 = getCategory(bgw4);
            final String string6 = bgw4.getGroup();
            RecipeCollection dit7;
            if (string6.isEmpty()) {
                dit7 = this.createCollection(dbu5);
            }
            else {
                dit7 = table2.get(dbu5, string6);
                if (dit7 == null) {
                    dit7 = this.createCollection(dbu5);
                    table2.put(dbu5, string6, dit7);
                }
            }
            dit7.add(bgw4);
        }
    }
    
    private RecipeCollection createCollection(final RecipeBookCategories dbu) {
        final RecipeCollection dit3 = new RecipeCollection();
        this.collections.add(dit3);
        this.collectionsByTab.computeIfAbsent(dbu, dbu -> Lists.newArrayList()).add(dit3);
        if (dbu == RecipeBookCategories.FURNACE_BLOCKS || dbu == RecipeBookCategories.FURNACE_FOOD || dbu == RecipeBookCategories.FURNACE_MISC) {
            this.addToCollection(RecipeBookCategories.FURNACE_SEARCH, dit3);
        }
        else if (dbu == RecipeBookCategories.BLAST_FURNACE_BLOCKS || dbu == RecipeBookCategories.BLAST_FURNACE_MISC) {
            this.addToCollection(RecipeBookCategories.BLAST_FURNACE_SEARCH, dit3);
        }
        else if (dbu == RecipeBookCategories.SMOKER_FOOD) {
            this.addToCollection(RecipeBookCategories.SMOKER_SEARCH, dit3);
        }
        else if (dbu == RecipeBookCategories.STONECUTTER) {
            this.addToCollection(RecipeBookCategories.STONECUTTER, dit3);
        }
        else if (dbu == RecipeBookCategories.CAMPFIRE) {
            this.addToCollection(RecipeBookCategories.CAMPFIRE, dit3);
        }
        else {
            this.addToCollection(RecipeBookCategories.SEARCH, dit3);
        }
        return dit3;
    }
    
    private void addToCollection(final RecipeBookCategories dbu, final RecipeCollection dit) {
        this.collectionsByTab.computeIfAbsent(dbu, dbu -> Lists.newArrayList()).add(dit);
    }
    
    private static RecipeBookCategories getCategory(final Recipe<?> bgw) {
        final RecipeType<?> bgz2 = bgw.getType();
        if (bgz2 == RecipeType.SMELTING) {
            if (bgw.getResultItem().getItem().isEdible()) {
                return RecipeBookCategories.FURNACE_FOOD;
            }
            if (bgw.getResultItem().getItem() instanceof BlockItem) {
                return RecipeBookCategories.FURNACE_BLOCKS;
            }
            return RecipeBookCategories.FURNACE_MISC;
        }
        else if (bgz2 == RecipeType.BLASTING) {
            if (bgw.getResultItem().getItem() instanceof BlockItem) {
                return RecipeBookCategories.BLAST_FURNACE_BLOCKS;
            }
            return RecipeBookCategories.BLAST_FURNACE_MISC;
        }
        else {
            if (bgz2 == RecipeType.SMOKING) {
                return RecipeBookCategories.SMOKER_FOOD;
            }
            if (bgz2 == RecipeType.STONECUTTING) {
                return RecipeBookCategories.STONECUTTER;
            }
            if (bgz2 == RecipeType.CAMPFIRE_COOKING) {
                return RecipeBookCategories.CAMPFIRE;
            }
            final ItemStack ben3 = bgw.getResultItem();
            final CreativeModeTab bdd4 = ben3.getItem().getItemCategory();
            if (bdd4 == CreativeModeTab.TAB_BUILDING_BLOCKS) {
                return RecipeBookCategories.BUILDING_BLOCKS;
            }
            if (bdd4 == CreativeModeTab.TAB_TOOLS || bdd4 == CreativeModeTab.TAB_COMBAT) {
                return RecipeBookCategories.EQUIPMENT;
            }
            if (bdd4 == CreativeModeTab.TAB_REDSTONE) {
                return RecipeBookCategories.REDSTONE;
            }
            return RecipeBookCategories.MISC;
        }
    }
    
    public static List<RecipeBookCategories> getCategories(final RecipeBookMenu<?> bbt) {
        if (bbt instanceof CraftingMenu || bbt instanceof InventoryMenu) {
            return Lists.<RecipeBookCategories>newArrayList(RecipeBookCategories.SEARCH, RecipeBookCategories.EQUIPMENT, RecipeBookCategories.BUILDING_BLOCKS, RecipeBookCategories.MISC, RecipeBookCategories.REDSTONE);
        }
        if (bbt instanceof FurnaceMenu) {
            return Lists.<RecipeBookCategories>newArrayList(RecipeBookCategories.FURNACE_SEARCH, RecipeBookCategories.FURNACE_FOOD, RecipeBookCategories.FURNACE_BLOCKS, RecipeBookCategories.FURNACE_MISC);
        }
        if (bbt instanceof BlastFurnaceMenu) {
            return Lists.<RecipeBookCategories>newArrayList(RecipeBookCategories.BLAST_FURNACE_SEARCH, RecipeBookCategories.BLAST_FURNACE_BLOCKS, RecipeBookCategories.BLAST_FURNACE_MISC);
        }
        if (bbt 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 dbu) {
        return this.collectionsByTab.getOrDefault(dbu, Collections.<RecipeCollection>emptyList());
    }
}
