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 EntropicEntanglerRecipe(Ingredient inputItem, Ingredient inputItem2, ItemStack output) implements Recipe<EntropicEntanglerRecipeInput> {

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

    // Read JSON Files
    // Turns into new EntropicEvisceratorRecipe

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

        ItemStack slot0 = input.getStackInSlot(0);
        ItemStack slot1 = input.getStackInSlot(1);

        // Allow either order (A+B or B+A)
        return (inputItem.test(slot0) && inputItem2.test(slot1)) ||
                (inputItem.test(slot1) && inputItem2.test(slot0));
    }

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

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

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

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

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

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