package net.minecraft.world.item.crafting;

import net.minecraft.world.Container;
import net.minecraft.world.level.ItemLike;
import net.minecraft.core.NonNullList;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.DyeColor;
import net.minecraft.world.level.block.entity.BannerBlockEntity;
import net.minecraft.world.item.BannerItem;
import net.minecraft.world.level.Level;
import net.minecraft.world.inventory.CraftingContainer;
import net.minecraft.resources.ResourceLocation;

public class BannerDuplicateRecipe extends CustomRecipe {
    public BannerDuplicateRecipe(final ResourceLocation uh) {
        super(uh);
    }
    
    @Override
    public boolean matches(final CraftingContainer bgu, final Level bqb) {
        DyeColor bje4 = null;
        ItemStack bki5 = null;
        ItemStack bki6 = null;
        for (int integer7 = 0; integer7 < bgu.getContainerSize(); ++integer7) {
            final ItemStack bki7 = bgu.getItem(integer7);
            final Item bke9 = bki7.getItem();
            if (bke9 instanceof BannerItem) {
                final BannerItem bij10 = (BannerItem)bke9;
                if (bje4 == null) {
                    bje4 = bij10.getColor();
                }
                else if (bje4 != bij10.getColor()) {
                    return false;
                }
                final int integer8 = BannerBlockEntity.getPatternCount(bki7);
                if (integer8 > 6) {
                    return false;
                }
                if (integer8 > 0) {
                    if (bki5 != null) {
                        return false;
                    }
                    bki5 = bki7;
                }
                else {
                    if (bki6 != null) {
                        return false;
                    }
                    bki6 = bki7;
                }
            }
        }
        return bki5 != null && bki6 != null;
    }
    
    @Override
    public ItemStack assemble(final CraftingContainer bgu) {
        for (int integer3 = 0; integer3 < bgu.getContainerSize(); ++integer3) {
            final ItemStack bki4 = bgu.getItem(integer3);
            if (!bki4.isEmpty()) {
                final int integer4 = BannerBlockEntity.getPatternCount(bki4);
                if (integer4 > 0 && integer4 <= 6) {
                    final ItemStack bki5 = bki4.copy();
                    bki5.setCount(1);
                    return bki5;
                }
            }
        }
        return ItemStack.EMPTY;
    }
    
    @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.isEmpty()) {
                if (bki5.getItem().hasCraftingRemainingItem()) {
                    gi3.set(integer4, new ItemStack(bki5.getItem().getCraftingRemainingItem()));
                }
                else if (bki5.hasTag() && BannerBlockEntity.getPatternCount(bki5) > 0) {
                    final ItemStack bki6 = bki5.copy();
                    bki6.setCount(1);
                    gi3.set(integer4, bki6);
                }
            }
        }
        return gi3;
    }
    
    @Override
    public RecipeSerializer<?> getSerializer() {
        return RecipeSerializer.BANNER_DUPLICATE;
    }
    
    @Override
    public boolean canCraftInDimensions(final int integer1, final int integer2) {
        return integer1 * integer2 >= 2;
    }
}
