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 EntropicEvisceratorRecipe(Ingredient inputItem, ItemStack output) implements Recipe<EntropicEvisceratorRecipeInput> {

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

    // Read JSON Files
    // Turns into new EntropicEvisceratorRecipe

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

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

    @Override
    public ItemStack craft(EntropicEvisceratorRecipeInput 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_EVISCERATOR_SERIALIZER;
    }

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

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

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

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

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