package common.inventory;

import common.block.tech.BlockWorkbench;
import common.entity.npc.EntityNPC;
import common.init.CraftingRegistry;
import common.item.ItemStack;
import common.util.BlockPos;
import common.world.World;

public class ContainerWorkbench extends Container
{
    private final int craftSlots;
    public final InventoryCrafting craftMatrix;
    public final IInventory craftResult = new InventoryCraftResult();
    private final World worldObj;
    private final BlockPos pos;
    private final BlockWorkbench block;

    public ContainerWorkbench(InventoryPlayer playerInventory, World worldIn, BlockPos posIn, BlockWorkbench block)
    {
    	int size = block.getSize();
    	this.block = block;
        this.worldObj = worldIn;
        this.pos = posIn;
        this.craftSlots = size * size;
        this.craftMatrix = new InventoryCrafting(this, size, size);
        this.addSlotToContainer(new SlotCrafting(playerInventory.player, this.craftMatrix, this.craftResult, 0, 134, 17 + ((size - 1) * 18) / 2));

        for (int i = 0; i < size; ++i)
        {
            for (int j = 0; j < size; ++j)
            {
                this.addSlotToContainer(new Slot(this.craftMatrix, j + i * size, 26 + j * 18, 17 + i * 18));
            }
        }

        for (int k = 0; k < 3; ++k)
        {
            for (int i1 = 0; i1 < 9; ++i1)
            {
                this.addSlotToContainer(new Slot(playerInventory, i1 + k * 9 + 9, 8 + i1 * 18, 30 + size * 18 + k * 18));
            }
        }

        for (int l = 0; l < 9; ++l)
        {
            this.addSlotToContainer(new Slot(playerInventory, l, 8 + l * 18, 88 + size * 18));
        }

        this.onCraftMatrixChanged(this.craftMatrix);
    }

    /**
     * Callback for when the crafting matrix is changed.
     */
    public void onCraftMatrixChanged(IInventory inventoryIn)
    {
        this.craftResult.setInventorySlotContents(0, CraftingRegistry.getMatching(this.craftMatrix, this.worldObj));
    }

    /**
     * Called when the container is closed.
     */
    public void onContainerClosed(EntityNPC playerIn)
    {
        super.onContainerClosed(playerIn);

        if (!this.worldObj.client)
        {
            for (int i = 0; i < this.craftSlots; ++i)
            {
                ItemStack itemstack = this.craftMatrix.removeStackFromSlot(i);

                if (itemstack != null)
                {
                    playerIn.dropPlayerItemWithRandomChoice(itemstack, false);
                }
            }
        }
    }

    public boolean canInteractWith(EntityNPC playerIn)
    {
        return this.worldObj.getState(this.pos).getBlock() != this.block ? false : playerIn.getDistanceSq((double)this.pos.getX() + 0.5D, (double)this.pos.getY() + 0.5D, (double)this.pos.getZ() + 0.5D) <= 64.0D;
    }

    /**
     * Take a stack from the specified inventory slot.
     */
    public ItemStack transferStackInSlot(EntityNPC playerIn, int index)
    {
        ItemStack itemstack = null;
        Slot slot = this.inventorySlots.get(index);

        if (slot != null && slot.getHasStack())
        {
            ItemStack itemstack1 = slot.getStack();
            itemstack = itemstack1.copy();

            if (index == 0)
            {
                if (!this.mergeItemStack(itemstack1, this.craftSlots + 1, this.craftSlots + 37, true))
                {
                    return null;
                }

                slot.onSlotChange(itemstack1, itemstack);
            }
            else if (index >= this.craftSlots + 1 && index < this.craftSlots + 28)
            {
                if (!this.mergeItemStack(itemstack1, this.craftSlots + 28, this.craftSlots + 37, false))
                {
                    return null;
                }
            }
            else if (index >= this.craftSlots + 28 && index < this.craftSlots + 37)
            {
                if (!this.mergeItemStack(itemstack1, this.craftSlots + 1, this.craftSlots + 28, false))
                {
                    return null;
                }
            }
            else if (!this.mergeItemStack(itemstack1, this.craftSlots + 1, this.craftSlots + 37, false))
            {
                return null;
            }

            if (itemstack1.size == 0)
            {
                slot.putStack((ItemStack)null);
            }
            else
            {
                slot.onSlotChanged();
            }

            if (itemstack1.size == itemstack.size)
            {
                return null;
            }

            slot.onPickupFromSlot(playerIn, itemstack1);
        }

        return itemstack;
    }

    /**
     * Called to determine if the current slot is valid for the stack merging (double-click) code. The stack passed in
     * is null for the initial slot that was double-clicked.
     */
    public boolean canMergeSlot(ItemStack stack, Slot slotIn)
    {
        return slotIn.inventory != this.craftResult && super.canMergeSlot(stack, slotIn);
    }
}
