package com.joelcrosby.fluxpylons.tileentity.container;

import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.inventory.Container;
import net.minecraft.inventory.Slot;
import net.minecraft.item.ItemStack;

import com.joelcrosby.fluxpylons.tileentity.TEBreakerAdv;

public class ContainerBreakerAdv extends Container {
	
	protected TEBreakerAdv tileEntity;
	
	 public ContainerBreakerAdv(InventoryPlayer inventoryPlayer, TEBreakerAdv te)
	    {
	        tileEntity = te;

	        addSlotToContainer(new Slot(tileEntity, 0, 62, 17));
	        addSlotToContainer(new Slot(tileEntity, 1, 80, 17));
	        addSlotToContainer(new Slot(tileEntity, 2, 98, 17));
	        
	        addSlotToContainer(new Slot(tileEntity, 3, 62, 35));
	        addSlotToContainer(new Slot(tileEntity, 4, 80, 35));
	        addSlotToContainer(new Slot(tileEntity, 5, 98, 35));
	        
	        addSlotToContainer(new Slot(tileEntity, 6, 62, 53));
	        addSlotToContainer(new Slot(tileEntity, 7, 80, 53));
	        addSlotToContainer(new Slot(tileEntity, 8, 98, 53));

	        bindPlayerInventory(inventoryPlayer);
	    }
	 
	 @Override
		public boolean canInteractWith(EntityPlayer player) {

			return tileEntity.isUseableByPlayer(player);
		}
	 
	 
	 protected void bindPlayerInventory(InventoryPlayer inventoryPlayer)
	    {

	        for (int i = 0; i < 3; i++)
	        {
	            for (int j = 0; j < 9; j++)
	            {
	                addSlotToContainer(new Slot(inventoryPlayer, j + i * 9 + 9, 8 + j * 18, 166 - (4 - i) * 18 - 10));
	            }
	        }

	        for (int i = 0; i < 9; i++)
	        {
	            addSlotToContainer(new Slot(inventoryPlayer, i , 8 + i * 18, 142));
	        }
	    }

		
	@Override
	public ItemStack transferStackInSlot(EntityPlayer par1EntityPlayer, int slotID)
 {
     ItemStack itemstack = null;
     Slot slot = (Slot)this.inventorySlots.get(slotID);

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

         if (slotID < 3 * 3)
         {
             if (!this.mergeItemStack(itemstack1, 3 * 3, this.inventorySlots.size(), true))
             {
                 return null;
             }
         }
         else if (!this.mergeItemStack(itemstack1, 0, 3 * 3, false))
         {
             return null;
         }

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

     return itemstack;
    
 }
 
}


