#include "mutil.h"

#include <Windows.h>
#include <shellapi.h>
#include <commdlg.h>
#include <shtypes.h>
#include <shobjidl_core.h>
#include <shlobj_core.h>

#include <sstream>
#include <string>
#include <iomanip>
#include <codecvt>
#include <filesystem>
#include <iostream>


namespace mutil {
	std::vector<uint8_t> from_hex_string(std::string string)
	{
		std::vector<uint8_t> ret;
		auto HexCharToByte = [](char ch)
			{
				if (ch <= 0x40)
					return ch - 48;
				else
					return ch - 55;
			};

		for (int i = 0; i < string.length(); i += 2)
		{
			char firstchar = HexCharToByte(string[i]);
			char secondchar = HexCharToByte(string[i + 1]);
			char result = (16 * firstchar) | secondchar;
			ret.push_back(result);
		}
		return ret;
	}

	std::string to_hex_string(uint8_t* barray, int length)
	{
		if (barray == nullptr || length == 0)
			return std::string();

		std::stringstream stream;
		for (size_t i = 0; i < length; i++)
			stream << std::setfill('0') << std::setw(2) << std::hex << (int)barray[i];

		return stream.str();
	}

	bool IsLittleEndian()
	{
		unsigned int i = 1;
		char* c = (char*)&i;
		return (*c);
	}

	static std::filesystem::path _currentPath;

	void SetCurrentPath(const std::filesystem::path& current_path)
	{
		_currentPath = current_path;
	}

	std::filesystem::path GetCurrentPath()
	{
		return _currentPath;
	}

	std::optional<std::string> SelectDirectory(const char* title)
	{
		auto currPath = std::filesystem::current_path();

		if (!SUCCEEDED(CoInitialize(nullptr)))
			return {};

		IFileDialog* pfd;
		if (!SUCCEEDED(CoCreateInstance(CLSID_FileOpenDialog, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&pfd))))
			return {};

		const size_t titleSize = strlen(title) + 1;
		wchar_t* wcTitle = new wchar_t[titleSize];
		mbstowcs(wcTitle, title, titleSize);

		DWORD dwOptions;
		IShellItem* psi;
		if (!SUCCEEDED(pfd->GetOptions(&dwOptions)) ||
			!SUCCEEDED(pfd->SetOptions(dwOptions | FOS_PICKFOLDERS)) ||
			!SUCCEEDED(pfd->SetTitle(wcTitle)) ||
			!SUCCEEDED(pfd->Show(NULL)) ||
			!SUCCEEDED(pfd->GetResult(&psi)))
		{
			pfd->Release();
			return {};
		}

		WCHAR* folderName;
		if (!SUCCEEDED(psi->GetDisplayName(SIGDN_DESKTOPABSOLUTEPARSING, &folderName)))
		{
			pfd->Release();
			psi->Release();
			return {};
		}

		pfd->Release();
		psi->Release();

		std::filesystem::current_path(currPath);

