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 jt) {
        final ListTag jz3 = jt.getList("Recipes", 10);
        for (int integer4 = 0; integer4 < jz3.size(); ++integer4) {
            this.add(new MerchantOffer(jz3.getCompound(integer4)));
        }
    }
    
    @Nullable
    public MerchantOffer getRecipeFor(final ItemStack ben1, final ItemStack ben2, final int integer) {
        if (integer <= 0 || integer >= this.size()) {
            for (int integer2 = 0; integer2 < this.size(); ++integer2) {
                final MerchantOffer biz6 = this.get(integer2);
                if (biz6.satisfiedBy(ben1, ben2)) {
                    return biz6;
                }
            }
            return null;
        }
        final MerchantOffer biz7 = this.get(integer);
        if (biz7.satisfiedBy(ben1, ben2)) {
            return biz7;
        }
        return null;
    }
    
    public void writeToStream(final FriendlyByteBuf kv) {
        kv.writeByte((byte)(this.size() & 0xFF));
        for (int integer3 = 0; integer3 < this.size(); ++integer3) {
            final MerchantOffer biz4 = this.get(integer3);
            kv.writeItem(biz4.getBaseCostA());
            kv.writeItem(biz4.getResult());
            final ItemStack ben5 = biz4.getCostB();
            kv.writeBoolean(!ben5.isEmpty());
            if (!ben5.isEmpty()) {
                kv.writeItem(ben5);
            }
            kv.writeBoolean(biz4.isOutOfStock());
            kv.writeInt(biz4.getUses());
            kv.writeInt(biz4.getMaxUses());
            kv.writeInt(biz4.getXp());
            kv.writeInt(biz4.getSpecialPriceDiff());
            kv.writeFloat(biz4.getPriceMultiplier());
            kv.writeInt(biz4.getDemand());
        }
    }
    
    public static MerchantOffers createFromStream(final FriendlyByteBuf kv) {
        final MerchantOffers bja2 = new MerchantOffers();
        for (int integer3 = kv.readByte() & 0xFF, integer4 = 0; integer4 < integer3; ++integer4) {
            final ItemStack ben5 = kv.readItem();
            final ItemStack ben6 = kv.readItem();
            ItemStack ben7 = ItemStack.EMPTY;
            if (kv.readBoolean()) {
                ben7 = kv.readItem();
            }
            final boolean boolean8 = kv.readBoolean();
            final int integer5 = kv.readInt();
            final int integer6 = kv.readInt();
            final int integer7 = kv.readInt();
            final int integer8 = kv.readInt();
            final float float13 = kv.readFloat();
            final int integer9 = kv.readInt();
            final MerchantOffer biz15 = new MerchantOffer(ben5, ben7, ben6, integer5, integer6, integer7, float13, integer9);
            if (boolean8) {
                biz15.setToOutOfStock();
            }
            biz15.setSpecialPriceDiff(integer8);
            bja2.add(biz15);
        }
        return bja2;
    }
    
    public CompoundTag createTag() {
        final CompoundTag jt2 = new CompoundTag();
        final ListTag jz3 = new ListTag();
        for (int integer4 = 0; integer4 < this.size(); ++integer4) {
            final MerchantOffer biz5 = this.get(integer4);
            ((AbstractList<CompoundTag>)jz3).add(biz5.createTag());
        }
        jt2.put("Recipes", jz3);
        return jt2;
    }
}
