/**
 * GLAD - OpenGL Loader Generator
 * Generated for OpenGL 3.3 Core Profile
 * 
 * This file implements the OpenGL function loader for the acrylic effects system.
 * It loads function pointers at runtime, making the binary self-contained.
 */

#include <glad/gl.h>
#include <stdio.h>

/* Feature flags */
int GLAD_GL_VERSION_1_0 = 0;
int GLAD_GL_VERSION_1_1 = 0;
int GLAD_GL_VERSION_1_2 = 0;
int GLAD_GL_VERSION_1_3 = 0;
int GLAD_GL_VERSION_1_4 = 0;
int GLAD_GL_VERSION_1_5 = 0;
int GLAD_GL_VERSION_2_0 = 0;
int GLAD_GL_VERSION_2_1 = 0;
int GLAD_GL_VERSION_3_0 = 0;
int GLAD_GL_VERSION_3_1 = 0;
int GLAD_GL_VERSION_3_2 = 0;
int GLAD_GL_VERSION_3_3 = 0;

/* Function pointers - Core 1.0 */
PFNGLCLEARPROC glad_glClear = NULL;
PFNGLCLEARCOLORPROC glad_glClearColor = NULL;
PFNGLENABLEPROC glad_glEnable = NULL;
PFNGLDISABLEPROC glad_glDisable = NULL;
PFNGLVIEWPORTPROC glad_glViewport = NULL;
PFNGLTEXIMAGE2DPROC glad_glTexImage2D = NULL;
PFNGLTEXPARAMETERIPROC glad_glTexParameteri = NULL;
PFNGLBINDTEXTUREPROC glad_glBindTexture = NULL;
PFNGLDELETETEXTURESPROC glad_glDeleteTextures = NULL;
PFNGLGENTEXTURESPROC glad_glGenTextures = NULL;
PFNGLDRAWARRAYSPROC glad_glDrawArrays = NULL;
PFNGLGETSTRINGPROC glad_glGetString = NULL;
PFNGLGETINTEGERVPROC glad_glGetIntegerv = NULL;
PFNGLBLENDFUNCPROC glad_glBlendFunc = NULL;

/* Function pointers - Core 1.3 */
PFNGLACTIVETEXTUREPROC glad_glActiveTexture = NULL;

/* Function pointers - Core 1.5 */
PFNGLGENBUFFERSPROC glad_glGenBuffers = NULL;
PFNGLDELETEBUFFERSPROC glad_glDeleteBuffers = NULL;
PFNGLBINDBUFFERPROC glad_glBindBuffer = NULL;
PFNGLBUFFERDATAPROC glad_glBufferData = NULL;

/* Function pointers - Core 2.0 */
PFNGLCREATESHADERPROC glad_glCreateShader = NULL;
PFNGLDELETESHADERPROC glad_glDeleteShader = NULL;
PFNGLSHADERSOURCEPROC glad_glShaderSource = NULL;
PFNGLCOMPILESHADERPROC glad_glCompileShader = NULL;
PFNGLGETSHADERIVPROC glad_glGetShaderiv = NULL;
PFNGLGETSHADERINFOLOGPROC glad_glGetShaderInfoLog = NULL;
PFNGLCREATEPROGRAMPROC glad_glCreateProgram = NULL;
PFNGLDELETEPROGRAMPROC glad_glDeleteProgram = NULL;
PFNGLATTACHSHADERPROC glad_glAttachShader = NULL;
PFNGLLINKPROGRAMPROC glad_glLinkProgram = NULL;
PFNGLGETPROGRAMIVPROC glad_glGetProgramiv = NULL;
PFNGLGETPROGRAMINFOLOGPROC glad_glGetProgramInfoLog = NULL;
PFNGLUSEPROGRAMPROC glad_glUseProgram = NULL;
PFNGLGETUNIFORMLOCATIONPROC glad_glGetUniformLocation = NULL;
PFNGLUNIFORM1IPROC glad_glUniform1i = NULL;
PFNGLUNIFORM1FPROC glad_glUniform1f = NULL;
PFNGLUNIFORM2FPROC glad_glUniform2f = NULL;
PFNGLUNIFORM4FPROC glad_glUniform4f = NULL;
PFNGLUNIFORMMATRIX4FVPROC glad_glUniformMatrix4fv = NULL;
PFNGLBINDATTRIBLOCATIONPROC glad_glBindAttribLocation = NULL;
PFNGLDETACHSHADERPROC glad_glDetachShader = NULL;
PFNGLVERTEXATTRIBPOINTERPROC glad_glVertexAttribPointer = NULL;
PFNGLENABLEVERTEXATTRIBARRAYPROC glad_glEnableVertexAttribArray = NULL;
PFNGLDISABLEVERTEXATTRIBARRAYPROC glad_glDisableVertexAttribArray = NULL;

