package client.renderer.entity;

import org.lwjgl.opengl.GL11;

import client.Client;
import client.renderer.GlState;
import client.renderer.model.ModelBase;
import client.renderer.model.ModelMinecart;
import client.renderer.texture.TextureMap;
import common.entity.item.EntityCart;
import common.init.Blocks;
import common.util.ExtMath;
import common.util.Vec3;
import common.world.State;

public class RenderMinecart<T extends EntityCart> extends Render<T>
{
    private static final String minecartTextures = "textures/object/minecart.png";

    /** instance of ModelMinecart for rendering */
    protected ModelBase modelMinecart = new ModelMinecart();

    public RenderMinecart(RenderManager renderManagerIn)
    {
        super(renderManagerIn);
//        this.shadowSize = 0.5F;
    }

    /**
     * Renders the desired {@code T} type Entity.
     */
    public void doRender(T entity, double x, double y, double z, float partialTicks)
    {
    	float entityYaw = entity.prevYaw + (entity.rotYaw - entity.prevYaw) * partialTicks;
        GL11.glPushMatrix();
        this.bindEntityTexture(entity);
        long i = (long)entity.getId() * 493286711L;
        i = i * i * 4392167121L + i * 98761L;
        float f = (((float)(i >> 16 & 7L) + 0.5F) / 8.0F - 0.5F) * 0.004F;
        float f1 = (((float)(i >> 20 & 7L) + 0.5F) / 8.0F - 0.5F) * 0.004F;
        float f2 = (((float)(i >> 24 & 7L) + 0.5F) / 8.0F - 0.5F) * 0.004F;
        GL11.glTranslatef(f, f1, f2);
        double d0 = entity.lastTickPosX + (entity.posX - entity.lastTickPosX) * (double)partialTicks;
        double d1 = entity.lastTickPosY + (entity.posY - entity.lastTickPosY) * (double)partialTicks;
        double d2 = entity.lastTickPosZ + (entity.posZ - entity.lastTickPosZ) * (double)partialTicks;
        double d3 = 0.30000001192092896D;
        Vec3 vec3 = entity.func_70489_a(d0, d1, d2);
        float f3 = entity.prevPitch + (entity.rotPitch - entity.prevPitch) * partialTicks;

        if (vec3 != null)
        {
            Vec3 vec31 = entity.func_70495_a(d0, d1, d2, d3);
            Vec3 vec32 = entity.func_70495_a(d0, d1, d2, -d3);

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

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

            x += vec3.xCoord - d0;
            y += (vec31.yCoord + vec32.yCoord) / 2.0D - d1;
            z += vec3.zCoord - d2;
            Vec3 vec33 = vec32.addVector(-vec31.xCoord, -vec31.yCoord, -vec31.zCoord);

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

        GL11.glTranslatef((float)x, (float)y + 0.375F, (float)z);
        GL11.glRotatef(180.0F - entityYaw, 0.0F, 1.0F, 0.0F);
        GL11.glRotatef(-f3, 0.0F, 0.0F, 1.0F);
        float f5 = (float)entity.getRollingAmplitude() - partialTicks;
        float f6 = (float)entity.getDamage() - partialTicks;

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

        if (f5 > 0.0F)
        {
            GL11.glRotatef(ExtMath.sin(f5) * f5 * f6 / 10.0F * (float)entity.getRollingDirection(), 1.0F, 0.0F, 0.0F);
        }

        int j = entity.getDisplayTileOffset();
        State iblockstate = entity.getDisplayTile();

        if (iblockstate.getBlock() != Blocks.air && !iblockstate.getBlock().getMaterial().isLiquid())
        {
            GL11.glPushMatrix();
            this.bindTexture(TextureMap.BLOCKS);
            float f4 = 0.75F;
            GL11.glScalef(f4, f4, f4);
            GL11.glTranslatef(-0.5F, (float)(j - 8) / 16.0F, 0.5F);
            this.func_180560_a(entity, partialTicks, iblockstate);
            GL11.glPopMatrix();
            GlState.color(1.0F, 1.0F, 1.0F, 1.0F);
            this.bindEntityTexture(entity);
        }

        GL11.glScalef(-1.0F, -1.0F, 1.0F);
        this.modelMinecart.render(entity, 0.0F, 0.0F, -0.1F, 0.0F, 0.0F, 0.0625F);
        GL11.glPopMatrix();
        super.doRender(entity, x, y, z, partialTicks);
    }

    /**
     * Returns the location of an entity's texture. Doesn't seem to be called unless you call Render.bindEntityTexture.
     */
    protected String getEntityTexture(T entity)
    {
        return minecartTextures;
    }

    protected void func_180560_a(T minecart, float partialTicks, State state)
    {
        GL11.glPushMatrix();
        Client.CLIENT.renderer.renderBlockEntity(state, getBrightness(minecart));
        GL11.glPopMatrix();
    }
}
