package client.renderer.entity;

import org.lwjgl.opengl.GL11;

import client.Client;
import client.renderer.GlState;
import client.renderer.Renderer;
import client.renderer.texture.TextureMap;
import common.block.Block;
import common.entity.item.EntityTnt;
import common.init.BlockRegistry;
import common.util.ExtMath;


public class RenderTntPrimed extends Render<EntityTnt>
{
    public RenderTntPrimed(RenderManager renderManagerIn)
    {
        super(renderManagerIn);
    }

    public void doRender(EntityTnt entity, double x, double y, double z, float partialTicks)
    {
        Renderer renderer = Client.CLIENT.renderer;
        GL11.glPushMatrix();
        GL11.glTranslatef((float)x, (float)y + 0.5F, (float)z);

        if ((float)entity.fuse - partialTicks + 1.0F < 10.0F)
        {
            float f = 1.0F - ((float)entity.fuse - partialTicks + 1.0F) / 10.0F;
            f = ExtMath.clampf(f, 0.0F, 1.0F);
            f = f * f;
            f = f * f;
            float f1 = 1.0F + f * 0.3F;
            GL11.glScalef(f1, f1, f1);
        }

        float f2 = (1.0F - ((float)entity.fuse - partialTicks + 1.0F) / 100.0F) * 0.8F;
        this.bindEntityTexture(entity);
        GL11.glTranslatef(-0.5F, -0.5F, 0.5F);
        Block tnt = BlockRegistry.byName("tnt" + (entity.explosionSize <= 0 || entity.explosionSize >= 8 ? "" : "_" + entity.explosionSize));
        renderer.renderBlockEntity(tnt.getState(), getBrightness(entity));
        GL11.glTranslatef(0.0F, 0.0F, 1.0F);

        if (entity.fuse / 5 % 2 == 0)
        {
            GlState.disableTexture2D();
            GlState.disableLighting();
            GlState.enableBlend();
            GlState.blendFunc(GL11.GL_SRC_ALPHA, GL11.GL_DST_ALPHA);
            GlState.color(1.0F, 1.0F, 1.0F, f2);
            GlState.doPolygonOffset(-3.0F, -3.0F);
            GlState.enablePolygonOffset();
            renderer.renderBlockEntity(tnt.getState(), 1.0F);
            GlState.doPolygonOffset(0.0F, 0.0F);
            GlState.disablePolygonOffset();
            GlState.color(1.0F, 1.0F, 1.0F, 1.0F);
            GlState.disableBlend();
            GlState.enableLighting();
            GlState.enableTexture2D();
        }

        GL11.glPopMatrix();
        super.doRender(entity, x, y, z, partialTicks);
    }

    protected String getEntityTexture(EntityTnt entity)
    {
        return TextureMap.BLOCKS;
    }
}
