package net.minecraft.data.recipes;

import javax.annotation.Nullable;
import com.google.gson.JsonElement;
import net.minecraft.core.Registry;
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 java.util.function.Consumer;
import net.minecraft.advancements.CriterionTriggerInstance;
import net.minecraft.world.item.crafting.RecipeSerializer;
import net.minecraft.advancements.Advancement;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.crafting.Ingredient;

public class UpgradeRecipeBuilder {
    private final Ingredient base;
    private final Ingredient addition;
    private final Item result;
    private final Advancement.Builder advancement;
    private final RecipeSerializer<?> type;
    
    public UpgradeRecipeBuilder(final RecipeSerializer<?> bmw, final Ingredient bmr2, final Ingredient bmr3, final Item bke) {
        this.advancement = Advancement.Builder.advancement();
        this.type = bmw;
        this.base = bmr2;
        this.addition = bmr3;
        this.result = bke;
    }
    
    public static UpgradeRecipeBuilder smithing(final Ingredient bmr1, final Ingredient bmr2, final Item bke) {
        return new UpgradeRecipeBuilder(RecipeSerializer.SMITHING, bmr1, bmr2, bke);
    }
    
    public UpgradeRecipeBuilder unlocks(final String string, final CriterionTriggerInstance ae) {
        this.advancement.addCriterion(string, ae);
        return this;
    }
    
    public void save(final Consumer<FinishedRecipe> consumer, final String string) {
        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.base, this.addition, this.result, 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 Ingredient base;
        private final Ingredient addition;
        private final Item result;
        private final Advancement.Builder advancement;
        private final ResourceLocation advancementId;
        private final RecipeSerializer<?> type;
        
        public Result(final ResourceLocation uh1, final RecipeSerializer<?> bmw, final Ingredient bmr3, final Ingredient bmr4, final Item bke, final Advancement.Builder a, final ResourceLocation uh7) {
            this.id = uh1;
            this.type = bmw;
            this.base = bmr3;
            this.addition = bmr4;
            this.result = bke;
            this.advancement = a;
            this.advancementId = uh7;
        }
        
        @Override
        public void serializeRecipeData(final JsonObject jsonObject) {
            jsonObject.add("base", this.base.toJson());
            jsonObject.add("addition", this.addition.toJson());
            final JsonObject jsonObject2 = new JsonObject();
            jsonObject2.addProperty("item", Registry.ITEM.getKey(this.result).toString());
            jsonObject.add("result", jsonObject2);
        }
        
        @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;
        }
    }
}
