package com.harry9137.ct.client.container;

import com.harry9137.ct.client.gui.elements.RestrictedSlot;
import com.harry9137.ct.tileentity.TileEntityTechTable;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.inventory.Container;
import net.minecraft.inventory.IInventory;
import net.minecraft.inventory.Slot;
import net.minecraft.item.ItemStack;

public class ContainerTechTable extends Container {
    protected TileEntityTechTable tileEntity;
    private EntityPlayer player;
    private IInventory playerinventory;

    public ContainerTechTable(InventoryPlayer inventoryPlayer, TileEntityTechTable te, int xSize, int ySize) {
        tileEntity = te;
        playerinventory = inventoryPlayer;




        bindSlots(inventoryPlayer, te, xSize, ySize);
    }


    protected void bindSlots(InventoryPlayer inventoryPlayer, TileEntityTechTable te, int xSize, int ySize) {
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; j++) {
                addSlotToContainer(new Slot(te, j + i * 3, (31 + j * 18) - 4, i * 18 + 32));
            }
        }
        addSlotToContainer(new RestrictedSlot(te, 10, getSlotFromInventory(te, 5).xDisplayPosition + 47, getSlotFromInventory(te, 5).yDisplayPosition));
        int leftCol = (xSize - 162) / 2 + 1;
        for (int playerInvRow = 0; playerInvRow < 3; playerInvRow++)
        {
            for (int playerInvCol = 0; playerInvCol < 9; playerInvCol++)
            {
                addSlotToContainer(new Slot(inventoryPlayer, playerInvCol + playerInvRow * 9 + 9, leftCol + playerInvCol * 18, ySize - (4 - playerInvRow) * 18 - 10));
            }

        }

        for (int hotbarSlot = 0; hotbarSlot < 9; hotbarSlot++)
        {
            addSlotToContainer(new Slot(inventoryPlayer, hotbarSlot, leftCol + hotbarSlot * 18, ySize - 24));
        }

    }

    @Override
    public ItemStack transferStackInSlot(EntityPlayer player, int slot) {
        ItemStack itemstack = null;
        Slot slot1 = (Slot) inventorySlots.get(slot);
        if (slot1 != null && slot1.getHasStack())
        {
            ItemStack itemstack1 = slot1.getStack();
            itemstack = itemstack1.copy();
            if (slot <= 9)
            {
                if (!mergeItemStack(itemstack1, inventorySlots.size(), inventorySlots.size(), true))
                {
                    return null;
                }
            }
            else if (!mergeItemStack(itemstack1, 0, inventorySlots.size(), false))
            {
                return null;
            }
            if (itemstack1.stackSize == 0)
            {
                slot1.putStack(null);
            }
            else
            {
                slot1.onSlotChanged();
            }
        }
        return itemstack;
    }

    @Override
    public boolean canInteractWith(EntityPlayer player) {
        return true;
    }
}
