package client.renderer;

import java.nio.ByteBuffer;
import java.util.List;

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

public abstract class Tessellator
{
    private static final RenderBuffer BUFFER = new RenderBuffer(2097152);

    public static RenderBuffer getBuffer()
    {
        return BUFFER;
    }
    
    public static void draw()
    {
        BUFFER.finishDrawing();
        if (BUFFER.getVertexCount() > 0)
        {
            VertexFormat vertexformat = BUFFER.getVertexFormat();
            int i = vertexformat.getNextOffset();
            ByteBuffer bytebuffer = BUFFER.getByteBuffer();
            List<VertexFormatElement> list = vertexformat.getElements();

            for (int j = 0; j < list.size(); ++j)
            {
                VertexFormatElement vertexformatelement = (VertexFormatElement)list.get(j);
                VertexFormatElement.EnumUsage vertexformatelement$enumusage = vertexformatelement.usage();
                int k = vertexformatelement.type().getGlConstant();
                int l = vertexformatelement.index();
                bytebuffer.position(vertexformat.getOffset(j));

                switch (vertexformatelement$enumusage)
                {
                    case POSITION:
					GL11.glVertexPointer(vertexformatelement.count(), k, i, bytebuffer);
					GL11.glEnableClientState(GL11.GL_VERTEX_ARRAY);
                        break;

                    case UV:
					GL13.glClientActiveTexture(GL13.GL_TEXTURE0 + l);
					GL11.glTexCoordPointer(vertexformatelement.count(), k, i, bytebuffer);
					GL11.glEnableClientState(GL11.GL_TEXTURE_COORD_ARRAY);
					GL13.glClientActiveTexture(GL13.GL_TEXTURE0);
                        break;

                    case COLOR:
					GL11.glColorPointer(vertexformatelement.count(), k, i, bytebuffer);
					GL11.glEnableClientState(GL11.GL_COLOR_ARRAY);
                        break;

                    case NORMAL:
					GL11.glNormalPointer(k, i, bytebuffer);
					GL11.glEnableClientState(GL11.GL_NORMAL_ARRAY);
                }
            }

            GL11.glDrawArrays(BUFFER.getDrawMode(), 0, BUFFER.getVertexCount());
            int i1 = 0;

            for (int j1 = list.size(); i1 < j1; ++i1)
            {
                VertexFormatElement vertexformatelement1 = (VertexFormatElement)list.get(i1);
                VertexFormatElement.EnumUsage vertexformatelement$enumusage1 = vertexformatelement1.usage();
                int k1 = vertexformatelement1.index();

                switch (vertexformatelement$enumusage1)
                {
                    case POSITION:
					GL11.glDisableClientState(GL11.GL_VERTEX_ARRAY);
                        break;

                    case UV:
					GL13.glClientActiveTexture(GL13.GL_TEXTURE0 + k1);
					GL11.glDisableClientState(GL11.GL_TEXTURE_COORD_ARRAY);
					GL13.glClientActiveTexture(GL13.GL_TEXTURE0);
                        break;

                    case COLOR:
					GL11.glDisableClientState(GL11.GL_COLOR_ARRAY);
                        GlState.resetColor();
                        break;

                    case NORMAL:
					GL11.glDisableClientState(GL11.GL_NORMAL_ARRAY);
                }
            }
        }
        BUFFER.reset();
    }
}
