package net.minecraft.world.item.crafting;

import net.minecraft.network.FriendlyByteBuf;
import com.google.gson.JsonElement;
import net.minecraft.world.item.ItemStack;
import net.minecraft.core.Registry;
import net.minecraft.world.level.ItemLike;
import net.minecraft.util.GsonHelper;
import com.google.gson.JsonObject;
import net.minecraft.resources.ResourceLocation;

public class SimpleCookingSerializer<T extends AbstractCookingRecipe> implements RecipeSerializer<T> {
    private final int defaultCookingTime;
    private final CookieBaker<T> factory;
    
    public SimpleCookingSerializer(final CookieBaker<T> a, final int integer) {
        this.defaultCookingTime = integer;
        this.factory = a;
    }
    
    @Override
    public T fromJson(final ResourceLocation uh, final JsonObject jsonObject) {
        final String string4 = GsonHelper.getAsString(jsonObject, "group", "");
        final JsonElement jsonElement5 = GsonHelper.isArrayNode(jsonObject, "ingredient") ? GsonHelper.getAsJsonArray(jsonObject, "ingredient") : GsonHelper.getAsJsonObject(jsonObject, "ingredient");
        final Ingredient bmr6 = Ingredient.fromJson(jsonElement5);
        final String string5 = GsonHelper.getAsString(jsonObject, "result");
        final ResourceLocation uh2 = new ResourceLocation(string5);
        final Object o;
        final String str;
        final ItemStack bki9 = new ItemStack(Registry.ITEM.getOptional(uh2).<Throwable>orElseThrow(() -> {
            new IllegalStateException("Item: " + str + " does not exist");
            return o;
        }));
        final float float10 = GsonHelper.getAsFloat(jsonObject, "experience", 0.0f);
        final int integer11 = GsonHelper.getAsInt(jsonObject, "cookingtime", this.defaultCookingTime);
        return this.factory.create(uh, string4, bmr6, bki9, float10, integer11);
    }
    
    @Override
    public T fromNetwork(final ResourceLocation uh, final FriendlyByteBuf mg) {
        final String string4 = mg.readUtf(32767);
        final Ingredient bmr5 = Ingredient.fromNetwork(mg);
        final ItemStack bki6 = mg.readItem();
        final float float7 = mg.readFloat();
        final int integer8 = mg.readVarInt();
        return this.factory.create(uh, string4, bmr5, bki6, float7, integer8);
    }
    
    @Override
    public void toNetwork(final FriendlyByteBuf mg, final T bmg) {
        mg.writeUtf(bmg.group);
        bmg.ingredient.toNetwork(mg);
        mg.writeItem(bmg.result);
        mg.writeFloat(bmg.experience);
        mg.writeVarInt(bmg.cookingTime);
    }
    
    interface CookieBaker<T extends AbstractCookingRecipe> {
        T create(final ResourceLocation uh, final String string, final Ingredient bmr, final ItemStack bki, final float float5, final int integer);
    }
}
