#ifndef NO_SHADERS

#include "jail_shader.h"
#include <SDL2/SDL_rect.h>    // for SDL_Point
#include <stdlib.h>           // for NULL, free, malloc, exit
#include <string.h>           // for strncmp
#include <iostream>           // for basic_ostream, char_traits, operator<<

#ifdef __APPLE__
#include "CoreFoundation/CoreFoundation.h"
#include <OpenGL/OpenGL.h>

#if ESSENTIAL_GL_PRACTICES_SUPPORT_GL3
#include <OpenGL/gl3.h>
#else
#include <OpenGL/gl.h>
#endif //!ESSENTIAL_GL_PRACTICES_SUPPORT_GL3
#else
#include <SDL2/SDL_opengl.h>
#endif

namespace shader
{
    SDL_Window *win = nullptr;
    SDL_Renderer *renderer = nullptr;
    GLuint programId = 0;
    SDL_Texture* backBuffer = nullptr;
    SDL_Point win_size = {320*4, 256*4};
    SDL_Point tex_size = {320, 256};
    bool usingOpenGL;

    #ifndef __APPLE__

    // I'm avoiding the use of GLEW or some extensions handler, but that 
    // doesn't mean you should...
    PFNGLCREATESHADERPROC glCreateShader;
    PFNGLSHADERSOURCEPROC glShaderSource;
    PFNGLCOMPILESHADERPROC glCompileShader;
    PFNGLGETSHADERIVPROC glGetShaderiv;
    PFNGLGETSHADERINFOLOGPROC glGetShaderInfoLog;
    PFNGLDELETESHADERPROC glDeleteShader;
    PFNGLATTACHSHADERPROC glAttachShader;
    PFNGLCREATEPROGRAMPROC glCreateProgram;
    PFNGLLINKPROGRAMPROC glLinkProgram;
    PFNGLVALIDATEPROGRAMPROC glValidateProgram;
    PFNGLGETPROGRAMIVPROC glGetProgramiv;
    PFNGLGETPROGRAMINFOLOGPROC glGetProgramInfoLog;
    PFNGLUSEPROGRAMPROC glUseProgram;

    bool initGLExtensions() {
        glCreateShader = (PFNGLCREATESHADERPROC)SDL_GL_GetProcAddress("glCreateShader");
        glShaderSource = (PFNGLSHADERSOURCEPROC)SDL_GL_GetProcAddress("glShaderSource");
        glCompileShader = (PFNGLCOMPILESHADERPROC)SDL_GL_GetProcAddress("glCompileShader");
        glGetShaderiv = (PFNGLGETSHADERIVPROC)SDL_GL_GetProcAddress("glGetShaderiv");
        glGetShaderInfoLog = (PFNGLGETSHADERINFOLOGPROC)SDL_GL_GetProcAddress("glGetShaderInfoLog");
        glDeleteShader = (PFNGLDELETESHADERPROC)SDL_GL_GetProcAddress("glDeleteShader");
        glAttachShader = (PFNGLATTACHSHADERPROC)SDL_GL_GetProcAddress("glAttachShader");
        glCreateProgram = (PFNGLCREATEPROGRAMPROC)SDL_GL_GetProcAddress("glCreateProgram");
        glLinkProgram = (PFNGLLINKPROGRAMPROC)SDL_GL_GetProcAddress("glLinkProgram");
        glValidateProgram = (PFNGLVALIDATEPROGRAMPROC)SDL_GL_GetProcAddress("glValidateProgram");
        glGetProgramiv = (PFNGLGETPROGRAMIVPROC)SDL_GL_GetProcAddress("glGetProgramiv");
        glGetProgramInfoLog = (PFNGLGETPROGRAMINFOLOGPROC)SDL_GL_GetProcAddress("glGetProgramInfoLog");
        glUseProgram = (PFNGLUSEPROGRAMPROC)SDL_GL_GetProcAddress("glUseProgram");

        return glCreateShader && glShaderSource && glCompileShader && glGetShaderiv && 
            glGetShaderInfoLog && glDeleteShader && glAttachShader && glCreateProgram &&
            glLinkProgram && glValidateProgram && glGetProgramiv && glGetProgramInfoLog &&
            glUseProgram;
    }

    #endif

    GLuint compileShader(const char* source, GLuint shaderType) {
        // Create ID for shader
        GLuint result = glCreateShader(shaderType);
        // Add define depending on shader type
        const char *sources[2] = { shaderType==GL_VERTEX_SHADER?"#define VERTEX\n":"#define FRAGMENT\n", source };
        // Define shader text
        glShaderSource(result, 2, sources, NULL);
        // Compile shader
        glCompileShader(result);

        //Check vertex shader for errors
        GLint shaderCompiled = GL_FALSE;
        glGetShaderiv( result, GL_COMPILE_STATUS, &shaderCompiled );
        if( shaderCompiled != GL_TRUE ) {
            std::cout << "Error en la compilación: " << result << "!" << std::endl;
            GLint logLength;
            glGetShaderiv(result, GL_INFO_LOG_LENGTH, &logLength);
            if (logLength > 0)
            {
                GLchar *log = (GLchar*)malloc(logLength);
                glGetShaderInfoLog(result, logLength, &logLength, log);
                std::cout << "Shader compile log:" << log << std::endl;
                free(log);
            }
            glDeleteShader(result);
            result = 0;
//        } else {
//            std::cout << "Shader compilado correctamente. Id = " << result << std::endl;
        }
        return result;
    }