/* Function pointers - Core 3.0 Framebuffer */
PFNGLGENFRAMEBUFFERSPROC glad_glGenFramebuffers = NULL;
PFNGLDELETEFRAMEBUFFERSPROC glad_glDeleteFramebuffers = NULL;
PFNGLBINDFRAMEBUFFERPROC glad_glBindFramebuffer = NULL;
PFNGLFRAMEBUFFERTEXTURE2DPROC glad_glFramebufferTexture2D = NULL;
PFNGLCHECKFRAMEBUFFERSTATUSPROC glad_glCheckFramebufferStatus = NULL;
PFNGLGENRENDERBUFFERSPROC glad_glGenRenderbuffers = NULL;
PFNGLDELETERENDERBUFFERSPROC glad_glDeleteRenderbuffers = NULL;
PFNGLBINDRENDERBUFFERPROC glad_glBindRenderbuffer = NULL;
PFNGLRENDERBUFFERSTORAGEPROC glad_glRenderbufferStorage = NULL;
PFNGLFRAMEBUFFERRENDERBUFFERPROC glad_glFramebufferRenderbuffer = NULL;
PFNGLBLITFRAMEBUFFERPROC glad_glBlitFramebuffer = NULL;

/* Function pointers - Core 3.0 Mipmap */
PFNGLGENERATEMIPMAPPROC glad_glGenerateMipmap = NULL;

/* Function pointers - Core 3.0 VAO */
PFNGLGENVERTEXARRAYSPROC glad_glGenVertexArrays = NULL;
PFNGLDELETEVERTEXARRAYSPROC glad_glDeleteVertexArrays = NULL;
PFNGLBINDVERTEXARRAYPROC glad_glBindVertexArray = NULL;

/* Parse GL version string to extract major.minor */
static void glad_parse_version(const char* version, int* major, int* minor) {
    const char* p = version;
    
    /* Skip "OpenGL ES " prefix if present */
    if (version[0] == 'O') {
        while (*p && *p != ' ') p++;
        if (*p == ' ') p++;
        if (*p == 'E') {
            while (*p && *p != ' ') p++;
            if (*p == ' ') p++;
        }
    }
    
    *major = 0;
    *minor = 0;
    
    /* Parse major version */
    while (*p >= '0' && *p <= '9') {
        *major = *major * 10 + (*p - '0');
        p++;
    }
    
    /* Skip dot */
    if (*p == '.') p++;
    
    /* Parse minor version */
    while (*p >= '0' && *p <= '9') {
        *minor = *minor * 10 + (*p - '0');
        p++;
    }
}

/* Load Core 1.0 functions */
static int glad_load_gl_1_0(GLADloadfunc load) {
    glad_glClear = (PFNGLCLEARPROC)load("glClear");
    glad_glClearColor = (PFNGLCLEARCOLORPROC)load("glClearColor");
    glad_glEnable = (PFNGLENABLEPROC)load("glEnable");
    glad_glDisable = (PFNGLDISABLEPROC)load("glDisable");
    glad_glViewport = (PFNGLVIEWPORTPROC)load("glViewport");
    glad_glTexImage2D = (PFNGLTEXIMAGE2DPROC)load("glTexImage2D");
    glad_glTexParameteri = (PFNGLTEXPARAMETERIPROC)load("glTexParameteri");
    glad_glBindTexture = (PFNGLBINDTEXTUREPROC)load("glBindTexture");
    glad_glDeleteTextures = (PFNGLDELETETEXTURESPROC)load("glDeleteTextures");
    glad_glGenTextures = (PFNGLGENTEXTURESPROC)load("glGenTextures");
    glad_glDrawArrays = (PFNGLDRAWARRAYSPROC)load("glDrawArrays");
    glad_glGetString = (PFNGLGETSTRINGPROC)load("glGetString");
    glad_glGetIntegerv = (PFNGLGETINTEGERVPROC)load("glGetIntegerv");
    glad_glBlendFunc = (PFNGLBLENDFUNCPROC)load("glBlendFunc");
    
    return glad_glClear && glad_glClearColor && glad_glEnable && glad_glDisable &&
           glad_glViewport && glad_glTexImage2D && glad_glTexParameteri && 
           glad_glBindTexture && glad_glDeleteTextures && glad_glGenTextures &&
           glad_glDrawArrays && glad_glGetString && glad_glGetIntegerv && glad_glBlendFunc;
}

/* Load Core 1.3 functions */
static int glad_load_gl_1_3(GLADloadfunc load) {
    glad_glActiveTexture = (PFNGLACTIVETEXTUREPROC)load("glActiveTexture");
    return glad_glActiveTexture != NULL;
}

