/*
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/gl/egl_gl_context.hpp"

#include "common/logger.hpp"
#include "game/window/window.hpp"
#if defined(WINDOW_WAYLAND)
#include "game/window/wayland_window.hpp"
#endif

using namespace polygun::renderer;

EGLGLContext::EGLGLContext(window::Window* window) :
	m_display(),
	m_config(),
	m_context()
{
	switch(window->get_window_type()) {
	case window::WindowType::WINDOW_TYPE_WIN32:
	case window::WindowType::WINDOW_TYPE_X11:
	case window::WindowType::WINDOW_TYPE_SERENITYOS:
		LOG_FATAL("Failed to create EGL context: EGL is only supported on Wayland");
		break;
	case window::WindowType::WINDOW_TYPE_WAYLAND:
#if defined(WINDOW_WAYLAND)
		m_display = eglGetDisplay(static_cast<window::WaylandWindow*>(window)->get_display());
#else
		LOG_FATAL("Requested creating EGL context on Wayland while Wayland window support is not compiled in");
#endif
		break;
		
	}
	initialize();
}

EGLGLContext::~EGLGLContext() {
	eglMakeCurrent(m_display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
	eglDestroySurface(m_display, m_surface);
	if(m_context)
		eglDestroyContext(m_display, m_context);
	eglTerminate(m_display);
	eglReleaseThread();
}

#if defined(WINDOW_WAYLAND)
void EGLGLContext::activate(wl_egl_window* window) {
	m_surface = eglCreateWindowSurface(m_display, m_config, reinterpret_cast<NativeWindowType>(window), nullptr);
	if(!eglMakeCurrent(m_display, m_surface, m_surface, m_context))
		LOG_FATAL("Failed to make EGL context current");
}
#endif

void EGLGLContext::swap_buffers() {
	eglSwapBuffers(m_display, m_surface);
}

#if defined(RENDERER_GL)
void* EGLGLContext::get_proc_address(const std::string& name) {
	return reinterpret_cast<void*>(eglGetProcAddress(name.c_str()));
}
#endif

void EGLGLContext::initialize() {
	const int context_attribs[] = {
		EGL_NONE
	};
	int surface_attribs[] = {
		EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
		EGL_RED_SIZE, 8,
		EGL_GREEN_SIZE, 8,
		EGL_BLUE_SIZE, 8,
		EGL_ALPHA_SIZE, 8,
		EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT,
		EGL_NONE
	};

	EGLint egl_major, egl_minor;
	if(!eglInitialize(m_display, &egl_major, &egl_minor))
		LOG_FATAL("Failed to initialize EGL");
	LOG_INFO("EGL version: %d.%d", egl_major, egl_minor);

	if(!eglBindAPI(EGL_OPENGL_API))
		LOG_FATAL("Failed to bind OpenGL EGL API");

	EGLint config_count;
	if(!eglChooseConfig(m_display, surface_attribs, &m_config, 1, &config_count) || config_count!=1)
		LOG_FATAL("Failed to choose EGL config");

	m_context = eglCreateContext(m_display, m_config, EGL_NO_CONTEXT, context_attribs);
	if(!m_context)
		LOG_FATAL("Failed to create EGL context");
}
