package net.minecraft.data.recipes;

import javax.annotation.Nullable;
import com.google.gson.JsonObject;
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.world.level.ItemLike;
import net.minecraft.world.item.crafting.RecipeSerializer;
import net.minecraft.advancements.Advancement;
import net.minecraft.world.item.crafting.Ingredient;
import net.minecraft.world.item.Item;

public class SingleItemRecipeBuilder {
    private final Item result;
    private final Ingredient ingredient;
    private final int count;
    private final Advancement.Builder advancement;
    private String group;
    private final RecipeSerializer<?> type;
    
    public SingleItemRecipeBuilder(final RecipeSerializer<?> bgy, final Ingredient bgt, final ItemLike bjv, final int integer) {
        this.advancement = Advancement.Builder.advancement();
        this.type = bgy;
        this.result = bjv.asItem();
        this.ingredient = bgt;
        this.count = integer;
    }
    
    public static SingleItemRecipeBuilder stonecutting(final Ingredient bgt, final ItemLike bjv) {
        return new SingleItemRecipeBuilder(RecipeSerializer.STONECUTTER, bgt, bjv, 1);
    }
    
    public static SingleItemRecipeBuilder stonecutting(final Ingredient bgt, final ItemLike bjv, final int integer) {
        return new SingleItemRecipeBuilder(RecipeSerializer.STONECUTTER, bgt, bjv, integer);
    }
    
    public SingleItemRecipeBuilder unlocks(final String string, final CriterionTriggerInstance ac) {
        this.advancement.addCriterion(string, ac);
        return this;
    }
    
    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("Single Item 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.type, (this.group == null) ? "" : this.group, this.ingredient, this.result, this.count, 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);
        }
    }
    
    public static class Result implements FinishedRecipe {
        private final ResourceLocation id;
        private final String group;
        private final Ingredient ingredient;
        private final Item result;
        private final int count;
        private final Advancement.Builder advancement;
        private final ResourceLocation advancementId;
        private final RecipeSerializer<?> type;
        
        public Result(final ResourceLocation sm1, final RecipeSerializer<?> bgy, final String string, final Ingredient bgt, final Item bei, final int integer, final Advancement.Builder a, final ResourceLocation sm8) {
            this.id = sm1;
            this.type = bgy;
            this.group = string;
            this.ingredient = bgt;
            this.result = bei;
            this.count = integer;
            this.advancement = a;
            this.advancementId = sm8;
        }
        
        @Override
        public void serializeRecipeData(final JsonObject jsonObject) {
            if (!this.group.isEmpty()) {
                jsonObject.addProperty("group", this.group);
            }
            jsonObject.add("ingredient", this.ingredient.toJson());
            jsonObject.addProperty("result", Registry.ITEM.getKey(this.result).toString());
            jsonObject.addProperty("count", this.count);
        }
        
        @Override
        public ResourceLocation getId() {
            return this.id;
        }
        
        @Override
        public RecipeSerializer<?> getType() {
            return this.type;
        }
        
        @Nullable
        @Override
        public JsonObject serializeAdvancement() {
            return this.advancement.serializeToJson();
        }
        
        @Nullable
        @Override
        public ResourceLocation getAdvancementId() {
            return this.advancementId;
        }
    }
}
