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 sm) {
        super(sm);
    }
    
    @Override
    public boolean matches(final CraftingContainer baz, final Level bjw) {
        DyeColor bdj4 = null;
        ItemStack ben5 = null;
        ItemStack ben6 = null;
        for (int integer7 = 0; integer7 < baz.getContainerSize(); ++integer7) {
            final ItemStack ben7 = baz.getItem(integer7);
            final Item bei9 = ben7.getItem();
            if (bei9 instanceof BannerItem) {
                final BannerItem bcm10 = (BannerItem)bei9;
                if (bdj4 == null) {
                    bdj4 = bcm10.getColor();
                }
                else if (bdj4 != bcm10.getColor()) {
                    return false;
                }
                final int integer8 = BannerBlockEntity.getPatternCount(ben7);
                if (integer8 > 6) {
                    return false;
                }
                if (integer8 > 0) {
                    if (ben5 != null) {
                        return false;
                    }
                    ben5 = ben7;
                }
                else {
                    if (ben6 != null) {
                        return false;
                    }
                    ben6 = ben7;
                }
            }
        }
        return ben5 != null && ben6 != null;
    }
    
    @Override
    public ItemStack assemble(final CraftingContainer baz) {
        for (int integer3 = 0; integer3 < baz.getContainerSize(); ++integer3) {
            final ItemStack ben4 = baz.getItem(integer3);
            if (!ben4.isEmpty()) {
                final int integer4 = BannerBlockEntity.getPatternCount(ben4);
                if (integer4 > 0 && integer4 <= 6) {
                    final ItemStack ben5 = ben4.copy();
                    ben5.setCount(1);
                    return ben5;
                }
            }
        }
        return ItemStack.EMPTY;
    }
    
    @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.isEmpty()) {
                if (ben5.getItem().hasCraftingRemainingItem()) {
                    fy3.set(integer4, new ItemStack(ben5.getItem().getCraftingRemainingItem()));
                }
                else if (ben5.hasTag() && BannerBlockEntity.getPatternCount(ben5) > 0) {
                    final ItemStack ben6 = ben5.copy();
                    ben6.setCount(1);
                    fy3.set(integer4, ben6);
                }
            }
        }
        return fy3;
    }
    
    @Override
    public RecipeSerializer<?> getSerializer() {
        return RecipeSerializer.BANNER_DUPLICATE;
    }
    
    @Override
    public boolean canCraftInDimensions(final int integer1, final int integer2) {
        return integer1 * integer2 >= 2;
    }
}
