package net.minecraft.world.item.crafting;

import net.minecraft.world.Container;
import net.minecraft.world.item.Item;
import net.minecraft.world.level.ItemLike;
import java.util.List;
import net.minecraft.world.item.ItemStack;
import com.google.common.collect.Lists;
import net.minecraft.world.level.Level;
import net.minecraft.world.inventory.CraftingContainer;
import net.minecraft.resources.ResourceLocation;

public class RepairItemRecipe extends CustomRecipe {
    public RepairItemRecipe(final ResourceLocation sm) {
        super(sm);
    }
    
    @Override
    public boolean matches(final CraftingContainer baz, final Level bjw) {
        final List<ItemStack> list4 = Lists.newArrayList();
        for (int integer5 = 0; integer5 < baz.getContainerSize(); ++integer5) {
            final ItemStack ben6 = baz.getItem(integer5);
            if (!ben6.isEmpty()) {
                list4.add(ben6);
                if (list4.size() > 1) {
                    final ItemStack ben7 = list4.get(0);
                    if (ben6.getItem() != ben7.getItem() || ben7.getCount() != 1 || ben6.getCount() != 1 || !ben7.getItem().canBeDepleted()) {
                        return false;
                    }
                }
            }
        }
        return list4.size() == 2;
    }
    
    @Override
    public ItemStack assemble(final CraftingContainer baz) {
        final List<ItemStack> list3 = Lists.newArrayList();
        for (int integer4 = 0; integer4 < baz.getContainerSize(); ++integer4) {
            final ItemStack ben5 = baz.getItem(integer4);
            if (!ben5.isEmpty()) {
                list3.add(ben5);
                if (list3.size() > 1) {
                    final ItemStack ben6 = list3.get(0);
                    if (ben5.getItem() != ben6.getItem() || ben6.getCount() != 1 || ben5.getCount() != 1 || !ben6.getItem().canBeDepleted()) {
                        return ItemStack.EMPTY;
                    }
                }
            }
        }
        if (list3.size() == 2) {
            final ItemStack ben7 = list3.get(0);
            final ItemStack ben5 = list3.get(1);
            if (ben7.getItem() == ben5.getItem() && ben7.getCount() == 1 && ben5.getCount() == 1 && ben7.getItem().canBeDepleted()) {
                final Item bei6 = ben7.getItem();
                final int integer5 = bei6.getMaxDamage() - ben7.getDamageValue();
                final int integer6 = bei6.getMaxDamage() - ben5.getDamageValue();
                final int integer7 = integer5 + integer6 + bei6.getMaxDamage() * 5 / 100;
                int integer8 = bei6.getMaxDamage() - integer7;
                if (integer8 < 0) {
                    integer8 = 0;
                }
                final ItemStack ben8 = new ItemStack(ben7.getItem());
                ben8.setDamageValue(integer8);
                return ben8;
            }
        }
        return ItemStack.EMPTY;
    }
    
    @Override
    public boolean canCraftInDimensions(final int integer1, final int integer2) {
        return integer1 * integer2 >= 2;
    }
    
    @Override
    public RecipeSerializer<?> getSerializer() {
        return RecipeSerializer.REPAIR_ITEM;
    }
}
