/*
* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at https://mozilla.org/MPL/2.0/.
 */

#include "ErrorUtils.hpp"

#if defined(_MSC_VER)
#include "Windows.h"
#endif

namespace OpenVulkano
{
#if defined(_MSC_VER)
	std::string ErrorUtils::GetLastErrorMessage()
	{
		// Get the error message ID, if any.
		DWORD errorID = GetLastError();
		if (errorID == 0)
		{
			return std::string();
		}

		LPSTR messageBuffer = nullptr;
		// Format the error message
		uint64_t size =
		FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
		               NULL, errorID, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPSTR) &messageBuffer, 0, NULL);

		// Buffer it up
		std::string message(messageBuffer, size);

		// Free the buffer and return the message
		LocalFree(messageBuffer);
		return message;
	}
#else
	std::string ErrorUtils::GetLastErrorMessage()
	{
		return std::string();
	}
#endif

}