package net.minecraft.world.inventory;

import net.minecraft.world.item.trading.MerchantOffers;
import net.minecraft.world.entity.player.Player;
import java.util.List;
import net.minecraft.world.ContainerHelper;
import java.util.Iterator;
import javax.annotation.Nullable;
import net.minecraft.world.item.trading.MerchantOffer;
import net.minecraft.world.item.ItemStack;
import net.minecraft.core.NonNullList;
import net.minecraft.world.item.trading.Merchant;
import net.minecraft.world.Container;

public class MerchantContainer implements Container {
    private final Merchant merchant;
    private final NonNullList<ItemStack> itemStacks;
    @Nullable
    private MerchantOffer activeOffer;
    private int selectionHint;
    private int futureXp;
    
    public MerchantContainer(final Merchant boy) {
        this.itemStacks = NonNullList.<ItemStack>withSize(3, ItemStack.EMPTY);
        this.merchant = boy;
    }
    
    @Override
    public int getContainerSize() {
        return this.itemStacks.size();
    }
    
    @Override
    public boolean isEmpty() {
        for (final ItemStack bki3 : this.itemStacks) {
            if (!bki3.isEmpty()) {
                return false;
            }
        }
        return true;
    }
    
    @Override
    public ItemStack getItem(final int integer) {
        return this.itemStacks.get(integer);
    }
    
    @Override
    public ItemStack removeItem(final int integer1, final int integer2) {
        final ItemStack bki4 = this.itemStacks.get(integer1);
        if (integer1 == 2 && !bki4.isEmpty()) {
            return ContainerHelper.removeItem(this.itemStacks, integer1, bki4.getCount());
        }
        final ItemStack bki5 = ContainerHelper.removeItem(this.itemStacks, integer1, integer2);
        if (!bki5.isEmpty() && this.isPaymentSlot(integer1)) {
            this.updateSellItem();
        }
        return bki5;
    }
    
    private boolean isPaymentSlot(final int integer) {
        return integer == 0 || integer == 1;
    }
    
    @Override
    public ItemStack removeItemNoUpdate(final int integer) {
        return ContainerHelper.takeItem(this.itemStacks, integer);
    }
    
    @Override
    public void setItem(final int integer, final ItemStack bki) {
        this.itemStacks.set(integer, bki);
        if (!bki.isEmpty() && bki.getCount() > this.getMaxStackSize()) {
            bki.setCount(this.getMaxStackSize());
        }
        if (this.isPaymentSlot(integer)) {
            this.updateSellItem();
        }
    }
    
    @Override
    public boolean stillValid(final Player bec) {
        return this.merchant.getTradingPlayer() == bec;
    }
    
    @Override
    public void setChanged() {
        this.updateSellItem();
    }
    
    public void updateSellItem() {
        this.activeOffer = null;
        ItemStack bki2;
        ItemStack bki3;
        if (this.itemStacks.get(0).isEmpty()) {
            bki2 = this.itemStacks.get(1);
            bki3 = ItemStack.EMPTY;
        }
        else {
            bki2 = this.itemStacks.get(0);
            bki3 = this.itemStacks.get(1);
        }
        if (bki2.isEmpty()) {
            this.setItem(2, ItemStack.EMPTY);
            this.futureXp = 0;
            return;
        }
        final MerchantOffers bpa4 = this.merchant.getOffers();
        if (!bpa4.isEmpty()) {
            MerchantOffer boz5 = bpa4.getRecipeFor(bki2, bki3, this.selectionHint);
            if (boz5 == null || boz5.isOutOfStock()) {
                this.activeOffer = boz5;
                boz5 = bpa4.getRecipeFor(bki3, bki2, this.selectionHint);
            }
            if (boz5 != null && !boz5.isOutOfStock()) {
                this.activeOffer = boz5;
                this.setItem(2, boz5.assemble());
                this.futureXp = boz5.getXp();
            }
            else {
                this.setItem(2, ItemStack.EMPTY);
                this.futureXp = 0;
            }
        }
        this.merchant.notifyTradeUpdated(this.getItem(2));
    }
    
    @Nullable
    public MerchantOffer getActiveOffer() {
        return this.activeOffer;
    }
    
    public void setSelectionHint(final int integer) {
        this.selectionHint = integer;
        this.updateSellItem();
    }
    
    @Override
    public void clearContent() {
        this.itemStacks.clear();
    }
    
    public int getFutureXp() {
        return this.futureXp;
    }
}
