#include "detail/graphics.h"

namespace deerith {
	namespace graphics {
		bool check_validation_layer_support();
	} // namespace graphics

	void graphics::create_vulkan_validation_layer() {
		if (!ACTIVE_VALIDATION_LAYER)
			return;

		deerith_graphics_trace("Initializing Vulkan validation layer");

		if (!check_validation_layer_support()) {
			deerith_graphics_error("Vulkan validation layer is not aviable");
			return;
		}
	}

	bool graphics::check_validation_layer_support() {
		uint32_t layer_count;
		vkEnumerateInstanceLayerProperties(&layer_count, nullptr);

		std::vector<VkLayerProperties> available_layers(layer_count);
		vkEnumerateInstanceLayerProperties(&layer_count, available_layers.data());

		for (const char* layerName : validation_layers) {
			bool layerFound = false;

			for (const auto& layerProperties : available_layers) {
				if (strcmp(layerName, layerProperties.layerName) == 0) {
					layerFound = true;
					break;
				}
			}

			if (!layerFound) {
				return false;
			}
		}

		return true;
	}

	std::vector<const char*> get_required_extensions() {
		uint32_t glfw_extension_count = 0;
		const char** glfw_extensions;
		glfw_extensions = glfwGetRequiredInstanceExtensions(&glfw_extension_count);

		std::vector<const char*> extensions(glfw_extensions, glfw_extensions + glfw_extension_count);

		if (ACTIVE_VALIDATION_LAYER) {
			extensions.push_back(VK_EXT_DEBUG_UTILS_EXTENSION_NAME);
		}

		return extensions;
	}

} // namespace deerith