package common.inventory;

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

public class ContainerMachine extends Container
{
    private final IInventory machine;
    private final TileEntityMachine tile;
    private int temperature;
    private Status status = Status.OFF;
    private final int[] resources;

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

        for (int j = 0; j < machine.getSizeInventory(); ++j)
        {
        	final int index = j;
            this.addSlotToContainer(new Slot(machine, j, 8 + j * 18, 40) {
            	public boolean isItemValid(ItemStack stack)
                {
                    return ContainerMachine.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.machine.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.machine.getSizeInventory())
            {
                if (!this.mergeItemStack(itemstack1, this.machine.getSizeInventory(), this.inventorySlots.size(), true))
                {
                    return null;
                }
            }
            else if (!this.mergeItemStack(itemstack1, 0, this.machine.getSizeInventory(), false))
            {
                return null;
            }

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

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

    public void onCraftGuiOpened(ICrafting listener)
    {
        super.onCraftGuiOpened(listener);
//        listener.sendAllWindowProperties(this, this.machine);
    }

    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 icrafting = (ICrafting)this.crafters.get(i);
	        if(this.temperature != this.tile.getTemperature())
	        	icrafting.sendProgressBarUpdate(this, this.resources.length, this.tile.getTemperature());
	        if(this.status != this.tile.getStatus())
	        	icrafting.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())
		        	icrafting.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();
        }
    }
}
