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 uh) {
        super(uh);
    }
    
    @Override
    public boolean matches(final CraftingContainer bgu, final Level bqb) {
        int integer4 = 0;
        ItemStack bki5 = ItemStack.EMPTY;
        for (int integer5 = 0; integer5 < bgu.getContainerSize(); ++integer5) {
            final ItemStack bki6 = bgu.getItem(integer5);
            if (!bki6.isEmpty()) {
                if (bki6.getItem() == Items.WRITTEN_BOOK) {
                    if (!bki5.isEmpty()) {
                        return false;
                    }
                    bki5 = bki6;
                }
                else {
                    if (bki6.getItem() != Items.WRITABLE_BOOK) {
                        return false;
                    }
                    ++integer4;
                }
            }
        }
        return !bki5.isEmpty() && bki5.hasTag() && integer4 > 0;
    }
    
    @Override
    public ItemStack assemble(final CraftingContainer bgu) {
        int integer3 = 0;
        ItemStack bki4 = ItemStack.EMPTY;
        for (int integer4 = 0; integer4 < bgu.getContainerSize(); ++integer4) {
            final ItemStack bki5 = bgu.getItem(integer4);
            if (!bki5.isEmpty()) {
                if (bki5.getItem() == Items.WRITTEN_BOOK) {
                    if (!bki4.isEmpty()) {
                        return ItemStack.EMPTY;
                    }
                    bki4 = bki5;
                }
                else {
                    if (bki5.getItem() != Items.WRITABLE_BOOK) {
                        return ItemStack.EMPTY;
                    }
                    ++integer3;
                }
            }
        }
        if (bki4.isEmpty() || !bki4.hasTag() || integer3 < 1 || WrittenBookItem.getGeneration(bki4) >= 2) {
            return ItemStack.EMPTY;
        }
        final ItemStack bki6 = new ItemStack(Items.WRITTEN_BOOK, integer3);
        final CompoundTag le6 = bki4.getTag().copy();
        le6.putInt("generation", WrittenBookItem.getGeneration(bki4) + 1);
        bki6.setTag(le6);
        return bki6;
    }
    
    @Override
    public NonNullList<ItemStack> getRemainingItems(final CraftingContainer bgu) {
        final NonNullList<ItemStack> gi3 = NonNullList.<ItemStack>withSize(bgu.getContainerSize(), ItemStack.EMPTY);
        for (int integer4 = 0; integer4 < gi3.size(); ++integer4) {
            final ItemStack bki5 = bgu.getItem(integer4);
            if (bki5.getItem().hasCraftingRemainingItem()) {
                gi3.set(integer4, new ItemStack(bki5.getItem().getCraftingRemainingItem()));
            }
            else if (bki5.getItem() instanceof WrittenBookItem) {
                final ItemStack bki6 = bki5.copy();
                bki6.setCount(1);
                gi3.set(integer4, bki6);
                break;
            }
        }
        return gi3;
    }
    
    @Override
    public RecipeSerializer<?> getSerializer() {
        return RecipeSerializer.BOOK_CLONING;
    }
    
    @Override
    public boolean canCraftInDimensions(final int integer1, final int integer2) {
        return integer1 >= 3 && integer2 >= 3;
    }
}
