package net.minecraft.world.item.crafting;

import net.minecraft.world.Container;
import net.minecraft.core.NonNullList;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.world.level.ItemLike;
import net.minecraft.world.item.WrittenBookItem;
import net.minecraft.world.item.Items;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.Level;
import net.minecraft.world.inventory.CraftingContainer;
import net.minecraft.resources.ResourceLocation;

public class BookCloningRecipe extends CustomRecipe {
    public BookCloningRecipe(final ResourceLocation sm) {
        super(sm);
    }
    
    @Override
    public boolean matches(final CraftingContainer baz, final Level bjw) {
        int integer4 = 0;
        ItemStack ben5 = ItemStack.EMPTY;
        for (int integer5 = 0; integer5 < baz.getContainerSize(); ++integer5) {
            final ItemStack ben6 = baz.getItem(integer5);
            if (!ben6.isEmpty()) {
                if (ben6.getItem() == Items.WRITTEN_BOOK) {
                    if (!ben5.isEmpty()) {
                        return false;
                    }
                    ben5 = ben6;
                }
                else {
                    if (ben6.getItem() != Items.WRITABLE_BOOK) {
                        return false;
                    }
                    ++integer4;
                }
            }
        }
        return !ben5.isEmpty() && ben5.hasTag() && integer4 > 0;
    }
    
    @Override
    public ItemStack assemble(final CraftingContainer baz) {
        int integer3 = 0;
        ItemStack ben4 = ItemStack.EMPTY;
        for (int integer4 = 0; integer4 < baz.getContainerSize(); ++integer4) {
            final ItemStack ben5 = baz.getItem(integer4);
            if (!ben5.isEmpty()) {
                if (ben5.getItem() == Items.WRITTEN_BOOK) {
                    if (!ben4.isEmpty()) {
                        return ItemStack.EMPTY;
                    }
                    ben4 = ben5;
                }
                else {
                    if (ben5.getItem() != Items.WRITABLE_BOOK) {
                        return ItemStack.EMPTY;
                    }
                    ++integer3;
                }
            }
        }
        if (ben4.isEmpty() || !ben4.hasTag() || integer3 < 1 || WrittenBookItem.getGeneration(ben4) >= 2) {
            return ItemStack.EMPTY;
        }
        final ItemStack ben6 = new ItemStack(Items.WRITTEN_BOOK, integer3);
        final CompoundTag jt6 = ben4.getTag().copy();
        jt6.putInt("generation", WrittenBookItem.getGeneration(ben4) + 1);
        ben6.setTag(jt6);
        return ben6;
    }
    
    @Override
    public NonNullList<ItemStack> getRemainingItems(final CraftingContainer baz) {
        final NonNullList<ItemStack> fy3 = NonNullList.<ItemStack>withSize(baz.getContainerSize(), ItemStack.EMPTY);
        for (int integer4 = 0; integer4 < fy3.size(); ++integer4) {
            final ItemStack ben5 = baz.getItem(integer4);
            if (ben5.getItem().hasCraftingRemainingItem()) {
                fy3.set(integer4, new ItemStack(ben5.getItem().getCraftingRemainingItem()));
            }
            else if (ben5.getItem() instanceof WrittenBookItem) {
                final ItemStack ben6 = ben5.copy();
                ben6.setCount(1);
                fy3.set(integer4, ben6);
                break;
            }
        }
        return fy3;
    }
    
    @Override
    public RecipeSerializer<?> getSerializer() {
        return RecipeSerializer.BOOK_CLONING;
    }
    
    @Override
    public boolean canCraftInDimensions(final int integer1, final int integer2) {
        return integer1 >= 3 && integer2 >= 3;
    }
}
