package com.pharmondev.oregen.tileentity.renderer;

import com.pharmondev.oregen.OreGenMod;
import com.pharmondev.oregen.blocks.OreGenBlock;
import com.pharmondev.oregen.container.ContainerOreGen;
import com.pharmondev.oregen.tileentity.TileEntityOreGen;

import net.minecraft.block.state.IBlockState;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.GlStateManager;
import net.minecraft.client.renderer.tileentity.TileEntitySpecialRenderer;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.util.EnumFacing;

public class RenderOreGen extends TileEntitySpecialRenderer<TileEntityOreGen>{
	
	@Override
	public void renderTileEntityAt(TileEntityOreGen te, double x, double y, double z, float partialTicks, int destroyStage) {
		super.renderTileEntityAt(te, x, y, z, partialTicks, destroyStage);
		//ITEM.hoverStart= 0f;
		
		
		IBlockState currentState = Minecraft.getMinecraft().world.getBlockState(te.getPos()); // Updates our current state variable
		EnumFacing facing = (EnumFacing) currentState.getValue(OreGenBlock.FACING); // Gets which way our block is facing
		
		ItemStack itemstack = te.getHandler().getStackInSlot(ContainerOreGen.INPUT_1);
		Item itemInOreGen = itemstack.getItem(); 
		EntityItem itemInBlockToRender = new EntityItem(Minecraft.getMinecraft().world, 0, 0, 0, new ItemStack(itemInOreGen));
		itemInBlockToRender.hoverStart = 0f;
		if(facing.equals(EnumFacing.EAST)){
			
			GlStateManager.pushMatrix();
			{
				GlStateManager.translate(x, y, z);
				GlStateManager.translate(0.875, 0.15, 0.5);
				GlStateManager.scale(1, 1, 1);
				Minecraft.getMinecraft().getRenderManager().doRenderEntity(itemInBlockToRender,  0, 0, 0, 0f, 0f, false);

			}
			GlStateManager.popMatrix();
		}
		else if(facing.equals(EnumFacing.WEST)){
			
			GlStateManager.pushMatrix();
			{
				GlStateManager.translate(x, y, z);
				GlStateManager.translate(1-0.875, 0.15, 0.5);
				GlStateManager.scale(1, 1, 1);
				Minecraft.getMinecraft().getRenderManager().doRenderEntity(itemInBlockToRender,  0, 0, 0, 0f, 0f, false);

			}
			GlStateManager.popMatrix();
		}
		else if(facing.equals(EnumFacing.NORTH)){
			
			GlStateManager.pushMatrix();
			{
				GlStateManager.translate(x, y, z);
				GlStateManager.translate(0.5, 0.15, 1-0.875);
				GlStateManager.scale(1, 1, 1);
				Minecraft.getMinecraft().getRenderManager().doRenderEntity(itemInBlockToRender,  0, 0, 0, 0f, 0f, false);

			}
			GlStateManager.popMatrix();
		}
		else if(facing.equals(EnumFacing.SOUTH)){
			
			GlStateManager.pushMatrix();
			{
				GlStateManager.translate(x, y, z);
				GlStateManager.translate(0.5, 0.15, 0.875);
				GlStateManager.scale(1, 1, 1);
				Minecraft.getMinecraft().getRenderManager().doRenderEntity(itemInBlockToRender,  0, 0, 0, 0f, 0f, false);

			}
			GlStateManager.popMatrix();
		}
		
		
		
		
		
		
	}
}
