package common.inventory;

import common.entity.npc.EntityNPC;
import common.item.ItemStack;
import common.tileentity.TileEntityDevice;
import common.tileentity.TileEntityDevice.Status;

public class ContainerTile extends Container
{
    private final IInventory tileInv;
    private final TileEntityDevice tile;
    private int temperature;
    private Status status = Status.OFF;
    private final int[] resources;

    public ContainerTile(InventoryPlayer playerInv, TileEntityDevice tile, IInventory tileInv, EntityNPC player)
    {
        this.tileInv = tileInv;
        this.tile = tile;
        this.resources = new int[tile.getNumResources()];
        tileInv.openInventory(player);
        int i = 71;

        for (int j = 0; j < tileInv.getSizeInventory(); ++j)
        {
        	final int index = j;
            this.addSlotToContainer(new Slot(tileInv, j, 8 + j * 18, 40) {
            	public boolean isItemValid(ItemStack stack)
                {
                    return ContainerTile.this.tile.isItemValidForSlot(index, stack);
                }
            });
        }

        for (int l = 0; l < 3; ++l)
        {
            for (int k = 0; k < 9; ++k)
            {
                this.addSlotToContainer(new Slot(playerInv, k + l * 9 + 9, 8 + k * 18, l * 18 + i));
            }
        }

        for (int i1 = 0; i1 < 9; ++i1)
        {
            this.addSlotToContainer(new Slot(playerInv, i1, 8 + i1 * 18, 58 + i));
        }
    }

    public boolean canInteractWith(EntityNPC playerIn)
    {
        return this.tileInv.isUseableByPlayer(playerIn);
    }
    
    protected boolean canTransfer(Slot slot, ItemStack stack) {
    	return slot.isItemValid(stack);
    }
    
    public ItemStack transferStackInSlot(EntityNPC playerIn, int index)
    {
        ItemStack itemstack = null;
        Slot slot = (Slot)this.inventorySlots.get(index);

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

            if (index < this.tileInv.getSizeInventory())
            {
                if (!this.mergeItemStack(itemstack1, this.tileInv.getSizeInventory(), this.inventorySlots.size(), true))
                {
                    return null;
                }
            }
            else if (!this.mergeItemStack(itemstack1, 0, this.tileInv.getSizeInventory(), false))
            {
                return null;
            }

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

        return itemstack;
    }
    
    public void onContainerClosed(EntityNPC playerIn)
    {
        super.onContainerClosed(playerIn);
        this.tileInv.closeInventory(playerIn);
    }

    public void onCraftGuiOpened(ICrafting listener)
    {
        super.onCraftGuiOpened(listener);
        listener.sendProgressBarUpdate(this, this.resources.length, this.tile.getTemperature());
        listener.sendProgressBarUpdate(this, this.resources.length + 1, this.tile.getStatus().ordinal());
        for(int z = 0; z < this.resources.length; z++) {
	        listener.sendProgressBarUpdate(this, z, this.tile.getResource(z).getValue());
        }
    }

    public void updateProgressBar(int id, int data)
    {
    	if(id == this.resources.length)
			this.tile.setTemperature(data);
    	else if(id == this.resources.length + 1)
			this.tile.setStatus(Status.values()[data % Status.values().length]);
		else
			this.tile.getResource(id).setValue(data);
    }
    
    public void detectAndSendChanges()
    {
        super.detectAndSendChanges();

        for (int i = 0; i < this.crafters.size(); ++i)
        {
            ICrafting listener = this.crafters.get(i);
	        if(this.temperature != this.tile.getTemperature())
	        	listener.sendProgressBarUpdate(this, this.resources.length, this.tile.getTemperature());
	        if(this.status != this.tile.getStatus())
	        	listener.sendProgressBarUpdate(this, this.resources.length + 1, this.tile.getStatus().ordinal());
	        for(int z = 0; z < this.resources.length; z++) {
		        if(this.resources[z] != this.tile.getResource(z).getValue())
		        	listener.sendProgressBarUpdate(this, z, this.tile.getResource(z).getValue());
	        }
        }
        
        this.temperature = this.tile.getTemperature();
        this.status = this.tile.getStatus();
        for(int z = 0; z < this.resources.length; z++) {
        	this.resources[z] = this.tile.getResource(z).getValue();
        }
    }
}
