package net.minecraft.world.item.trading;

import java.util.AbstractList;
import net.minecraft.nbt.Tag;
import net.minecraft.network.FriendlyByteBuf;
import javax.annotation.Nullable;
import net.minecraft.world.item.ItemStack;
import net.minecraft.nbt.ListTag;
import net.minecraft.nbt.CompoundTag;
import java.util.ArrayList;

public class MerchantOffers extends ArrayList<MerchantOffer> {
    public MerchantOffers() {
    }
    
    public MerchantOffers(final CompoundTag le) {
        final ListTag lk3 = le.getList("Recipes", 10);
        for (int integer4 = 0; integer4 < lk3.size(); ++integer4) {
            this.add(new MerchantOffer(lk3.getCompound(integer4)));
        }
    }
    
    @Nullable
    public MerchantOffer getRecipeFor(final ItemStack bki1, final ItemStack bki2, final int integer) {
        if (integer <= 0 || integer >= this.size()) {
            for (int integer2 = 0; integer2 < this.size(); ++integer2) {
                final MerchantOffer boz6 = this.get(integer2);
                if (boz6.satisfiedBy(bki1, bki2)) {
                    return boz6;
                }
            }
            return null;
        }
        final MerchantOffer boz7 = this.get(integer);
        if (boz7.satisfiedBy(bki1, bki2)) {
            return boz7;
        }
        return null;
    }
    
    public void writeToStream(final FriendlyByteBuf mg) {
        mg.writeByte((byte)(this.size() & 0xFF));
        for (int integer3 = 0; integer3 < this.size(); ++integer3) {
            final MerchantOffer boz4 = this.get(integer3);
            mg.writeItem(boz4.getBaseCostA());
            mg.writeItem(boz4.getResult());
            final ItemStack bki5 = boz4.getCostB();
            mg.writeBoolean(!bki5.isEmpty());
            if (!bki5.isEmpty()) {
                mg.writeItem(bki5);
            }
            mg.writeBoolean(boz4.isOutOfStock());
            mg.writeInt(boz4.getUses());
            mg.writeInt(boz4.getMaxUses());
            mg.writeInt(boz4.getXp());
            mg.writeInt(boz4.getSpecialPriceDiff());
            mg.writeFloat(boz4.getPriceMultiplier());
            mg.writeInt(boz4.getDemand());
        }
    }
    
    public static MerchantOffers createFromStream(final FriendlyByteBuf mg) {
        final MerchantOffers bpa2 = new MerchantOffers();
        for (int integer3 = mg.readByte() & 0xFF, integer4 = 0; integer4 < integer3; ++integer4) {
            final ItemStack bki5 = mg.readItem();
            final ItemStack bki6 = mg.readItem();
            ItemStack bki7 = ItemStack.EMPTY;
            if (mg.readBoolean()) {
                bki7 = mg.readItem();
            }
            final boolean boolean8 = mg.readBoolean();
            final int integer5 = mg.readInt();
            final int integer6 = mg.readInt();
            final int integer7 = mg.readInt();
            final int integer8 = mg.readInt();
            final float float13 = mg.readFloat();
            final int integer9 = mg.readInt();
            final MerchantOffer boz15 = new MerchantOffer(bki5, bki7, bki6, integer5, integer6, integer7, float13, integer9);
            if (boolean8) {
                boz15.setToOutOfStock();
            }
            boz15.setSpecialPriceDiff(integer8);
            bpa2.add(boz15);
        }
        return bpa2;
    }
    
    public CompoundTag createTag() {
        final CompoundTag le2 = new CompoundTag();
        final ListTag lk3 = new ListTag();
        for (int integer4 = 0; integer4 < this.size(); ++integer4) {
            final MerchantOffer boz5 = this.get(integer4);
            ((AbstractList<CompoundTag>)lk3).add(boz5.createTag());
        }
        le2.put("Recipes", lk3);
        return le2;
    }
}
