package net.minecraft.world.inventory;

import net.minecraft.world.item.trading.MerchantOffer;
import net.minecraft.stats.Stats;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.Container;
import net.minecraft.world.item.trading.Merchant;
import net.minecraft.world.entity.player.Player;

public class MerchantResultSlot extends Slot {
    private final MerchantContainer slots;
    private final Player player;
    private int removeCount;
    private final Merchant merchant;
    
    public MerchantResultSlot(final Player bec, final Merchant boy, final MerchantContainer bhl, final int integer4, final int integer5, final int integer6) {
        super(bhl, integer4, integer5, integer6);
        this.player = bec;
        this.merchant = boy;
        this.slots = bhl;
    }
    
    @Override
    public boolean mayPlace(final ItemStack bki) {
        return false;
    }
    
    @Override
    public ItemStack remove(final int integer) {
        if (this.hasItem()) {
            this.removeCount += Math.min(integer, this.getItem().getCount());
        }
        return super.remove(integer);
    }
    
    @Override
    protected void onQuickCraft(final ItemStack bki, final int integer) {
        this.removeCount += integer;
        this.checkTakeAchievements(bki);
    }
    
    @Override
    protected void checkTakeAchievements(final ItemStack bki) {
        bki.onCraftedBy(this.player.level, this.player, this.removeCount);
        this.removeCount = 0;
    }
    
    @Override
    public ItemStack onTake(final Player bec, final ItemStack bki) {
        this.checkTakeAchievements(bki);
        final MerchantOffer boz4 = this.slots.getActiveOffer();
        if (boz4 != null) {
            final ItemStack bki2 = this.slots.getItem(0);
            final ItemStack bki3 = this.slots.getItem(1);
            if (boz4.take(bki2, bki3) || boz4.take(bki3, bki2)) {
                this.merchant.notifyTrade(boz4);
                bec.awardStat(Stats.TRADED_WITH_VILLAGER);
                this.slots.setItem(0, bki2);
                this.slots.setItem(1, bki3);
            }
            this.merchant.overrideXp(this.merchant.getVillagerXp() + boz4.getXp());
        }
        return bki;
    }
}
