package common.inventory;

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

public class InventoryMerchant implements IInventory
{
    private final EntityNPC theMerchant;
    private MerchantRecipeList recipeList;
    private ItemStack[] theInventory = new ItemStack[3];
    private final EntityNPC thePlayer;
    private MerchantRecipe currentRecipe;
    private int currentRecipeIndex;

    public InventoryMerchant(EntityNPC thePlayerIn, EntityNPC theMerchantIn)
    {
        this.thePlayer = thePlayerIn;
        this.theMerchant = theMerchantIn;
    }

    public void setRecipes(MerchantRecipeList recipes)
    {
        this.recipeList = recipes;
    }

    public MerchantRecipeList getRecipes()
    {
        return this.recipeList;
    }

    /**
     * Returns the number of slots in the inventory.
     */
    public int getSizeInventory()
    {
        return this.theInventory.length;
    }

    /**
     * Returns the stack in the given slot.
     */
    public ItemStack getStackInSlot(int index)
    {
        return this.theInventory[index];
    }

    /**
     * Removes up to a specified number of items from an inventory slot and returns them in a new stack.
     */
    public ItemStack decrStackSize(int index, int count)
    {
        if (this.theInventory[index] != null)
        {
            if (index == 2)
            {
                ItemStack itemstack2 = this.theInventory[index];
                this.theInventory[index] = null;
                return itemstack2;
            }
            else if (this.theInventory[index].size <= count)
            {
                ItemStack itemstack1 = this.theInventory[index];
                this.theInventory[index] = null;

                if (this.inventoryResetNeededOnSlotChange(index))
                {
                    this.resetRecipeAndSlots();
                }

                return itemstack1;
            }
            else
            {
                ItemStack itemstack = this.theInventory[index].splitStack(count);

                if (this.theInventory[index].size == 0)
                {
                    this.theInventory[index] = null;
                }

                if (this.inventoryResetNeededOnSlotChange(index))
                {
                    this.resetRecipeAndSlots();
                }

                return itemstack;
            }
        }
        else
        {
            return null;
        }
    }

    /**
     * if par1 slot has changed, does resetRecipeAndSlots need to be called?
     */
    private boolean inventoryResetNeededOnSlotChange(int p_70469_1_)
    {
        return p_70469_1_ == 0 || p_70469_1_ == 1;
    }

    /**
     * Removes a stack from the given slot and returns it.
     */
    public ItemStack removeStackFromSlot(int index)
    {
        if (this.theInventory[index] != null)
        {
            ItemStack itemstack = this.theInventory[index];
            this.theInventory[index] = null;
            return itemstack;
        }
        else
        {
            return null;
        }
    }

    /**
     * Sets the given item stack to the specified slot in the inventory (can be crafting or armor sections).
     */
    public void setInventorySlotContents(int index, ItemStack stack)
    {
        this.theInventory[index] = stack;

        if (stack != null && stack.size > this.getInventoryStackLimit())
        {
            stack.size = this.getInventoryStackLimit();
        }

        if (this.inventoryResetNeededOnSlotChange(index))
        {
            this.resetRecipeAndSlots();
        }
    }

//    /**
//     * Get the name of this object. For players this returns their username
//     */
//    public String getName()
//    {
//        return "entity.NPC.name";
//    }
//
//    /**
//     * Returns true if this thing is named
//     */
//    public boolean hasCustomName()
//    {
//        return false;
//    }

    /**
     * Get the formatted ChatComponent that will be used for the sender's username in chat
     */
    public String getCommandName()
    {
        return "NSC"; // (TextComponent)(this.hasCustomName() ? new TextComponent(this.getName()) : new Translation(this.getName()));
    }

    /**
     * Returns the maximum stack size for a inventory slot. Seems to always be 64, possibly will be extended.
     */
    public int getInventoryStackLimit()
    {
        return ItemStack.MAX_SIZE;
    }

    /**
     * Do not make give this method the name canInteractWith because it clashes with Container
     */
    public boolean isUseableByPlayer(EntityNPC player)
    {
        return this.theMerchant == null || this.theMerchant.getTalking() == player;
    }

    public void openInventory(EntityNPC player)
    {
    }

    public void closeInventory(EntityNPC player)
    {
    }

    /**
     * Returns true if automation is allowed to insert the given stack (ignoring stack size) into the given slot.
     */
    public boolean isItemValidForSlot(int index, ItemStack stack)
    {
        return true;
    }

    /**
     * For tile entities, ensures the chunk containing the tile entity is saved to disk later - the game won't think it
     * hasn't changed and skip it.
     */
    public void markDirty()
    {
        this.resetRecipeAndSlots();
    }

    public void resetRecipeAndSlots()
    {
        this.currentRecipe = null;
        ItemStack itemstack = this.theInventory[0];
        ItemStack itemstack1 = this.theInventory[1];

        if (itemstack == null)
        {
            itemstack = itemstack1;
            itemstack1 = null;
        }

        if (itemstack == null)
        {
            this.setInventorySlotContents(2, (ItemStack)null);
        }
        else
        {
            MerchantRecipeList merchantrecipelist = this.theMerchant == null ? this.recipeList : this.theMerchant.getTrades(this.thePlayer);

            if (merchantrecipelist != null)
            {
                MerchantRecipe merchantrecipe = merchantrecipelist.canUse(itemstack, itemstack1, this.currentRecipeIndex);

                if (merchantrecipe != null) // && !merchantrecipe.isRecipeDisabled())
                {
                    this.currentRecipe = merchantrecipe;
                    this.setInventorySlotContents(2, merchantrecipe.result().copy());
                }
                else if (itemstack1 != null)
                {
                    merchantrecipe = merchantrecipelist.canUse(itemstack1, itemstack, this.currentRecipeIndex);

                    if (merchantrecipe != null) // && !merchantrecipe.isRecipeDisabled())
                    {
                        this.currentRecipe = merchantrecipe;
                        this.setInventorySlotContents(2, merchantrecipe.result().copy());
                    }
                    else
                    {
                        this.setInventorySlotContents(2, (ItemStack)null);
                    }
                }
                else
                {
                    this.setInventorySlotContents(2, (ItemStack)null);
                }
            }
        }

//        if(this.theMerchant != null)
//        	this.theMerchant.verifySellingItem(this.getStackInSlot(2));
    }

    public MerchantRecipe getCurrentRecipe()
    {
        return this.currentRecipe;
    }

    public void setCurrentRecipeIndex(int currentRecipeIndexIn)
    {
        this.currentRecipeIndex = currentRecipeIndexIn;
        this.resetRecipeAndSlots();
    }

    public int getField(int id)
    {
        return 0;
    }

    public void setField(int id, int value)
    {
    }

    public int getFieldCount()
    {
        return 0;
    }

    public void clear()
    {
        for (int i = 0; i < this.theInventory.length; ++i)
        {
            this.theInventory[i] = null;
        }
    }
}
