package net.minecraft.data.recipes;

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

public class SimpleCookingRecipeBuilder {
    private final Item result;
    private final Ingredient ingredient;
    private final float experience;
    private final int cookingTime;
    private final Advancement.Builder advancement;
    private String group;
    private final SimpleCookingSerializer<?> serializer;
    
    private SimpleCookingRecipeBuilder(final ItemLike bjv, final Ingredient bgt, final float float3, final int integer, final SimpleCookingSerializer<?> bhf) {
        this.advancement = Advancement.Builder.advancement();
        this.result = bjv.asItem();
        this.ingredient = bgt;
        this.experience = float3;
        this.cookingTime = integer;
        this.serializer = bhf;
    }
    
    public static SimpleCookingRecipeBuilder cooking(final Ingredient bgt, final ItemLike bjv, final float float3, final int integer, final SimpleCookingSerializer<?> bhf) {
        return new SimpleCookingRecipeBuilder(bjv, bgt, float3, integer, bhf);
    }
    
    public static SimpleCookingRecipeBuilder blasting(final Ingredient bgt, final ItemLike bjv, final float float3, final int integer) {
        return cooking(bgt, bjv, float3, integer, RecipeSerializer.BLASTING_RECIPE);
    }
    
    public static SimpleCookingRecipeBuilder smelting(final Ingredient bgt, final ItemLike bjv, final float float3, final int integer) {
        return cooking(bgt, bjv, float3, integer, RecipeSerializer.SMELTING_RECIPE);
    }
    
    public SimpleCookingRecipeBuilder unlocks(final String string, final CriterionTriggerInstance ac) {
        this.advancement.addCriterion(string, ac);
        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);
        final ResourceLocation sm5 = new ResourceLocation(string);
        if (sm5.equals(sm4)) {
            throw new IllegalStateException("Recipe " + sm5 + " should remove its 'save' argument");
        }
        this.save(consumer, sm5);
    }
    
    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.group == null) ? "" : this.group, this.ingredient, this.result, this.experience, this.cookingTime, this.advancement, new ResourceLocation(sm.getNamespace(), "recipes/" + this.result.getItemCategory().getRecipeFolderName() + "/" + sm.getPath()), this.serializer));
    }
    
    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 float experience;
        private final int cookingTime;
        private final Advancement.Builder advancement;
        private final ResourceLocation advancementId;
        private final RecipeSerializer<? extends AbstractCookingRecipe> serializer;
        
        public Result(final ResourceLocation sm1, final String string, final Ingredient bgt, final Item bei, final float float5, final int integer, final Advancement.Builder a, final ResourceLocation sm8, final RecipeSerializer<? extends AbstractCookingRecipe> bgy) {
            this.id = sm1;
            this.group = string;
            this.ingredient = bgt;
            this.result = bei;
            this.experience = float5;
            this.cookingTime = integer;
            this.advancement = a;
            this.advancementId = sm8;
            this.serializer = bgy;
        }
        
        @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("experience", this.experience);
            jsonObject.addProperty("cookingtime", this.cookingTime);
        }
        
        @Override
        public RecipeSerializer<?> getType() {
            return this.serializer;
        }
        
        @Override
        public ResourceLocation getId() {
            return this.id;
        }
        
        @Nullable
        @Override
        public JsonObject serializeAdvancement() {
            return this.advancement.serializeToJson();
        }
        
        @Nullable
        @Override
        public ResourceLocation getAdvancementId() {
            return this.advancementId;
        }
    }
}
