package client.renderer.entity;

import org.lwjgl.opengl.GL11;

import client.Client;
import client.renderer.BlockRenderer;
import client.renderer.DefaultVertexFormats;
import client.renderer.GlState;
import client.renderer.RenderBuffer;
import client.renderer.Tessellator;
import client.renderer.blockmodel.IBakedModel;
import client.renderer.texture.TextureMap;
import common.block.Block;
import common.entity.item.EntityFalling;
import common.util.BlockPos;
import common.world.State;
import common.world.World;

public class RenderFallingBlock extends Render<EntityFalling>
{
    public RenderFallingBlock(RenderManager renderManagerIn)
    {
        super(renderManagerIn);
//        this.shadowSize = 0.5F;
    }

    /**
     * Renders the desired {@code T} type Entity.
     */
    public void doRender(EntityFalling entity, double x, double y, double z, float partialTicks)
    {
        if (entity.getBlock() != null)
        {
            this.bindTexture(TextureMap.locationBlocksTexture);
            State iblockstate = entity.getBlock();
            Block block = iblockstate.getBlock();
            BlockPos blockpos = new BlockPos(entity);
            World world = entity.getWorldObj();

            if (iblockstate != world.getState(blockpos) && block.getRenderType() != -1)
            {
                if (block.getRenderType() == 3)
                {
                    GL11.glPushMatrix();
                    GL11.glTranslatef((float)x, (float)y, (float)z);
                    GlState.disableLighting();
//                    Tessellator tessellator = Tessellator.getInstance();
                    RenderBuffer worldrenderer = Tessellator.getBuffer();
                    worldrenderer.begin(GL11.GL_QUADS, DefaultVertexFormats.BLOCK);
                    int i = blockpos.getX();
                    int j = blockpos.getY();
                    int k = blockpos.getZ();
                    worldrenderer.setTranslation((double)((float)(-i) - 0.5F), (double)(-j), (double)((float)(-k) - 0.5F));
                    BlockRenderer blockrendererdispatcher = Client.CLIENT.getBlockRendererDispatcher();
                    IBakedModel ibakedmodel = blockrendererdispatcher.getModelFromBlockState(iblockstate, world, (BlockPos)null);
                    blockrendererdispatcher.renderModel(world, ibakedmodel, iblockstate, blockpos, worldrenderer, false);
                    worldrenderer.setTranslation(0.0D, 0.0D, 0.0D);
                    Tessellator.draw();
                    GlState.enableLighting();
                    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(EntityFalling entity)
    {
        return TextureMap.locationBlocksTexture;
    }
}