		std::u16string u16(reinterpret_cast<const char16_t*>(folderName));
		return std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t>{}.to_bytes(u16);
	}

	std::optional<std::string> SelectFile(const char* filter, const char* title)
	{
		auto currPath = std::filesystem::current_path();

		// common dialog box structure, setting all fields to 0 is important
		OPENFILENAME ofn = { 0 };
		TCHAR szFile[260] = { 0 };

		// Initialize remaining fields of OPENFILENAME structure
		ofn.lStructSize = sizeof(ofn);
		ofn.hwndOwner = NULL;
		ofn.lpstrFile = szFile;
		ofn.nMaxFile = sizeof(szFile);
		ofn.lpstrFilter = filter;
		ofn.lpstrTitle = title;
		ofn.nFilterIndex = 1;
		ofn.lpstrFileTitle = NULL;
		ofn.nMaxFileTitle = 0;
		ofn.lpstrInitialDir = NULL;
		ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;

		std::optional<std::string> result = {};
		if (GetOpenFileName(&ofn) == TRUE)
			result = std::string(szFile);

		std::filesystem::current_path(currPath);
		return result;
	}


	int64_t GetCurrentTimeMillisec()
	{
		return std::chrono::duration_cast<std::chrono::milliseconds>(
			std::chrono::system_clock::now().time_since_epoch()).count();
	}

	std::vector<std::string> StringSplit(const std::string& delimiter, const std::string& content)
	{
		std::vector<std::string> tokens;
		size_t pos = 0;
		size_t prevPos = 0;
		std::string token;
		while ((pos = content.find(delimiter, prevPos)) != std::string::npos)
		{
			token = content.substr(prevPos, pos - prevPos);
			tokens.push_back(token);
			prevPos = pos + delimiter.length();
		}
		tokens.push_back(content.substr(prevPos));
		return tokens;
	}

	std::string SplitWords(const std::string& value)
	{
		std::stringstream outStream;
		std::stringstream inStream(value);

		char ch;
		inStream >> ch;
		outStream << ch;
		while (inStream >> ch)
		{
			if (isupper(ch))
				outStream << " ";
			outStream << ch;
		}
		return outStream.str();
	}

	std::string Unsplit(const std::string& value)
	{
		std::stringstream in(value);
		std::stringstream out;
		std::string word;
		while (in >> word)
			out << word;
		return out.str();
	}

	std::string MakeCapital(std::string value)
	{
		if (islower(value[0]))
			value[0] = toupper(value[0]);
		return value;
	}

	static const std::string base64_chars =
		"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
		"abcdefghijklmnopqrstuvwxyz"
		"0123456789+/";

	static inline bool is_base64(BYTE c)
	{
		return (isalnum(c) || (c == '+') || (c == '/'));
	}

	std::string base64_encode(BYTE const* buf, unsigned int bufLen)
	{
		std::string ret;
		int i = 0;
		int j = 0;
		BYTE char_array_3[3];
		BYTE char_array_4[4];

		while (bufLen--)
		{
			char_array_3[i++] = *(buf++);
			if (i == 3)
			{
				char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
				char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
				char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
				char_array_4[3] = char_array_3[2] & 0x3f;

				for (i = 0; (i < 4); i++)
					ret += base64_chars[char_array_4[i]];
				i = 0;
			}
		}

		if (i)
		{
			for (j = i; j < 3; j++)
				char_array_3[j] = '\0';

			char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
			char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
			char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
			char_array_4[3] = char_array_3[2] & 0x3f;

			for (j = 0; (j < i + 1); j++)
				ret += base64_chars[char_array_4[j]];

			while ((i++ < 3))
				ret += '=';
		}

		return ret;
	}

	std::vector<BYTE> base64_decode(std::string const& encoded_string)
	{
		size_t in_len = encoded_string.size();
		int i = 0;
		int j = 0;
		int in_ = 0;
		BYTE char_array_4[4], char_array_3[3];
		std::vector<BYTE> ret;

		while (in_len-- && (encoded_string[in_] != '=') && is_base64(encoded_string[in_]))
		{
			char_array_4[i++] = encoded_string[in_];
			in_++;
			if (i == 4)
			{
				for (i = 0; i < 4; i++)
					char_array_4[i] = static_cast<BYTE>(base64_chars.find(char_array_4[i])); // base64_chars len < 255

				char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
				char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
				char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];

				for (i = 0; (i < 3); i++)
					ret.push_back(char_array_3[i]);
				i = 0;
			}
		}

		if (i)
		{
			for (j = i; j < 4; j++)
				char_array_4[j] = 0;

			for (j = 0; j < 4; j++)
				char_array_4[j] = static_cast<BYTE>(base64_chars.find(char_array_4[j]));

			char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
			char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
			char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];

			for (j = 0; (j < i - 1); j++) ret.push_back(char_array_3[j]);
		}

		return ret;
	}

	int64_t GetTimezoneBias()
	{
		_TIME_ZONE_INFORMATION timezoneInfo{};
		if (GetTimeZoneInformation(&timezoneInfo) == TIME_ZONE_ID_INVALID)
			LOG_LAST_ERROR("Failed to get timezone.");

		return static_cast<int64_t>(timezoneInfo.Bias) * 60;
	}

	void OpenConsole()
	{
		AllocConsole();
		freopen_s((FILE**)stdout, "CONOUT$", "w", stdout);
		freopen_s((FILE**)stderr, "CONOUT$", "w", stderr);

		auto consoleWindow = GetConsoleWindow();
		SetForegroundWindow(consoleWindow);
		ShowWindow(consoleWindow, SW_RESTORE);
		ShowWindow(consoleWindow, SW_SHOW);
	}

	void OpenURL(const char* url)
	{
		ShellExecute(nullptr, nullptr, url, nullptr, nullptr, SW_SHOW);
	}


	std::string SelectFolder(const std::filesystem::path& dir)
	{
		std::string folderPath;

		IFileDialog* pFileDialog;
		HRESULT hr = CoCreateInstance(CLSID_FileOpenDialog, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&pFileDialog));
		if (SUCCEEDED(hr))
		{
			DWORD options;
			pFileDialog->GetOptions(&options);
			pFileDialog->SetOptions(options | FOS_PICKFOLDERS);

			if (!dir.empty())
			{
				PIDLIST_ABSOLUTE pidl;
				hr = SHParseDisplayName(dir.c_str(), NULL, &pidl, 0, NULL);
				if (SUCCEEDED(hr))
				{
					IShellItem* pDefaultFolder;
					hr = SHCreateShellItem(NULL, NULL, pidl, &pDefaultFolder);
					if (SUCCEEDED(hr))
					{
						pFileDialog->SetFolder(pDefaultFolder);
						pDefaultFolder->Release();
					}
					CoTaskMemFree(pidl);
				}
			}

			hr = pFileDialog->Show(NULL);
			if (SUCCEEDED(hr))
			{
				IShellItem* pItem;
				hr = pFileDialog->GetResult(&pItem);
				if (SUCCEEDED(hr))
				{
					PWSTR pszFolderPath;
					hr = pItem->GetDisplayName(SIGDN_FILESYSPATH, &pszFolderPath);
					if (SUCCEEDED(hr))
					{
						folderPath = std::filesystem::path(pszFolderPath).string();
						CoTaskMemFree(pszFolderPath);
					}
					pItem->Release();
				}
			}

			pFileDialog->Release();
		}

		return folderPath;
	}

	void OpenFolder(const std::filesystem::path& folderPath)
	{
		std::string command = "explorer \"" + folderPath.string() + "\"";

		int result = std::system(command.c_str());

		if (result == 0)
		{
			std::cout << "Successfully opened folder." << std::endl;
		}
		else
		{
			std::cerr << "Failed to open folder." << std::endl;
		}
	}

	void backup_and_rename_file(std::string filepath, bool restore)
	{
		const std::filesystem::path file_path(filepath);
		std::filesystem::path backup_path = file_path.string() + ".bak";
		if (!restore)
		{
			if (exists(file_path))
				std::filesystem::rename(file_path, backup_path);
		}
		else
		{
			if (exists(backup_path))
				std::filesystem::rename(backup_path, file_path);
		}
	}
}