/* Load Core 1.5 functions */
static int glad_load_gl_1_5(GLADloadfunc load) {
    glad_glGenBuffers = (PFNGLGENBUFFERSPROC)load("glGenBuffers");
    glad_glDeleteBuffers = (PFNGLDELETEBUFFERSPROC)load("glDeleteBuffers");
    glad_glBindBuffer = (PFNGLBINDBUFFERPROC)load("glBindBuffer");
    glad_glBufferData = (PFNGLBUFFERDATAPROC)load("glBufferData");
    
    return glad_glGenBuffers && glad_glDeleteBuffers && 
           glad_glBindBuffer && glad_glBufferData;
}

/* Load Core 2.0 functions */
static int glad_load_gl_2_0(GLADloadfunc load) {
    glad_glCreateShader = (PFNGLCREATESHADERPROC)load("glCreateShader");
    glad_glDeleteShader = (PFNGLDELETESHADERPROC)load("glDeleteShader");
    glad_glShaderSource = (PFNGLSHADERSOURCEPROC)load("glShaderSource");
    glad_glCompileShader = (PFNGLCOMPILESHADERPROC)load("glCompileShader");
    glad_glGetShaderiv = (PFNGLGETSHADERIVPROC)load("glGetShaderiv");
    glad_glGetShaderInfoLog = (PFNGLGETSHADERINFOLOGPROC)load("glGetShaderInfoLog");
    glad_glCreateProgram = (PFNGLCREATEPROGRAMPROC)load("glCreateProgram");
    glad_glDeleteProgram = (PFNGLDELETEPROGRAMPROC)load("glDeleteProgram");
    glad_glAttachShader = (PFNGLATTACHSHADERPROC)load("glAttachShader");
    glad_glLinkProgram = (PFNGLLINKPROGRAMPROC)load("glLinkProgram");
    glad_glGetProgramiv = (PFNGLGETPROGRAMIVPROC)load("glGetProgramiv");
    glad_glGetProgramInfoLog = (PFNGLGETPROGRAMINFOLOGPROC)load("glGetProgramInfoLog");
    glad_glUseProgram = (PFNGLUSEPROGRAMPROC)load("glUseProgram");
    glad_glGetUniformLocation = (PFNGLGETUNIFORMLOCATIONPROC)load("glGetUniformLocation");
    glad_glUniform1i = (PFNGLUNIFORM1IPROC)load("glUniform1i");
    glad_glUniform1f = (PFNGLUNIFORM1FPROC)load("glUniform1f");
    glad_glUniform2f = (PFNGLUNIFORM2FPROC)load("glUniform2f");
    glad_glUniform4f = (PFNGLUNIFORM4FPROC)load("glUniform4f");
    glad_glUniformMatrix4fv = (PFNGLUNIFORMMATRIX4FVPROC)load("glUniformMatrix4fv");
    glad_glBindAttribLocation = (PFNGLBINDATTRIBLOCATIONPROC)load("glBindAttribLocation");
    glad_glDetachShader = (PFNGLDETACHSHADERPROC)load("glDetachShader");
    glad_glVertexAttribPointer = (PFNGLVERTEXATTRIBPOINTERPROC)load("glVertexAttribPointer");
    glad_glEnableVertexAttribArray = (PFNGLENABLEVERTEXATTRIBARRAYPROC)load("glEnableVertexAttribArray");
    glad_glDisableVertexAttribArray = (PFNGLDISABLEVERTEXATTRIBARRAYPROC)load("glDisableVertexAttribArray");
    
    return glad_glCreateShader && glad_glDeleteShader && glad_glShaderSource &&
           glad_glCompileShader && glad_glGetShaderiv && glad_glGetShaderInfoLog &&
           glad_glCreateProgram && glad_glDeleteProgram && glad_glAttachShader &&
           glad_glLinkProgram && glad_glGetProgramiv && glad_glGetProgramInfoLog &&
           glad_glUseProgram && glad_glGetUniformLocation && glad_glUniform1i &&
           glad_glUniform1f && glad_glUniform2f && glad_glUniform4f &&
           glad_glUniformMatrix4fv && glad_glBindAttribLocation && glad_glDetachShader &&
           glad_glVertexAttribPointer && glad_glEnableVertexAttribArray && 
           glad_glDisableVertexAttribArray;
}

