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 sm, final String string, final ItemStack ben, final NonNullList<Ingredient> fy) {
        this.id = sm;
        this.group = string;
        this.result = ben;
        this.ingredients = fy;
    }
    
    @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 baz, final Level bjw) {
        final StackedContents ayl4 = new StackedContents();
        int integer5 = 0;
        for (int integer6 = 0; integer6 < baz.getContainerSize(); ++integer6) {
            final ItemStack ben7 = baz.getItem(integer6);
            if (!ben7.isEmpty()) {
                ++integer5;
                ayl4.accountStack(ben7, 1);
            }
        }
        return integer5 == this.ingredients.size() && ayl4.canCraft(this, null);
    }
    
    @Override
    public ItemStack assemble(final CraftingContainer baz) {
        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 sm, final JsonObject jsonObject) {
            final String string4 = GsonHelper.getAsString(jsonObject, "group", "");
            final NonNullList<Ingredient> fy5 = itemsFromJson(GsonHelper.getAsJsonArray(jsonObject, "ingredients"));
            if (fy5.isEmpty()) {
                throw new JsonParseException("No ingredients for shapeless recipe");
            }
            if (fy5.size() > 9) {
                throw new JsonParseException("Too many ingredients for shapeless recipe");
            }
            final ItemStack ben6 = ShapedRecipe.itemFromJson(GsonHelper.getAsJsonObject(jsonObject, "result"));
            return new ShapelessRecipe(sm, string4, ben6, fy5);
        }
        
        private static NonNullList<Ingredient> itemsFromJson(final JsonArray jsonArray) {
            final NonNullList<Ingredient> fy2 = NonNullList.<Ingredient>create();
            for (int integer3 = 0; integer3 < jsonArray.size(); ++integer3) {
                final Ingredient bgt4 = Ingredient.fromJson(jsonArray.get(integer3));
                if (!bgt4.isEmpty()) {
                    fy2.add(bgt4);
                }
            }
            return fy2;
        }
        
        @Override
        public ShapelessRecipe fromNetwork(final ResourceLocation sm, final FriendlyByteBuf kv) {
            final String string4 = kv.readUtf(32767);
            final int integer5 = kv.readVarInt();
            final NonNullList<Ingredient> fy6 = NonNullList.<Ingredient>withSize(integer5, Ingredient.EMPTY);
            for (int integer6 = 0; integer6 < fy6.size(); ++integer6) {
                fy6.set(integer6, Ingredient.fromNetwork(kv));
            }
            final ItemStack ben7 = kv.readItem();
            return new ShapelessRecipe(sm, string4, ben7, fy6);
        }
        
        @Override
        public void toNetwork(final FriendlyByteBuf kv, final ShapelessRecipe bhc) {
            kv.writeUtf(bhc.group);
            kv.writeVarInt(bhc.ingredients.size());
            for (final Ingredient bgt5 : bhc.ingredients) {
                bgt5.toNetwork(kv);
            }
            kv.writeItem(bhc.result);
        }
    }
}
