// =====================================================================================================================
//  fennec, a free and open source game engine
//  Copyright © 2025 - 2026  Medusa Slockbower
//
//  This program is free software: you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation, either version 3 of the License, or
//  (at your option) any later version.
//
//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
//
//  You should have received a copy of the GNU General Public License
//  along with this program.  If not, see <https://www.gnu.org/licenses/>.
// =====================================================================================================================


#include <fennec/core/logger.h>
#include <fennec/renderers/vulkan/vkcontext.h>

namespace fennec {

vkcontext::vkcontext(display_server* display, const dynarray<cstring>& extensions)
	: gfxcontext(display) {

	const vk::app_info app(
		"", {
			.major = 1,
			.minor = 0,
			.patch = 0,
			.meta  = 0
		},
		"fennec", {
			.major = FENNEC_VERSION_MAJOR,
			.minor = FENNEC_VERSION_MINOR,
			.patch = FENNEC_VERSION_PATCH,
		}
	);

	vk::instance::create_info info(
		vk::instance_flag_none, app
	);

	if constexpr (FENNEC_DEBUG) {
		info.enable_layer("VK_LAYER_KHRONOS_validation");
		info.enable_extension(VK_EXT_DEBUG_UTILS_EXTENSION_NAME);

		info.attach_debugger(*this);
	}

	for (const cstring& ext : extensions) {
		info.enable_extension(ext);
	}

	instance = make_unique<vk::instance>(info);
}

vkcontext::~vkcontext() {
}

bool vkcontext::is_valid() {
	return instance;
}

void vkcontext::message(uint32_t                 severity,
	                    uint32_t                 type,
	                    const cstring&           id,
	                    const cstring&           message,
                        const dynarray<cstring>& queue,
                        const dynarray<cstring>& commands,
                        const dynarray<string>&  objects) {

	if (id == "specialuse-extension") {
		return;
	}

	logger::log(translate_severity(severity), format(
		"\n\t[Vulkan Message] \"{}\" - #{}\n\t{}\n\tObjects = {}\n\tQueue = {}\n\tCommands = {}",
		vk::message_type_string(type), id, message,
		objects, queue, commands
	));
}
} // fennec