#ifndef COMMON_H
#define COMMON_H

#include <memory>
#include <optional>
#include <windows.h>

#ifdef UNICODE
#define WA "W"
#else
#define WA "A"
#endif

struct wstring_owner
{
	wstring_owner();
	wstring_owner(wchar_t* s);
	wstring_owner& operator=(wchar_t* s);
	wstring_owner(wstring_owner& other) = delete;
	wstring_owner& operator=(wstring_owner& other) = delete;
	wstring_owner(wstring_owner&& other) noexcept;
	wstring_owner& operator=(wstring_owner&& other) noexcept;
	wchar_t *release();
	operator bool() const;
	~wstring_owner();
	wchar_t* p = nullptr;
};

wstring_owner WsoFromSz(const char* buf, int cp = CP_UTF8);
int EBMessageBox(const wchar_t* wszText, const wchar_t* wszCaption, unsigned uType);
wstring_owner WsoCopy(const wchar_t* s);

struct Win32Error : public std::exception
{
	Win32Error();
	Win32Error(DWORD code);
	~Win32Error();
	const char* what() const noexcept;
	const wchar_t* WhatW() const noexcept;
	DWORD code;
private:
	char* m_szMsg = NULL;
	wchar_t* m_wszMsg = NULL;
};

struct Library
{
	Library(const wchar_t* lib);
	~Library();
	template <class T> T* GetProcAddress(const char* szProc);
private:
	HMODULE m_hModule;
};

template <typename T>
T* Library::GetProcAddress(const char* const szProc)
{
	return (T*)(void*)::GetProcAddress(m_hModule, szProc);
}

template<size_t N, typename... T>
inline int wszf(wchar_t (&buf)[N], const wchar_t* const fmt, T... xs)
{
	return _snwprintf_s(buf, N, _TRUNCATE, fmt, xs...);
}

/* Create and return an object of type C. If construction fails,
 * return nothing. The returned value must be checked before being
 * used, as dereferencing is undefined if the value is empty. */
template <typename T, typename... U>
std::optional<T> maybe_make(U... xs)
{
	try {
		return T(xs...);
	} catch (...) {
		return {};
	}
}

/* Variable template for caching values from GetSystemMetrics. */
template <int I>
auto Metric = GetSystemMetrics(I);

/* Check result of Windows API call, throwing error on NULL. */
template <typename T>
inline T require(const T x)
{
	if (!x) throw Win32Error();
	return x;
}

/* Check result of Windows API call, showing a warning on NULL. */
template <typename T>
inline T prefer(const T x)
{
	if (!x) {
		EBMessageBox(Win32Error().WhatW(), L"System Error", MB_ICONWARNING);
		return (T)NULL;
	}
	return x;
}

/* Return integer scaled for current DPI. */
inline int Dpi(const int i)
{
	extern int g_dpi;
	return MulDiv(i, g_dpi, 96);
}

inline int Cmp(const int a, const int b)
{
	if (a == b) return 0;
	if (a > b) return 1;
	return -1;
}

/* Get window rectangle relative to parent. */
inline BOOL GetRelativeRect(const HWND hWnd, RECT* const rr)
{
	if (!GetClientRect(hWnd, rr)) return 0;
	return MapWindowPoints(hWnd, GetParent(hWnd), (POINT*)rr, 2);
}

inline BOOL SetWindowRect(const HWND hWnd,
    const long left, const long top, const long right, const long bottom)
{
	return MoveWindow(hWnd,
	    left, top,
	    right-left, bottom-top,
	    TRUE);
}

inline BOOL SetWindowRect(const HWND hWnd, const RECT r)
{
	return SetWindowRect(hWnd, r.left, r.top, r.right, r.bottom);
}

#endif