package net.minecraft.client.renderer.entity;

import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import net.minecraft.block.Block;
import net.minecraft.client.model.ModelBase;
import net.minecraft.client.model.ModelMinecart;
import net.minecraft.client.renderer.RenderBlocks;
import net.minecraft.client.renderer.texture.TextureMap;
import net.minecraft.entity.Entity;
import net.minecraft.entity.item.EntityMinecart;
import net.minecraft.util.MathHelper;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.Vec3;
import org.lwjgl.opengl.GL11;

@SideOnly(Side.CLIENT)
public class RenderMinecart extends Render
{
	private static final ResourceLocation minecartTextures = new ResourceLocation("textures/entity/minecart.png");
	protected ModelBase modelMinecart = new ModelMinecart();
	protected final RenderBlocks field_94145_f;
	private static final String __OBFID = "CL_00001013";

	public RenderMinecart()
	{
		this.shadowSize = 0.5F;
		this.field_94145_f = new RenderBlocks();
	}

	public void doRender(EntityMinecart par1EntityMinecart, double par2, double par4, double par6, float par8, float par9)
	{
		GL11.glPushMatrix();
		this.bindEntityTexture(par1EntityMinecart);
		long i = (long)par1EntityMinecart.getEntityId() * 493286711L;
		i = i * i * 4392167121L + i * 98761L;
		float f2 = (((float)(i >> 16 & 7L) + 0.5F) / 8.0F - 0.5F) * 0.004F;
		float f3 = (((float)(i >> 20 & 7L) + 0.5F) / 8.0F - 0.5F) * 0.004F;
		float f4 = (((float)(i >> 24 & 7L) + 0.5F) / 8.0F - 0.5F) * 0.004F;
		GL11.glTranslatef(f2, f3, f4);
		double d3 = par1EntityMinecart.lastTickPosX + (par1EntityMinecart.posX - par1EntityMinecart.lastTickPosX) * (double)par9;
		double d4 = par1EntityMinecart.lastTickPosY + (par1EntityMinecart.posY - par1EntityMinecart.lastTickPosY) * (double)par9;
		double d5 = par1EntityMinecart.lastTickPosZ + (par1EntityMinecart.posZ - par1EntityMinecart.lastTickPosZ) * (double)par9;
		double d6 = 0.30000001192092896D;
		Vec3 vec3 = par1EntityMinecart.func_70489_a(d3, d4, d5);
		float f5 = par1EntityMinecart.prevRotationPitch + (par1EntityMinecart.rotationPitch - par1EntityMinecart.prevRotationPitch) * par9;

		if (vec3 != null)
		{
			Vec3 vec31 = par1EntityMinecart.func_70495_a(d3, d4, d5, d6);
			Vec3 vec32 = par1EntityMinecart.func_70495_a(d3, d4, d5, -d6);

			if (vec31 == null)
			{
				vec31 = vec3;
			}

			if (vec32 == null)
			{
				vec32 = vec3;
			}

			par2 += vec3.xCoord - d3;
			par4 += (vec31.yCoord + vec32.yCoord) / 2.0D - d4;
			par6 += vec3.zCoord - d5;
			Vec3 vec33 = vec32.addVector(-vec31.xCoord, -vec31.yCoord, -vec31.zCoord);

			if (vec33.lengthVector() != 0.0D)
			{
				vec33 = vec33.normalize();
				par8 = (float)(Math.atan2(vec33.zCoord, vec33.xCoord) * 180.0D / Math.PI);
				f5 = (float)(Math.atan(vec33.yCoord) * 73.0D);
			}
		}

		GL11.glTranslatef((float)par2, (float)par4, (float)par6);
		GL11.glRotatef(180.0F - par8, 0.0F, 1.0F, 0.0F);
		GL11.glRotatef(-f5, 0.0F, 0.0F, 1.0F);
		float f7 = (float)par1EntityMinecart.getRollingAmplitude() - par9;
		float f8 = par1EntityMinecart.getDamage() - par9;

		if (f8 < 0.0F)
		{
			f8 = 0.0F;
		}

		if (f7 > 0.0F)
		{
			GL11.glRotatef(MathHelper.sin(f7) * f7 * f8 / 10.0F * (float)par1EntityMinecart.getRollingDirection(), 1.0F, 0.0F, 0.0F);
		}

		int k = par1EntityMinecart.getDisplayTileOffset();
		Block block = par1EntityMinecart.func_145820_n();
		int j = par1EntityMinecart.getDisplayTileData();

		if (block.getRenderType() != -1)
		{
			GL11.glPushMatrix();
			this.bindTexture(TextureMap.locationBlocksTexture);
			float f6 = 0.75F;
			GL11.glScalef(f6, f6, f6);
			GL11.glTranslatef(0.0F, (float)k / 16.0F, 0.0F);
			this.func_147910_a(par1EntityMinecart, par9, block, j);
			GL11.glPopMatrix();
			GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F);
			this.bindEntityTexture(par1EntityMinecart);
		}

		GL11.glScalef(-1.0F, -1.0F, 1.0F);
		this.modelMinecart.render(par1EntityMinecart, 0.0F, 0.0F, -0.1F, 0.0F, 0.0F, 0.0625F);
		GL11.glPopMatrix();
	}

	protected ResourceLocation getEntityTexture(EntityMinecart par1EntityMinecart)
	{
		return minecartTextures;
	}

	protected void func_147910_a(EntityMinecart p_147910_1_, float p_147910_2_, Block p_147910_3_, int p_147910_4_)
	{
		float f1 = p_147910_1_.getBrightness(p_147910_2_);
		GL11.glPushMatrix();
		this.field_94145_f.renderBlockAsItem(p_147910_3_, p_147910_4_, f1);
		GL11.glPopMatrix();
	}

	protected ResourceLocation getEntityTexture(Entity par1Entity)
	{
		return this.getEntityTexture((EntityMinecart)par1Entity);
	}

	public void doRender(Entity par1Entity, double par2, double par4, double par6, float par8, float par9)
	{
		this.doRender((EntityMinecart)par1Entity, par2, par4, par6, par8, par9);
	}
}