package net.minecraft.client.renderer;

import java.nio.ByteBuffer;
import java.util.Iterator;
import java.util.List;
import net.minecraft.client.renderer.vertex.VertexFormat;
import net.minecraft.client.renderer.vertex.VertexFormatElement;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import org.lwjgl.opengl.GL11;

@SideOnly(Side.CLIENT)
public class WorldVertexBufferUploader
{
    private static final String __OBFID = "CL_00002567";

    public int draw(WorldRenderer p_178177_1_, int p_178177_2_)
    {
        if (p_178177_2_ > 0)
        {
            VertexFormat vertexformat = p_178177_1_.func_178973_g();
            int j = vertexformat.func_177338_f();
            ByteBuffer bytebuffer = p_178177_1_.func_178966_f();
            List list = vertexformat.func_177343_g();
            Iterator iterator = list.iterator();
            VertexFormatElement vertexformatelement;
            VertexFormatElement.EnumUsage enumusage;
            int k;

            while (iterator.hasNext())
            {
                vertexformatelement = (VertexFormatElement)iterator.next();
                enumusage = vertexformatelement.func_177375_c();
                k = vertexformatelement.func_177367_b().func_177397_c();
                int l = vertexformatelement.func_177369_e();

                switch (WorldVertexBufferUploader.SwitchEnumUsage.field_178958_a[enumusage.ordinal()])
                {
                    case 1:
                        bytebuffer.position(vertexformatelement.func_177373_a());
                        GL11.glVertexPointer(vertexformatelement.func_177370_d(), k, j, bytebuffer);
                        GL11.glEnableClientState(GL11.GL_VERTEX_ARRAY);
                        break;
                    case 2:
                        bytebuffer.position(vertexformatelement.func_177373_a());
                        OpenGlHelper.setClientActiveTexture(OpenGlHelper.defaultTexUnit + l);
                        GL11.glTexCoordPointer(vertexformatelement.func_177370_d(), k, j, bytebuffer);
                        GL11.glEnableClientState(GL11.GL_TEXTURE_COORD_ARRAY);
                        OpenGlHelper.setClientActiveTexture(OpenGlHelper.defaultTexUnit);
                        break;
                    case 3:
                        bytebuffer.position(vertexformatelement.func_177373_a());
                        GL11.glColorPointer(vertexformatelement.func_177370_d(), k, j, bytebuffer);
                        GL11.glEnableClientState(GL11.GL_COLOR_ARRAY);
                        break;
                    case 4:
                        bytebuffer.position(vertexformatelement.func_177373_a());
                        GL11.glNormalPointer(k, j, bytebuffer);
                        GL11.glEnableClientState(GL11.GL_NORMAL_ARRAY);
                }
            }

            GL11.glDrawArrays(p_178177_1_.getDrawMode(), 0, p_178177_1_.func_178989_h());
            iterator = list.iterator();

            while (iterator.hasNext())
            {
                vertexformatelement = (VertexFormatElement)iterator.next();
                enumusage = vertexformatelement.func_177375_c();
                k = vertexformatelement.func_177369_e();

                switch (WorldVertexBufferUploader.SwitchEnumUsage.field_178958_a[enumusage.ordinal()])
                {
                    case 1:
                        GL11.glDisableClientState(GL11.GL_VERTEX_ARRAY);
                        break;
                    case 2:
                        OpenGlHelper.setClientActiveTexture(OpenGlHelper.defaultTexUnit + k);
                        GL11.glDisableClientState(GL11.GL_TEXTURE_COORD_ARRAY);
                        OpenGlHelper.setClientActiveTexture(OpenGlHelper.defaultTexUnit);
                        break;
                    case 3:
                        GL11.glDisableClientState(GL11.GL_COLOR_ARRAY);
                        GlStateManager.func_179117_G();
                        break;
                    case 4:
                        GL11.glDisableClientState(GL11.GL_NORMAL_ARRAY);
                }
            }
        }

        p_178177_1_.reset();
        return p_178177_2_;
    }

    @SideOnly(Side.CLIENT)

    static final class SwitchEnumUsage
        {
            static final int[] field_178958_a = new int[VertexFormatElement.EnumUsage.values().length];
            private static final String __OBFID = "CL_00002566";

            static
            {
                try
                {
                    field_178958_a[VertexFormatElement.EnumUsage.POSITION.ordinal()] = 1;
                }
                catch (NoSuchFieldError var4)
                {
                    ;
                }

                try
                {
                    field_178958_a[VertexFormatElement.EnumUsage.UV.ordinal()] = 2;
                }
                catch (NoSuchFieldError var3)
                {
                    ;
                }

                try
                {
                    field_178958_a[VertexFormatElement.EnumUsage.COLOR.ordinal()] = 3;
                }
                catch (NoSuchFieldError var2)
                {
                    ;
                }

                try
                {
                    field_178958_a[VertexFormatElement.EnumUsage.NORMAL.ordinal()] = 4;
                }
                catch (NoSuchFieldError var1)
                {
                    ;
                }
            }
        }
}