package net.minecraft.data.recipes;

import javax.annotation.Nullable;
import net.minecraft.world.item.crafting.RecipeSerializer;
import java.util.Iterator;
import com.google.gson.JsonElement;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import org.apache.logging.log4j.LogManager;
import net.minecraft.advancements.RequirementsStrategy;
import net.minecraft.advancements.AdvancementRewards;
import net.minecraft.advancements.critereon.RecipeUnlockedTrigger;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.core.Registry;
import java.util.function.Consumer;
import net.minecraft.advancements.CriterionTriggerInstance;
import net.minecraft.tags.Tag;
import com.google.common.collect.Lists;
import net.minecraft.world.level.ItemLike;
import net.minecraft.advancements.Advancement;
import net.minecraft.world.item.crafting.Ingredient;
import java.util.List;
import net.minecraft.world.item.Item;
import org.apache.logging.log4j.Logger;

public class ShapelessRecipeBuilder {
    private static final Logger LOGGER;
    private final Item result;
    private final int count;
    private final List<Ingredient> ingredients;
    private final Advancement.Builder advancement;
    private String group;
    
    public ShapelessRecipeBuilder(final ItemLike bjv, final int integer) {
        this.ingredients = Lists.newArrayList();
        this.advancement = Advancement.Builder.advancement();
        this.result = bjv.asItem();
        this.count = integer;
    }
    
    public static ShapelessRecipeBuilder shapeless(final ItemLike bjv) {
        return new ShapelessRecipeBuilder(bjv, 1);
    }
    
    public static ShapelessRecipeBuilder shapeless(final ItemLike bjv, final int integer) {
        return new ShapelessRecipeBuilder(bjv, integer);
    }
    
    public ShapelessRecipeBuilder requires(final Tag<Item> aaz) {
        return this.requires(Ingredient.of(aaz));
    }
    
    public ShapelessRecipeBuilder requires(final ItemLike bjv) {
        return this.requires(bjv, 1);
    }
    
    public ShapelessRecipeBuilder requires(final ItemLike bjv, final int integer) {
        for (int integer2 = 0; integer2 < integer; ++integer2) {
            this.requires(Ingredient.of(bjv));
        }
        return this;
    }
    
    public ShapelessRecipeBuilder requires(final Ingredient bgt) {
        return this.requires(bgt, 1);
    }
    
    public ShapelessRecipeBuilder requires(final Ingredient bgt, final int integer) {
        for (int integer2 = 0; integer2 < integer; ++integer2) {
            this.ingredients.add(bgt);
        }
        return this;
    }
    
    public ShapelessRecipeBuilder unlocks(final String string, final CriterionTriggerInstance ac) {
        this.advancement.addCriterion(string, ac);
        return this;
    }
    
    public ShapelessRecipeBuilder group(final String string) {
        this.group = string;
        return this;
    }
    
    public void save(final Consumer<FinishedRecipe> consumer) {
        this.save(consumer, Registry.ITEM.getKey(this.result));
    }
    
    public void save(final Consumer<FinishedRecipe> consumer, final String string) {
        final ResourceLocation sm4 = Registry.ITEM.getKey(this.result);
        if (new ResourceLocation(string).equals(sm4)) {
            throw new IllegalStateException("Shapeless Recipe " + string + " should remove its 'save' argument");
        }
        this.save(consumer, new ResourceLocation(string));
    }
    
    public void save(final Consumer<FinishedRecipe> consumer, final ResourceLocation sm) {
        this.ensureValid(sm);
        this.advancement.parent(new ResourceLocation("recipes/root")).addCriterion("has_the_recipe", new RecipeUnlockedTrigger.TriggerInstance(sm)).rewards(AdvancementRewards.Builder.recipe(sm)).requirements(RequirementsStrategy.OR);
        consumer.accept(new Result(sm, this.result, this.count, (this.group == null) ? "" : this.group, this.ingredients, this.advancement, new ResourceLocation(sm.getNamespace(), "recipes/" + this.result.getItemCategory().getRecipeFolderName() + "/" + sm.getPath())));
    }
    
    private void ensureValid(final ResourceLocation sm) {
        if (this.advancement.getCriteria().isEmpty()) {
            throw new IllegalStateException("No way of obtaining recipe " + sm);
        }
    }
    
    static {
        LOGGER = LogManager.getLogger();
    }
    
    public static class Result implements FinishedRecipe {
        private final ResourceLocation id;
        private final Item result;
        private final int count;
        private final String group;
        private final List<Ingredient> ingredients;
        private final Advancement.Builder advancement;
        private final ResourceLocation advancementId;
        
        public Result(final ResourceLocation sm1, final Item bei, final int integer, final String string, final List<Ingredient> list, final Advancement.Builder a, final ResourceLocation sm7) {
            this.id = sm1;
            this.result = bei;
            this.count = integer;
            this.group = string;
            this.ingredients = list;
            this.advancement = a;
            this.advancementId = sm7;
        }
        
        @Override
        public void serializeRecipeData(final JsonObject jsonObject) {
            if (!this.group.isEmpty()) {
                jsonObject.addProperty("group", this.group);
            }
            final JsonArray jsonArray3 = new JsonArray();
            for (final Ingredient bgt5 : this.ingredients) {
                jsonArray3.add(bgt5.toJson());
            }
            jsonObject.add("ingredients", jsonArray3);
            final JsonObject jsonObject2 = new JsonObject();
            jsonObject2.addProperty("item", Registry.ITEM.getKey(this.result).toString());
            if (this.count > 1) {
                jsonObject2.addProperty("count", this.count);
            }
            jsonObject.add("result", jsonObject2);
        }
        
        @Override
        public RecipeSerializer<?> getType() {
            return RecipeSerializer.SHAPELESS_RECIPE;
        }
        
        @Override
        public ResourceLocation getId() {
            return this.id;
        }
        
        @Nullable
        @Override
        public JsonObject serializeAdvancement() {
            return this.advancement.serializeToJson();
        }
        
        @Nullable
        @Override
        public ResourceLocation getAdvancementId() {
            return this.advancementId;
        }
    }
}
