package common.inventory;

import common.entity.npc.EntityNPC;
import common.item.ItemStack;
import common.village.MerchantRecipe;

public class SlotMerchantResult extends Slot
{
    private final InventoryMerchant theMerchantInventory;
    private EntityNPC thePlayer;
    private int traded;
//    private final EntityVillager theMerchant;

    public SlotMerchantResult(EntityNPC player, InventoryMerchant merchantInventory, int slotIndex, int xPosition, int yPosition)
    {
        super(merchantInventory, slotIndex, xPosition, yPosition);
        this.thePlayer = player;
//        this.theMerchant = merchant;
        this.theMerchantInventory = merchantInventory;
    }

    /**
     * Check if the stack is a valid item for this slot. Always true beside for the armor slots.
     */
    public boolean isItemValid(ItemStack stack)
    {
        return false;
    }

    /**
     * Decrease the size of the stack in slot (first int arg) by the amount of the second int arg. Returns the new
     * stack.
     */
    public ItemStack decrStackSize(int amount)
    {
        if (this.getHasStack())
        {
            this.traded += Math.min(amount, this.getStack().size);
        }

        return super.decrStackSize(amount);
    }

    /**
     * the itemStack passed in is the output - ie, iron ingots, and pickaxes, not ore and wood. Typically increases an
     * internal count then calls onCrafting(item).
     */
    protected void onCrafting(ItemStack stack, int amount)
    {
        this.traded += amount;
        this.onCrafting(stack);
    }

    /**
     * the itemStack passed in is the output - ie, iron ingots, and pickaxes, not ore and wood.
     */
    protected void onCrafting(ItemStack stack)
    {
//        stack.onCrafting(this.thePlayer.worldObj, this.thePlayer, this.traded);
        this.traded = 0;
    }

    public void onPickupFromSlot(EntityNPC playerIn, ItemStack stack)
    {
        this.onCrafting(stack);
        MerchantRecipe merchantrecipe = this.theMerchantInventory.getCurrentRecipe();

        if (merchantrecipe != null)
        {
            ItemStack itemstack = this.theMerchantInventory.getStackInSlot(0);
            ItemStack itemstack1 = this.theMerchantInventory.getStackInSlot(1);

            if (this.doTrade(merchantrecipe, itemstack, itemstack1) || this.doTrade(merchantrecipe, itemstack1, itemstack))
            {
//            	if(this.theMerchant != null)
//            		this.theMerchant.useRecipe(merchantrecipe);
//                playerIn.triggerAchievement(StatRegistry.timesTradedWithNpcStat);

                if (itemstack != null && itemstack.size <= 0)
                {
                    itemstack = null;
                }

                if (itemstack1 != null && itemstack1.size <= 0)
                {
                    itemstack1 = null;
                }

                this.theMerchantInventory.setInventorySlotContents(0, itemstack);
                this.theMerchantInventory.setInventorySlotContents(1, itemstack1);
            }
        }
    }

    private boolean doTrade(MerchantRecipe trade, ItemStack firstItem, ItemStack secondItem)
    {
        ItemStack itemstack = trade.first();
        ItemStack itemstack1 = trade.second();

        if (firstItem != null && firstItem.getItem() == itemstack.getItem())
        {
            if (itemstack1 != null && secondItem != null && itemstack1.getItem() == secondItem.getItem())
            {
                firstItem.size -= itemstack.size;
                secondItem.size -= itemstack1.size;
                return true;
            }

            if (itemstack1 == null && secondItem == null)
            {
                firstItem.size -= itemstack.size;
                return true;
            }
        }

        return false;
    }
}
