package client.renderer.entity;

import org.lwjgl.opengl.GL11;

import client.Client;
import client.renderer.DefaultVertexFormats;
import client.renderer.GlState;
import client.renderer.RenderBuffer;
import client.renderer.Renderer;
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.init.Blocks;
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.BLOCKS);
            State state = entity.getBlock();
            Block block = state.getBlock();
            BlockPos pos = new BlockPos(entity);
            World world = entity.getWorldObj();

            if (state != world.getState(pos) && block != Blocks.air && !block.getMaterial().isLiquid())
            {
                GL11.glPushMatrix();
                GL11.glTranslatef((float)x, (float)y, (float)z);
                GlState.disableLighting();
//                    Tessellator tessellator = Tessellator.getInstance();
                RenderBuffer rb = Tessellator.getBuffer();
                rb.begin(GL11.GL_QUADS, DefaultVertexFormats.BLOCK);
                int i = pos.getX();
                int j = pos.getY();
                int k = pos.getZ();
                rb.setTranslation((double)((float)(-i) - 0.5F), (double)(-j), (double)((float)(-k) - 0.5F));
                Renderer renderer = Client.CLIENT.renderer;
                IBakedModel model = renderer.getBlockModel(state, world, null);
                renderer.renderModel(world, model, state, pos, rb, false);
                rb.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.BLOCKS;
    }
}
