package client.renderer.entity;

import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GL13;

import client.renderer.DefaultVertexFormats;
import client.renderer.GlState;
import client.renderer.RenderBuffer;
import client.renderer.Tessellator;
import common.entity.item.EntityXp;
import common.util.ExtMath;

public class RenderXp extends Render<EntityXp> {
	private static final String TEXTURE = "textures/object/experience.png";

	public RenderXp(RenderManager renderManagerIn) {
		super(renderManagerIn);
	}

	public void doRender(EntityXp entity, double x, double y, double z, float partial) {
		GL11.glPushMatrix();
		GL11.glTranslatef((float)x, (float)y, (float)z);
		this.bindEntityTexture(entity);
		int value = entity.getXpValue();
		float u1 = (float)(value % 4 * 16 + 0) / 64.0F;
		float u2 = (float)(value % 4 * 16 + 16) / 64.0F;
		float v1 = (float)(value / 4 * 16 + 0) / 64.0F;
		float v2 = (float)(value / 4 * 16 + 16) / 64.0F;
		float bx = 1.0F;
		float dx = 0.5F;
		float dy = 0.25F;
		int light = RenderManager.getBrightnessForRender(entity);
		int block = light % 65536;
		int sky = light / 65536;
		GL13.glMultiTexCoord2f(GL13.GL_TEXTURE1, (float)block / 1.0F, (float)sky / 1.0F);
		GlState.color(1.0F, 1.0F, 1.0F, 1.0F);
		float color = ((float)entity.xpColor + partial) / 16.0F;
		int g = (int)((ExtMath.sin(color + 0.0F) + 1.0F) * 0.5F * 255.0F);
		float n = (int)((ExtMath.sin(color + (float)Math.PI) + 1.0F) * 0.5F * 255.0F);
		int r = (int)(n * (value / 10.0f));
		int b = (int)(n * ((10 - value) / 10.0f));
		GL11.glRotatef(180.0F - this.manager.playerViewY, 0.0F, 1.0F, 0.0F);
		GL11.glRotatef(-this.manager.playerViewX, 1.0F, 0.0F, 0.0F);
		float size = 0.25F;
		GL11.glScalef(size, size, size);
		RenderBuffer rb = Tessellator.getBuffer();
		rb.begin(GL11.GL_QUADS, DefaultVertexFormats.POSITION_TEX_COLOR_NORMAL);
		rb.pos((double)(0.0F - dx), (double)(0.0F - dy), 0.0D).tex((double)u1, (double)v2).color(r, g, b, 128).normal(0.0F, 1.0F, 0.0F)
				.endVertex();
		rb.pos((double)(bx - dx), (double)(0.0F - dy), 0.0D).tex((double)u2, (double)v2).color(r, g, b, 128).normal(0.0F, 1.0F, 0.0F)
				.endVertex();
		rb.pos((double)(bx - dx), (double)(1.0F - dy), 0.0D).tex((double)u2, (double)v1).color(r, g, b, 128).normal(0.0F, 1.0F, 0.0F)
				.endVertex();
		rb.pos((double)(0.0F - dx), (double)(1.0F - dy), 0.0D).tex((double)u1, (double)v1).color(r, g, b, 128).normal(0.0F, 1.0F, 0.0F)
				.endVertex();
		Tessellator.draw();
		GlState.disableBlend();
		GlState.disableRescaleNormal();
		GL11.glPopMatrix();
		super.doRender(entity, x, y, z, partial);
	}

	protected String getEntityTexture(EntityXp entity) {
		return TEXTURE;
	}

    protected void renderName(EntityXp entity, double x, double y, double z)
    {
    	String name = entity.getLabel();
    	if(name != null && !name.isEmpty())
    		this.manager.renderLabel(entity, name, x, y - 0.5, z, 64);
    }
}
