package net.minecraft.client.gui.screens.recipebook;

import net.minecraft.world.item.ItemStack;
import it.unimi.dsi.fastutil.ints.IntList;
import net.minecraft.world.entity.player.StackedContents;
import java.util.Iterator;
import net.minecraft.stats.RecipeBook;
import com.google.common.collect.Sets;
import com.google.common.collect.Lists;
import java.util.Set;
import net.minecraft.world.item.crafting.Recipe;
import java.util.List;

public class RecipeCollection {
    private final List<Recipe<?>> recipes;
    private final Set<Recipe<?>> craftable;
    private final Set<Recipe<?>> fitsDimensions;
    private final Set<Recipe<?>> known;
    private boolean singleResultItem;
    
    public RecipeCollection() {
        this.recipes = Lists.newArrayList();
        this.craftable = Sets.newHashSet();
        this.fitsDimensions = Sets.newHashSet();
        this.known = Sets.newHashSet();
        this.singleResultItem = true;
    }
    
    public boolean hasKnownRecipes() {
        return !this.known.isEmpty();
    }
    
    public void updateKnownRecipes(final RecipeBook aal) {
        for (final Recipe<?> bgw4 : this.recipes) {
            if (aal.contains(bgw4)) {
                this.known.add(bgw4);
            }
        }
    }
    
    public void canCraft(final StackedContents ayl, final int integer2, final int integer3, final RecipeBook aal) {
        for (int integer4 = 0; integer4 < this.recipes.size(); ++integer4) {
            final Recipe<?> bgw7 = this.recipes.get(integer4);
            final boolean boolean8 = bgw7.canCraftInDimensions(integer2, integer3) && aal.contains(bgw7);
            if (boolean8) {
                this.fitsDimensions.add(bgw7);
            }
            else {
                this.fitsDimensions.remove(bgw7);
            }
            if (boolean8 && ayl.canCraft(bgw7, null)) {
                this.craftable.add(bgw7);
            }
            else {
                this.craftable.remove(bgw7);
            }
        }
    }
    
    public boolean isCraftable(final Recipe<?> bgw) {
        return this.craftable.contains(bgw);
    }
    
    public boolean hasCraftable() {
        return !this.craftable.isEmpty();
    }
    
    public boolean hasFitting() {
        return !this.fitsDimensions.isEmpty();
    }
    
    public List<Recipe<?>> getRecipes() {
        return this.recipes;
    }
    
    public List<Recipe<?>> getRecipes(final boolean boolean1) {
        final List<Recipe<?>> list3 = Lists.newArrayList();
        final Set<Recipe<?>> set4 = boolean1 ? this.craftable : this.fitsDimensions;
        for (final Recipe<?> bgw6 : this.recipes) {
            if (set4.contains(bgw6)) {
                list3.add(bgw6);
            }
        }
        return list3;
    }
    
    public List<Recipe<?>> getDisplayRecipes(final boolean boolean1) {
        final List<Recipe<?>> list3 = Lists.newArrayList();
        for (final Recipe<?> bgw5 : this.recipes) {
            if (this.fitsDimensions.contains(bgw5) && this.craftable.contains(bgw5) == boolean1) {
                list3.add(bgw5);
            }
        }
        return list3;
    }
    
    public void add(final Recipe<?> bgw) {
        this.recipes.add(bgw);
        if (this.singleResultItem) {
            final ItemStack ben3 = this.recipes.get(0).getResultItem();
            final ItemStack ben4 = bgw.getResultItem();
            this.singleResultItem = (ItemStack.isSame(ben3, ben4) && ItemStack.tagMatches(ben3, ben4));
        }
    }
    
    public boolean hasSingleResultItem() {
        return this.singleResultItem;
    }
}
