/*
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.
*/

#include "game/renderer/master_renderer.hpp"

#include "common/logger.hpp"
#if defined(RENDERER_GL)
#include "game/renderer/gl/gl_context.hpp"
#include "game/renderer/gl/gl_mesh_renderer.hpp"
#include "game/renderer/gl/gl_gui_renderer.hpp"
#include "game/renderer/gl/gl_shader.hpp"
#include "game/renderer/gl/gl_mesh.hpp"
#include "game/renderer/gl/gl_texture.hpp"
#endif
#include "game/window/window.hpp"

using namespace polygun::renderer;

#if defined(RENDERER_GL)
static void clear_screen_impl_gl(float r, float g, float b) {
	glClearColor(r, g, b, 1.0f);
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 
}
#endif

MasterRenderer::MasterRenderer() :
	m_renderer_type(RendererType::RENDERER_TYPE_GL),
	m_clear_screen_impl(),
#if defined(RENDERER_GL)
	m_gl_context(nullptr),
#endif
	m_mesh_renderer(),
	m_gui_renderer()
{}

MasterRenderer::~MasterRenderer() {
#if defined(RENDERER_GL)
	if(m_gl_context)
		delete m_gl_context;
#endif
}

void MasterRenderer::init(RendererType renderer_type, window::Window* window) {
	bool created = false;
	m_renderer_type = renderer_type;
	switch(m_renderer_type) {
	case RendererType::RENDERER_TYPE_GL:
#if defined(RENDERER_GL)
		create_opengl_renderer(window);
		created = true;
#endif
		break;
	}
	if(!created)
		LOG_FATAL("Could not create any available renderer");
}

void MasterRenderer::clear_screen(float r, float g, float b) {
	m_clear_screen_impl(r, g, b);
}

Shader* MasterRenderer::create_shader() const {
	switch(m_renderer_type) {
	case RendererType::RENDERER_TYPE_GL:
#if defined(RENDERER_GL)
		return new GLShader;
#else
		LOG_FATAL("Requested creating GLShader while OpenGL renderer is not available!");
		break;
#endif
	}
	return nullptr;
}

Mesh* MasterRenderer::create_mesh() const {
	switch(m_renderer_type) {
	case RendererType::RENDERER_TYPE_GL:
#if defined(RENDERER_GL)
		return new GLMesh;
#else
		LOG_FATAL("Requested creating GLMesh while OpenGL renderer is not available!");
		break;
#endif
	}
	return nullptr;
}

Texture* MasterRenderer::create_texture() const {
	switch(m_renderer_type) {
	case RendererType::RENDERER_TYPE_GL:
#if defined(RENDERER_GL)
		return new GLTexture;
#else
		LOG_FATAL("Requested creating GLTexture while OpenGL renderer is not available!");
		break;
#endif
	}
	return nullptr;
}

#if defined(RENDERER_GL)
void MasterRenderer::create_opengl_renderer(window::Window* window) {
	m_clear_screen_impl = clear_screen_impl_gl;
	m_gl_context = GLContext::create_context(window);
	LOG_INFO("OpenGL version string: %s", glGetString(GL_VERSION));
	LOG_INFO("OpenGL vendor: %s", glGetString(GL_VENDOR));
	LOG_INFO("OpenGL renderer: %s", glGetString(GL_RENDERER));
	LOG_INFO("GLSL version: %s", glGetString(GL_SHADING_LANGUAGE_VERSION));
	m_gl_context->query_extensions();
	opengl::init(m_gl_context);
	m_mesh_renderer.reset(new GLMeshRenderer);
	m_gui_renderer.reset(new GLGUIRenderer(this));
}
#endif
