package com.acethewildfire.acesbs.recipe;

import com.mojang.serialization.MapCodec;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import net.minecraft.item.ItemStack;
import net.minecraft.network.RegistryByteBuf;
import net.minecraft.network.codec.PacketCodec;
import net.minecraft.recipe.Ingredient;
import net.minecraft.recipe.Recipe;
import net.minecraft.recipe.RecipeSerializer;
import net.minecraft.recipe.RecipeType;
import net.minecraft.registry.RegistryWrapper;
import net.minecraft.util.collection.DefaultedList;
import net.minecraft.world.World;

public record EntropicStabilizerRecipe(Ingredient inputItem, ItemStack output) implements Recipe<EntropicStabilizerRecipeInput> {

    @Override
    public DefaultedList<Ingredient> getIngredients() {
        DefaultedList<Ingredient> list = DefaultedList.of();
        list.add(this.inputItem);
        return list;
    }

    // Read JSON Files
    // Turns into new EntropicStabilizerRecipe

    @Override
    public boolean matches(EntropicStabilizerRecipeInput input, World world) {
        if(world.isClient()) {
            return false;
        }

        return inputItem.test(input.getStackInSlot(0));
    }

    @Override
    public ItemStack craft(EntropicStabilizerRecipeInput input, RegistryWrapper.WrapperLookup lookup) {
        return output.copy();
    }

    @Override
    public boolean fits(int width, int height) {
        return true;
    }

    @Override
    public ItemStack getResult(RegistryWrapper.WrapperLookup registriesLookup) {
        return output;
    }

    @Override
    public RecipeSerializer<?> getSerializer() {
        return ModRecipies.ENTROPIC_STABILIZER_SERIALIZER;
    }

    @Override
    public RecipeType<?> getType() {
        return ModRecipies.ENTROPIC_STABILIZER_TYPE;
    }

    public static class Serializer implements RecipeSerializer<EntropicStabilizerRecipe> {
        public static final MapCodec<EntropicStabilizerRecipe> CODEC = RecordCodecBuilder.mapCodec(inst -> inst.group(
                Ingredient.DISALLOW_EMPTY_CODEC.fieldOf("ingredient").forGetter(EntropicStabilizerRecipe::inputItem),
                ItemStack.CODEC.fieldOf("result").forGetter(EntropicStabilizerRecipe::output)
        ).apply(inst, EntropicStabilizerRecipe::new));

        public static final PacketCodec<RegistryByteBuf, EntropicStabilizerRecipe> STREAM_CODEC =
                PacketCodec.tuple(
                        Ingredient.PACKET_CODEC, EntropicStabilizerRecipe::inputItem,
                        ItemStack.PACKET_CODEC, EntropicStabilizerRecipe::output,
                        EntropicStabilizerRecipe::new);

        @Override
        public MapCodec<EntropicStabilizerRecipe> codec() {
            return CODEC;
        }

        @Override
        public PacketCodec<RegistryByteBuf, EntropicStabilizerRecipe> packetCodec() {
            return STREAM_CODEC;
        }
        
    }
}
