package net.minecraft.world.item.crafting;

import java.util.Iterator;
import net.minecraft.network.FriendlyByteBuf;
import com.google.gson.JsonArray;
import com.google.gson.JsonParseException;
import net.minecraft.util.GsonHelper;
import com.google.gson.JsonObject;
import net.minecraft.world.Container;
import it.unimi.dsi.fastutil.ints.IntList;
import net.minecraft.world.entity.player.StackedContents;
import net.minecraft.world.level.Level;
import net.minecraft.world.inventory.CraftingContainer;
import net.minecraft.core.NonNullList;
import net.minecraft.world.item.ItemStack;
import net.minecraft.resources.ResourceLocation;

public class ShapelessRecipe implements CraftingRecipe {
    private final ResourceLocation id;
    private final String group;
    private final ItemStack result;
    private final NonNullList<Ingredient> ingredients;
    
    public ShapelessRecipe(final ResourceLocation uh, final String string, final ItemStack bki, final NonNullList<Ingredient> gi) {
        this.id = uh;
        this.group = string;
        this.result = bki;
        this.ingredients = gi;
    }
    
    @Override
    public ResourceLocation getId() {
        return this.id;
    }
    
    @Override
    public RecipeSerializer<?> getSerializer() {
        return RecipeSerializer.SHAPELESS_RECIPE;
    }
    
    @Override
    public String getGroup() {
        return this.group;
    }
    
    @Override
    public ItemStack getResultItem() {
        return this.result;
    }
    
    @Override
    public NonNullList<Ingredient> getIngredients() {
        return this.ingredients;
    }
    
    @Override
    public boolean matches(final CraftingContainer bgu, final Level bqb) {
        final StackedContents bee4 = new StackedContents();
        int integer5 = 0;
        for (int integer6 = 0; integer6 < bgu.getContainerSize(); ++integer6) {
            final ItemStack bki7 = bgu.getItem(integer6);
            if (!bki7.isEmpty()) {
                ++integer5;
                bee4.accountStack(bki7, 1);
            }
        }
        return integer5 == this.ingredients.size() && bee4.canCraft(this, null);
    }
    
    @Override
    public ItemStack assemble(final CraftingContainer bgu) {
        return this.result.copy();
    }
    
    @Override
    public boolean canCraftInDimensions(final int integer1, final int integer2) {
        return integer1 * integer2 >= this.ingredients.size();
    }
    
    public static class Serializer implements RecipeSerializer<ShapelessRecipe> {
        @Override
        public ShapelessRecipe fromJson(final ResourceLocation uh, final JsonObject jsonObject) {
            final String string4 = GsonHelper.getAsString(jsonObject, "group", "");
            final NonNullList<Ingredient> gi5 = itemsFromJson(GsonHelper.getAsJsonArray(jsonObject, "ingredients"));
            if (gi5.isEmpty()) {
                throw new JsonParseException("No ingredients for shapeless recipe");
            }
            if (gi5.size() > 9) {
                throw new JsonParseException("Too many ingredients for shapeless recipe");
            }
            final ItemStack bki6 = ShapedRecipe.itemFromJson(GsonHelper.getAsJsonObject(jsonObject, "result"));
            return new ShapelessRecipe(uh, string4, bki6, gi5);
        }
        
        private static NonNullList<Ingredient> itemsFromJson(final JsonArray jsonArray) {
            final NonNullList<Ingredient> gi2 = NonNullList.<Ingredient>create();
            for (int integer3 = 0; integer3 < jsonArray.size(); ++integer3) {
                final Ingredient bmr4 = Ingredient.fromJson(jsonArray.get(integer3));
                if (!bmr4.isEmpty()) {
                    gi2.add(bmr4);
                }
            }
            return gi2;
        }
        
        @Override
        public ShapelessRecipe fromNetwork(final ResourceLocation uh, final FriendlyByteBuf mg) {
            final String string4 = mg.readUtf(32767);
            final int integer5 = mg.readVarInt();
            final NonNullList<Ingredient> gi6 = NonNullList.<Ingredient>withSize(integer5, Ingredient.EMPTY);
            for (int integer6 = 0; integer6 < gi6.size(); ++integer6) {
                gi6.set(integer6, Ingredient.fromNetwork(mg));
            }
            final ItemStack bki7 = mg.readItem();
            return new ShapelessRecipe(uh, string4, bki7, gi6);
        }
        
        @Override
        public void toNetwork(final FriendlyByteBuf mg, final ShapelessRecipe bna) {
            mg.writeUtf(bna.group);
            mg.writeVarInt(bna.ingredients.size());
            for (final Ingredient bmr5 : bna.ingredients) {
                bmr5.toNetwork(mg);
            }
            mg.writeItem(bna.result);
        }
    }
}
