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 biy) {
        this.itemStacks = NonNullList.<ItemStack>withSize(3, ItemStack.EMPTY);
        this.merchant = biy;
    }
    
    @Override
    public int getContainerSize() {
        return this.itemStacks.size();
    }
    
    @Override
    public boolean isEmpty() {
        for (final ItemStack ben3 : this.itemStacks) {
            if (!ben3.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 ben4 = this.itemStacks.get(integer1);
        if (integer1 == 2 && !ben4.isEmpty()) {
            return ContainerHelper.removeItem(this.itemStacks, integer1, ben4.getCount());
        }
        final ItemStack ben5 = ContainerHelper.removeItem(this.itemStacks, integer1, integer2);
        if (!ben5.isEmpty() && this.isPaymentSlot(integer1)) {
            this.updateSellItem();
        }
        return ben5;
    }
    
    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 ben) {
        this.itemStacks.set(integer, ben);
        if (!ben.isEmpty() && ben.getCount() > this.getMaxStackSize()) {
            ben.setCount(this.getMaxStackSize());
        }
        if (this.isPaymentSlot(integer)) {
            this.updateSellItem();
        }
    }
    
    @Override
    public boolean stillValid(final Player ayj) {
        return this.merchant.getTradingPlayer() == ayj;
    }
    
    @Override
    public void setChanged() {
        this.updateSellItem();
    }
    
    public void updateSellItem() {
        this.activeOffer = null;
        ItemStack ben2;
        ItemStack ben3;
        if (this.itemStacks.get(0).isEmpty()) {
            ben2 = this.itemStacks.get(1);
            ben3 = ItemStack.EMPTY;
        }
        else {
            ben2 = this.itemStacks.get(0);
            ben3 = this.itemStacks.get(1);
        }
        if (ben2.isEmpty()) {
            this.setItem(2, ItemStack.EMPTY);
            this.futureXp = 0;
            return;
        }
        final MerchantOffers bja4 = this.merchant.getOffers();
        if (!bja4.isEmpty()) {
            MerchantOffer biz5 = bja4.getRecipeFor(ben2, ben3, this.selectionHint);
            if (biz5 == null || biz5.isOutOfStock()) {
                this.activeOffer = biz5;
                biz5 = bja4.getRecipeFor(ben3, ben2, this.selectionHint);
            }
            if (biz5 != null && !biz5.isOutOfStock()) {
                this.activeOffer = biz5;
                this.setItem(2, biz5.assemble());
                this.futureXp = biz5.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;
    }
}
