/*
PolyGun

Copyright (c) 2023 mrkubax10 <mrkubax10@onet.pl>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

// Simple OpenGL extension loader to replace GLEW because it has some problems

#include "game/renderer/gl/opengl.hpp"

#include "game/renderer/gl/gl_context.hpp"
#include "common/logger.hpp"

#if defined(RENDERER_GL)
static void is_extension_supported(polygun::renderer::GLContext* context, const std::string& name, const std::string& display_name, bool& output) {
	output = context->is_extension_supported(name.c_str());
	if(output)
		LOG_INFO("%s are supported by GPU", display_name.c_str());
	else
		LOG_INFO("%s aren't supported by GPU", display_name.c_str());
}

bool GLEW_ARB_vertex_array_object;
bool GLEW_ARB_framebuffer_object;

PFNGLGENVERTEXARRAYSPROC glGenVertexArrays;
PFNGLDELETEVERTEXARRAYSPROC glDeleteVertexArrays;
PFNGLBINDVERTEXARRAYPROC glBindVertexArray;
PFNGLVERTEXATTRIBPOINTERPROC glVertexAttribPointer;
PFNGLENABLEVERTEXATTRIBARRAYPROC glEnableVertexAttribArray;
PFNGLDISABLEVERTEXATTRIBARRAYPROC glDisableVertexAttribArray;
PFNGLGENBUFFERSPROC glGenBuffers;
PFNGLDELETEBUFFERSPROC glDeleteBuffers;
PFNGLBINDBUFFERPROC glBindBuffer;
PFNGLBUFFERDATAPROC glBufferData;
PFNGLGENERATEMIPMAPPROC glGenerateMipmap;
PFNGLCREATEPROGRAMPROC glCreateProgram;
PFNGLDELETEPROGRAMPROC glDeleteProgram;
PFNGLATTACHSHADERPROC glAttachShader;
PFNGLDETACHSHADERPROC glDetachShader;
PFNGLLINKPROGRAMPROC glLinkProgram;
PFNGLUSEPROGRAMPROC glUseProgram;
PFNGLGETPROGRAMIVPROC glGetProgramiv;
PFNGLGETPROGRAMINFOLOGPROC glGetProgramInfoLog;
PFNGLGETUNIFORMLOCATIONPROC glGetUniformLocation;
PFNGLUNIFORM1IPROC glUniform1i;
PFNGLUNIFORM2FPROC glUniform2f;
PFNGLUNIFORM3FPROC glUniform3f;
PFNGLUNIFORM4FPROC glUniform4f;
PFNGLUNIFORMMATRIX4FVPROC glUniformMatrix4fv;
PFNGLGETATTRIBLOCATIONPROC glGetAttribLocation;
PFNGLCREATESHADERPROC glCreateShader;
PFNGLDELETESHADERPROC glDeleteShader;
PFNGLSHADERSOURCEPROC glShaderSource;
PFNGLCOMPILESHADERPROC glCompileShader;
PFNGLGETSHADERIVPROC glGetShaderiv;
PFNGLGETSHADERINFOLOGPROC glGetShaderInfoLog;
PFNGLBINDSAMPLERPROC glBindSampler;
PFNGLDRAWELEMENTSBASEVERTEXPROC glDrawElementsBaseVertex;
PFNGLBLENDEQUATIONSEPARATEPROC glBlendEquationSeparate;
PFNGLBLENDFUNCSEPARATEPROC glBlendFuncSeparate;
#if defined(_WIN32)
PFNGLBLENDEQUATIONPROC glBlendEquation;
PFNGLACTIVETEXTUREPROC glActiveTexture;
#endif

void polygun::opengl::init(renderer::GLContext* context) {
	is_extension_supported(context, "GL_ARB_vertex_array_object", "Vertex Array Objects", GLEW_ARB_vertex_array_object);
	is_extension_supported(context, "GL_ARB_framebuffer_object", "Framebuffer Objects", GLEW_ARB_framebuffer_object);

	glGenVertexArrays = (PFNGLGENVERTEXARRAYSPROC)context->get_proc_address("glGenVertexArrays");
	glDeleteVertexArrays = (PFNGLDELETEVERTEXARRAYSPROC)context->get_proc_address("glDeleteVertexArrays");
	glBindVertexArray = (PFNGLBINDVERTEXARRAYPROC)context->get_proc_address("glBindVertexArray");
	glVertexAttribPointer = (PFNGLVERTEXATTRIBPOINTERPROC)context->get_proc_address("glVertexAttribPointer");
	glEnableVertexAttribArray = (PFNGLENABLEVERTEXATTRIBARRAYPROC)context->get_proc_address("glEnableVertexAttribArray");
	glDisableVertexAttribArray = (PFNGLDISABLEVERTEXATTRIBARRAYPROC)context->get_proc_address("glDisableVertexAttribArray");
	glGenBuffers = (PFNGLGENBUFFERSPROC)context->get_proc_address("glGenBuffers");
	glDeleteBuffers = (PFNGLDELETEBUFFERSPROC)context->get_proc_address("glDeleteBuffers");
	glBindBuffer = (PFNGLBINDBUFFERPROC)context->get_proc_address("glBindBuffer");
	glBufferData = (PFNGLBUFFERDATAPROC)context->get_proc_address("glBufferData");
	glGenerateMipmap = (PFNGLGENERATEMIPMAPPROC)context->get_proc_address("glGenerateMipmap");
	glCreateProgram = (PFNGLCREATEPROGRAMPROC)context->get_proc_address("glCreateProgram");
	glDeleteProgram = (PFNGLDELETEPROGRAMPROC)context->get_proc_address("glDeleteProgram");
	glAttachShader = (PFNGLATTACHSHADERPROC)context->get_proc_address("glAttachShader");
	glDetachShader = (PFNGLDETACHSHADERPROC)context->get_proc_address("glDetachShader");
	glLinkProgram = (PFNGLLINKPROGRAMPROC)context->get_proc_address("glLinkProgram");
	glUseProgram = (PFNGLUSEPROGRAMPROC)context->get_proc_address("glUseProgram");
	glGetProgramiv = (PFNGLGETPROGRAMIVPROC)context->get_proc_address("glGetProgramiv");
	glGetProgramInfoLog = (PFNGLGETPROGRAMINFOLOGPROC)context->get_proc_address("glGetProgramInfoLog");
	glGetUniformLocation = (PFNGLGETUNIFORMLOCATIONPROC)context->get_proc_address("glGetUniformLocation");
	glUniform1i = (PFNGLUNIFORM1IPROC)context->get_proc_address("glUniform1i");
	glUniform2f = (PFNGLUNIFORM2FPROC)context->get_proc_address("glUniform2f");
	glUniform3f = (PFNGLUNIFORM3FPROC)context->get_proc_address("glUniform3f");
	glUniform4f = (PFNGLUNIFORM4FPROC)context->get_proc_address("glUniform4f");
	glUniformMatrix4fv = (PFNGLUNIFORMMATRIX4FVPROC)context->get_proc_address("glUniformMatrix4fv");
	glGetAttribLocation = (PFNGLGETATTRIBLOCATIONPROC)context->get_proc_address("glGetAttribLocation");
	glCreateShader = (PFNGLCREATESHADERPROC)context->get_proc_address("glCreateShader");
	glDeleteShader = (PFNGLDELETESHADERPROC)context->get_proc_address("glDeleteShader");
	glShaderSource = (PFNGLSHADERSOURCEPROC)context->get_proc_address("glShaderSource");
	glCompileShader = (PFNGLCOMPILESHADERPROC)context->get_proc_address("glCompileShader");
	glGetShaderiv = (PFNGLGETSHADERIVPROC)context->get_proc_address("glGetShaderiv");
	glGetShaderInfoLog = (PFNGLGETSHADERINFOLOGPROC)context->get_proc_address("glGetShaderInfoLog");
	glBindSampler = (PFNGLBINDSAMPLERPROC)context->get_proc_address("glBindSampler");
	glDrawElementsBaseVertex = (PFNGLDRAWELEMENTSBASEVERTEXPROC)context->get_proc_address("glDrawElementsBaseVertex");
	glBlendEquationSeparate = (PFNGLBLENDEQUATIONSEPARATEPROC)context->get_proc_address("glBlendEquationSeparate");
	glBlendFuncSeparate = (PFNGLBLENDFUNCSEPARATEPROC)context->get_proc_address("glBlendFuncSeparate");
#if defined(_WIN32)
	glBlendEquation = (PFNGLBLENDEQUATIONPROC)context->get_proc_address("glBlendEquation");
	glActiveTexture = (PFNGLACTIVETEXTUREPROC)context->get_proc_address("glActiveTexture");
#endif
}
#endif

void polygun::opengl::check_gl(const char* file, size_t line) {
	GLenum error = glGetError();
	if(error!=GL_NO_ERROR)
		utils::Logger::get().error(file, line, "Previous OpenGL operation failed with code: %d", error);
}

GLenum polygun::opengl::var_type_to_glenum(polygun::misc::VarType var_type) {
	switch(var_type) {
	case polygun::misc::VarType::VAR_TYPE_CHAR:
		return GL_BYTE;
	case polygun::misc::VarType::VAR_TYPE_UCHAR:
		return GL_UNSIGNED_BYTE;
	case polygun::misc::VarType::VAR_TYPE_SHORT:
		return GL_SHORT;
	case polygun::misc::VarType::VAR_TYPE_USHORT:
		return GL_UNSIGNED_SHORT;
	case polygun::misc::VarType::VAR_TYPE_INT:
		return GL_INT;
	case polygun::misc::VarType::VAR_TYPE_UINT:
		return GL_UNSIGNED_INT;
	case polygun::misc::VarType::VAR_TYPE_FLOAT:
		return GL_FLOAT;
	}
	return 0;
}
