package com.hbm.inventory.container;

import com.hbm.inventory.SlotMachineOutput;
import com.hbm.tileentity.machine.TileEntityCoreTitanium;

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

public class ContainerCoreTitanium extends Container {
	
	private TileEntityCoreTitanium diFurnace;
	private int progress;
	private int power;
	
	public ContainerCoreTitanium(InventoryPlayer invPlayer, TileEntityCoreTitanium tedf) {
		
		diFurnace = tedf;
		
		//Input Storage
		this.addSlotToContainer(new Slot(tedf, 0, 8, 18));
		this.addSlotToContainer(new Slot(tedf, 1, 26, 18));
		this.addSlotToContainer(new Slot(tedf, 2, 44, 18));
		this.addSlotToContainer(new Slot(tedf, 3, 62, 18));
		this.addSlotToContainer(new Slot(tedf, 4, 80, 18));
		this.addSlotToContainer(new Slot(tedf, 5, 98, 18));
		this.addSlotToContainer(new Slot(tedf, 6, 116, 18));
		this.addSlotToContainer(new Slot(tedf, 7, 134, 18));
		this.addSlotToContainer(new Slot(tedf, 8, 152, 18));
		//Inputs
		this.addSlotToContainer(new Slot(tedf, 9, 8, 54));
		this.addSlotToContainer(new Slot(tedf, 10, 8, 72));
		//Outputs
		this.addSlotToContainer(new SlotMachineOutput(tedf, 11, 152, 54));
		this.addSlotToContainer(new SlotMachineOutput(tedf, 12, 152, 72));
		//Output Storage
		this.addSlotToContainer(new SlotMachineOutput(tedf, 13, 8, 108));
		this.addSlotToContainer(new SlotMachineOutput(tedf, 14, 26, 108));
		this.addSlotToContainer(new SlotMachineOutput(tedf, 15, 44, 108));
		this.addSlotToContainer(new SlotMachineOutput(tedf, 16, 62, 108));
		this.addSlotToContainer(new SlotMachineOutput(tedf, 17, 80, 108));
		this.addSlotToContainer(new SlotMachineOutput(tedf, 18, 98, 108));
		this.addSlotToContainer(new SlotMachineOutput(tedf, 19, 116, 108));
		this.addSlotToContainer(new SlotMachineOutput(tedf, 20, 134, 108));
		this.addSlotToContainer(new SlotMachineOutput(tedf, 21, 152, 108));
		//Power Cell
		this.addSlotToContainer(new Slot(tedf, 22, 44, 72));
		
		for(int i = 0; i < 3; i++)
		{
			for(int j = 0; j < 9; j++)
			{
				this.addSlotToContainer(new Slot(invPlayer, j + i * 9 + 9, 8 + j * 18, 84 + i * 18 + 56));
			}
		}
		
		for(int i = 0; i < 9; i++)
		{
			this.addSlotToContainer(new Slot(invPlayer, i, 8 + i * 18, 142 + 56));
		}
	}
	
	@Override
	public void addCraftingToCrafters(ICrafting crafting) {
		super.addCraftingToCrafters(crafting);
		crafting.sendProgressBarUpdate(this, 0, this.diFurnace.progress);
		crafting.sendProgressBarUpdate(this, 1, (int)this.diFurnace.power);
	}
	
	@Override
    public ItemStack transferStackInSlot(EntityPlayer p_82846_1_, int par2)
    {
		ItemStack var3 = null;
		Slot var4 = (Slot) this.inventorySlots.get(par2);
		
		if (var4 != null && var4.getHasStack())
		{
			ItemStack var5 = var4.getStack();
			var3 = var5.copy();
			
            if (par2 <= 22) {
				if (!this.mergeItemStack(var5, 23, this.inventorySlots.size(), true))
				{
					return null;
				}
			}
			else if (!this.mergeItemStack(var5, 22, 23, false))
			{
				if (!this.mergeItemStack(var5, 9, 11, false))
					if (!this.mergeItemStack(var5, 0, 9, false))
						return null;
			}
			
			if (var5.stackSize == 0)
			{
				var4.putStack((ItemStack) null);
			}
			else
			{
				var4.onSlotChanged();
			}
		}
		
		return var3;
    }

	@Override
	public boolean canInteractWith(EntityPlayer player) {
		return diFurnace.isUseableByPlayer(player);
	}
	
	@Override
	public void detectAndSendChanges() {
		super.detectAndSendChanges();
		
		for(int i = 0; i < this.crafters.size(); i++)
		{
			ICrafting par1 = (ICrafting)this.crafters.get(i);
			
			if(this.progress != this.diFurnace.progress)
			{
				par1.sendProgressBarUpdate(this, 0, this.diFurnace.progress);
			}
			
			if(this.power != this.diFurnace.power)
			{
				par1.sendProgressBarUpdate(this, 1, (int)this.diFurnace.power);
			}
		}
		
		this.progress = this.diFurnace.progress;
		this.power = (int)this.diFurnace.power;
	}
	
	@Override
	public void updateProgressBar(int i, int j) {
		if(i == 0)
		{
			diFurnace.progress = j;
		}
		if(i == 1)
		{
			diFurnace.power = j;
		}
	}

}
