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<?> bmw, final Ingredient bmr, final ItemLike bqa, final int integer) {
        this.advancement = Advancement.Builder.advancement();
        this.type = bmw;
        this.result = bqa.asItem();
        this.ingredient = bmr;
        this.count = integer;
    }
    
    public static SingleItemRecipeBuilder stonecutting(final Ingredient bmr, final ItemLike bqa) {
        return new SingleItemRecipeBuilder(RecipeSerializer.STONECUTTER, bmr, bqa, 1);
    }
    
    public static SingleItemRecipeBuilder stonecutting(final Ingredient bmr, final ItemLike bqa, final int integer) {
        return new SingleItemRecipeBuilder(RecipeSerializer.STONECUTTER, bmr, bqa, integer);
    }
    
    public SingleItemRecipeBuilder unlocks(final String string, final CriterionTriggerInstance ae) {
        this.advancement.addCriterion(string, ae);
        return this;
    }
    
    public void save(final Consumer<FinishedRecipe> consumer, final String string) {
        final ResourceLocation uh4 = Registry.ITEM.getKey(this.result);
        if (new ResourceLocation(string).equals(uh4)) {
            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 uh) {
        this.ensureValid(uh);
        this.advancement.parent(new ResourceLocation("recipes/root")).addCriterion("has_the_recipe", RecipeUnlockedTrigger.unlocked(uh)).rewards(AdvancementRewards.Builder.recipe(uh)).requirements(RequirementsStrategy.OR);
        consumer.accept(new Result(uh, this.type, (this.group == null) ? "" : this.group, this.ingredient, this.result, this.count, this.advancement, new ResourceLocation(uh.getNamespace(), "recipes/" + this.result.getItemCategory().getRecipeFolderName() + "/" + uh.getPath())));
    }
    
    private void ensureValid(final ResourceLocation uh) {
        if (this.advancement.getCriteria().isEmpty()) {
            throw new IllegalStateException("No way of obtaining recipe " + uh);
        }
    }
    
    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 uh1, final RecipeSerializer<?> bmw, final String string, final Ingredient bmr, final Item bke, final int integer, final Advancement.Builder a, final ResourceLocation uh8) {
            this.id = uh1;
            this.type = bmw;
            this.group = string;
            this.ingredient = bmr;
            this.result = bke;
            this.count = integer;
            this.advancement = a;
            this.advancementId = uh8;
        }
        
        @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;
        }
    }
}