/* Load Core 3.0 functions (framebuffer and VAO) */
static int glad_load_gl_3_0(GLADloadfunc load) {
    /* Framebuffer functions */
    glad_glGenFramebuffers = (PFNGLGENFRAMEBUFFERSPROC)load("glGenFramebuffers");
    glad_glDeleteFramebuffers = (PFNGLDELETEFRAMEBUFFERSPROC)load("glDeleteFramebuffers");
    glad_glBindFramebuffer = (PFNGLBINDFRAMEBUFFERPROC)load("glBindFramebuffer");
    glad_glFramebufferTexture2D = (PFNGLFRAMEBUFFERTEXTURE2DPROC)load("glFramebufferTexture2D");
    glad_glCheckFramebufferStatus = (PFNGLCHECKFRAMEBUFFERSTATUSPROC)load("glCheckFramebufferStatus");
    glad_glGenRenderbuffers = (PFNGLGENRENDERBUFFERSPROC)load("glGenRenderbuffers");
    glad_glDeleteRenderbuffers = (PFNGLDELETERENDERBUFFERSPROC)load("glDeleteRenderbuffers");
    glad_glBindRenderbuffer = (PFNGLBINDRENDERBUFFERPROC)load("glBindRenderbuffer");
    glad_glRenderbufferStorage = (PFNGLRENDERBUFFERSTORAGEPROC)load("glRenderbufferStorage");
    glad_glFramebufferRenderbuffer = (PFNGLFRAMEBUFFERRENDERBUFFERPROC)load("glFramebufferRenderbuffer");
    glad_glBlitFramebuffer = (PFNGLBLITFRAMEBUFFERPROC)load("glBlitFramebuffer");
    
    /* Mipmap */
    glad_glGenerateMipmap = (PFNGLGENERATEMIPMAPPROC)load("glGenerateMipmap");
    
    /* VAO functions */
    glad_glGenVertexArrays = (PFNGLGENVERTEXARRAYSPROC)load("glGenVertexArrays");
    glad_glDeleteVertexArrays = (PFNGLDELETEVERTEXARRAYSPROC)load("glDeleteVertexArrays");
    glad_glBindVertexArray = (PFNGLBINDVERTEXARRAYPROC)load("glBindVertexArray");
    
    return glad_glGenFramebuffers && glad_glDeleteFramebuffers && 
           glad_glBindFramebuffer && glad_glFramebufferTexture2D &&
           glad_glCheckFramebufferStatus && glad_glGenRenderbuffers &&
           glad_glDeleteRenderbuffers && glad_glBindRenderbuffer &&
           glad_glRenderbufferStorage && glad_glFramebufferRenderbuffer &&
           glad_glBlitFramebuffer && glad_glGenVertexArrays &&
           glad_glDeleteVertexArrays && glad_glBindVertexArray;
}

/**
 * Initialize GLAD loader
 * 
 * @param load Function pointer to load GL function pointers (e.g., SDL_GL_GetProcAddress)
 * @return GL version (major * 10000 + minor), or 0 on failure
 */
int gladLoadGL(GLADloadfunc load) {
    int major = 0, minor = 0;
    const char* version;
    
    if (load == NULL) {
        return 0;
    }
    
    /* First, we need glGetString to get the version */
    glad_glGetString = (PFNGLGETSTRINGPROC)load("glGetString");
    if (glad_glGetString == NULL) {
        return 0;
    }
    
    version = (const char*)glad_glGetString(0x1F02); /* GL_VERSION */
    if (version == NULL) {
        return 0;
    }
    
    glad_parse_version(version, &major, &minor);
    
    /* Set version flags */
    GLAD_GL_VERSION_1_0 = (major > 1 || (major == 1 && minor >= 0));
    GLAD_GL_VERSION_1_1 = (major > 1 || (major == 1 && minor >= 1));
    GLAD_GL_VERSION_1_2 = (major > 1 || (major == 1 && minor >= 2));
    GLAD_GL_VERSION_1_3 = (major > 1 || (major == 1 && minor >= 3));
    GLAD_GL_VERSION_1_4 = (major > 1 || (major == 1 && minor >= 4));
    GLAD_GL_VERSION_1_5 = (major > 1 || (major == 1 && minor >= 5));
    GLAD_GL_VERSION_2_0 = (major > 2 || (major == 2 && minor >= 0));
    GLAD_GL_VERSION_2_1 = (major > 2 || (major == 2 && minor >= 1));
    GLAD_GL_VERSION_3_0 = (major > 3 || (major == 3 && minor >= 0));
    GLAD_GL_VERSION_3_1 = (major > 3 || (major == 3 && minor >= 1));
    GLAD_GL_VERSION_3_2 = (major > 3 || (major == 3 && minor >= 2));
    GLAD_GL_VERSION_3_3 = (major > 3 || (major == 3 && minor >= 3));
    
    /* Load functions by version */
    if (GLAD_GL_VERSION_1_0 && !glad_load_gl_1_0(load)) return 0;
    if (GLAD_GL_VERSION_1_3 && !glad_load_gl_1_3(load)) return 0;
    if (GLAD_GL_VERSION_1_5 && !glad_load_gl_1_5(load)) return 0;
    if (GLAD_GL_VERSION_2_0 && !glad_load_gl_2_0(load)) return 0;
    if (GLAD_GL_VERSION_3_0 && !glad_load_gl_3_0(load)) return 0;
    
    return major * 10000 + minor;
}