    GLuint compileProgram(const char* vertexShaderSource, const char* fragmentShaderSource) {
        GLuint programId = 0;
        GLuint vtxShaderId, fragShaderId;

        programId = glCreateProgram();

        vtxShaderId = compileShader(vertexShaderSource, GL_VERTEX_SHADER);
        fragShaderId = compileShader(fragmentShaderSource?fragmentShaderSource:vertexShaderSource, GL_FRAGMENT_SHADER);

        if(vtxShaderId && fragShaderId) {
            // Associate shader with program
            glAttachShader(programId, vtxShaderId);
            glAttachShader(programId, fragShaderId);
            glLinkProgram(programId);
            glValidateProgram(programId);

            // Check the status of the compile/link
            GLint logLen;
            glGetProgramiv(programId, GL_INFO_LOG_LENGTH, &logLen);
            if(logLen > 0) {
                char* log = (char*) malloc(logLen * sizeof(char));
                // Show any errors as appropriate
                glGetProgramInfoLog(programId, logLen, &logLen, log);
                std::cout << "Prog Info Log: " << std::endl << log << std::endl;
                free(log);
            }
        }
        if(vtxShaderId) {
            glDeleteShader(vtxShaderId);
        }
        if(fragShaderId) {
            glDeleteShader(fragShaderId);
        }
        return programId;
    }

    const bool init(SDL_Window* win, SDL_Texture* backBuffer, const char* vertexShader, const char* fragmentShader)
    {
        shader::win = win;
        shader::renderer = SDL_GetRenderer(win);
        shader::backBuffer = backBuffer;
        SDL_GetWindowSize(win, &win_size.x, &win_size.y);
        int access;
        SDL_QueryTexture(backBuffer, NULL, &access, &tex_size.x, &tex_size.y);
        if (access != SDL_TEXTUREACCESS_TARGET)
        {
            std::cout << "ERROR FATAL: La textura per al render ha de tindre SDL_TEXTUREACCESS_TARGET definit." << std::endl;
            exit(1);
        }

        SDL_RendererInfo rendererInfo;
        SDL_GetRendererInfo(renderer, &rendererInfo);

        if(!strncmp(rendererInfo.name, "opengl", 6)) {
            //std::cout << "Es OpenGL!" << std::endl;
    #ifndef __APPLE__
            if (!initGLExtensions()) {
                std::cout << "WARNING: No s'han pogut inicialitzar les extensions d'OpenGL!" << std::endl;
                usingOpenGL = false;
                return false;
            }
    #endif
            // Compilar el shader y dejarlo listo para usar.
            programId = compileProgram(vertexShader, fragmentShader);
            //std::cout << "programId = " << programId << std::endl;
        } else {
            std::cout << "WARNING: El driver del renderer no es OpenGL." << std::endl;
            usingOpenGL = false;
            return false;
        }
        usingOpenGL = true;
        return true;
    }

    void render()
    {
        GLint oldProgramId;
        // Guarrada para obtener el textureid (en driverdata->texture)
        //Detach the texture
        SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);

        SDL_SetRenderTarget(renderer, NULL);
        SDL_RenderClear(renderer);

        if (usingOpenGL) {
            SDL_GL_BindTexture(backBuffer, NULL, NULL);
            if(programId != 0) {
                glGetIntegerv(GL_CURRENT_PROGRAM,&oldProgramId);
                glUseProgram(programId);
            }
            
            GLfloat minx, miny, maxx, maxy;
            GLfloat minu, maxu, minv, maxv;

            // Coordenadas de la ventana donde pintar.
            minx = 0.0f;
            miny = 0.0f;
            maxx = tex_size.x;
            maxy = tex_size.y;

            minu = 0.0f;
            maxu = 1.0f;
            minv = 0.0f;
            maxv = 1.0f;

            glViewport(0, 0, win_size.x, win_size.y);

            glBegin(GL_TRIANGLE_STRIP);
                glTexCoord2f(minu, minv);
                glVertex2f(minx, miny);
                glTexCoord2f(maxu, minv);
                glVertex2f(maxx, miny);
                glTexCoord2f(minu, maxv);
                glVertex2f(minx, maxy);
                glTexCoord2f(maxu, maxv);
                glVertex2f(maxx, maxy);
            glEnd();
            SDL_GL_SwapWindow(win);

            if(programId != 0) {
                glUseProgram(oldProgramId);
            }
        } else {
            SDL_RenderCopy(renderer, backBuffer, NULL, NULL);
            SDL_RenderPresent(renderer);
        }
    }
}
#endif