// For licensing and usage information, read docs/release/winui_license.txt
//****************************************************************************

#ifndef WINUI_MUI_UTIL_H
#define WINUI_MUI_UTIL_H

#include "emucore.h"

void __cdecl ErrorMsg(const char* fmt, ...);
void __cdecl dprintf(const char* fmt, ...);


UINT GetDepth(HWND hWnd);

/* Open a text file */
void DisplayTextFile(HWND hWnd, const char *cName);

#define PACKVERSION(major,minor) MAKELONG(minor,major)

/* Check for old version of comctl32.dll */
LONG GetCommonControlVersion(void);

void ShellExecuteCommon(HWND hWnd, const char *cName);
char * MyStrStrI(const char* pFirst, const char* pSrch);
char * ConvertToWindowsNewlines(const char *source);

const char * GetDriverFilename(int);

BOOL DriverIsClone(uint32_t driver_index);
BOOL DriverIsBroken(uint32_t driver_index);
BOOL DriverIsHarddisk(uint32_t driver_index);
BOOL DriverHasOptionalBIOS(uint32_t driver_index);
BOOL DriverIsStereo(uint32_t driver_index);
BOOL DriverIsVector(uint32_t driver_index);
int DriverNumScreens(uint32_t driver_index);
BOOL DriverIsBios(uint32_t driver_index);
BOOL DriverUsesRoms(uint32_t driver_index);
BOOL DriverUsesSamples(uint32_t driver_index);
BOOL DriverUsesTrackball(uint32_t driver_index);
BOOL DriverUsesLightGun(uint32_t driver_index);
BOOL DriverUsesMouse(uint32_t driver_index);
BOOL DriverSupportsSaveState(uint32_t driver_index);
BOOL DriverIsVertical(uint32_t driver_index);
BOOL DriverIsMechanical(uint32_t driver_index);
BOOL DriverIsArcade(uint32_t driver_index);

int isDriverVector(const machine_config *config);
int numberOfSpeakers(const machine_config *config);
int numberOfScreens(const machine_config *config);

void FlushFileCaches(void);

BOOL StringIsSuffixedBy(const char *s, const char *suffix);

BOOL SafeIsAppThemed(void);

// provides result of FormatMessage()
// resulting buffer must be free'd with LocalFree()
void GetSystemErrorMessage(DWORD dwErrorId, TCHAR **tErrorMessage);

HICON win_extract_icon_utf8(HINSTANCE inst, const char* exefilename, UINT iconindex);
TCHAR* win_tstring_strdup(LPCTSTR str);
HANDLE win_create_file_utf8(const char* filename, DWORD desiredmode, DWORD sharemode,
							LPSECURITY_ATTRIBUTES securityattributes, DWORD creationdisposition,
							DWORD flagsandattributes, HANDLE templatehandle);
void ErrorMessageBox(const char *fmt, ...);

 //  wstring_from_utf8
 //============================================================

WCHAR *ui_wstring_from_utf8(const char *utf8string)
{
	int char_count;
	WCHAR *result;

	// convert MAME string (UTF-8) to UTF-16
	char_count = MultiByteToWideChar(CP_UTF8, 0, utf8string, -1, nullptr, 0);
	result = (WCHAR *)malloc(char_count * sizeof(*result));
	if (result != nullptr)
		MultiByteToWideChar(CP_UTF8, 0, utf8string, -1, result, char_count);

	return result;
}

char *ui_utf8_from_wstring(const WCHAR *wstring)
{
	int char_count;
	char *result;

	// convert UTF-16 to MAME string (UTF-8)
	char_count = WideCharToMultiByte(CP_UTF8, 0, wstring, -1, nullptr, 0, nullptr, nullptr);
	result = (char *)malloc(char_count * sizeof(*result));
	if (result != nullptr)
		WideCharToMultiByte(CP_UTF8, 0, wstring, -1, result, char_count, nullptr, nullptr);
	return result;
}

/// Convert a windows UTF-16 string to a UTF-8 string
///
/// @param s[in] the UTF-16 string
/// @return std::string UTF-8 string
inline std::string ui_to_utf8(std::wstring_view wstr)
{
	if (wstr.empty())
		return {};
	int len = ::WideCharToMultiByte(CP_UTF8, 0, &wstr[0], wstr.size(), nullptr, 0, nullptr, nullptr);
	std::string out(len, 0);
	WideCharToMultiByte(CP_UTF8, 0, &wstr[0], wstr.size(), &out[0], len, nullptr, nullptr);
	return out;
}

/// Convert a UTF-8 string to a windows UTF-16 string
///
/// @param s[in] the UTF-8 string
/// @param n[in] the UTF-8 string's length, or -1 if string is null-terminated
/// @return std::wstring UTF-16 string
inline std::wstring ui_to_utf16(std::string_view str)
{
	if (str.empty())
		return {};
	int len = ::MultiByteToWideChar(CP_UTF8, 0, &str[0], str.size(), NULL, 0);
	std::wstring out(len, 0);
	MultiByteToWideChar(CP_UTF8, 0, &str[0], str.size(), &out[0], len);
	return out;
}


#endif /* MUI_UTIL_H */

