#include "Utils.hpp"
#include <iostream>
#include <string>

#ifdef _WIN32
#include <Windows.h>
#include <shlobj.h>
#endif

namespace Li {

	std::string Utils::GetDocumentsFolder() {
		std::string documentsFolder = "";

#ifdef _WIN32
		WCHAR documents[MAX_PATH];
		HRESULT result = SHGetFolderPath(NULL, CSIDL_PERSONAL, NULL, SHGFP_TYPE_CURRENT, documents);
		std::wstring wDocuments = std::wstring(documents);
		documentsFolder = std::string(wDocuments.begin(), wDocuments.end());
#else
#error no way to find documents folder on this platform.
#endif

		return documentsFolder;
	}

	bool Utils::CompareStringCaseInsensitive(std::string input, std::string compare) {
		unsigned int sz = input.size();
		if (compare.size() != sz)
			return false;
		for (unsigned int i = 0; i < sz; ++i)
			if (tolower(input[i]) != tolower(compare[i]))
				return false;
		return true;
	}
	std::string Utils::HumanReadableByteStr(uint64_t bytes)
	{
		const char* suffix[5] = { "B", "KB", "MB", "GB", "TB" };
		char length = sizeof(suffix) / sizeof(suffix[0]);

		int i = 0;
		double dblBytes = bytes;

		if (bytes > 1024) {
			for (i = 0; (bytes / 1024) > 0 && i < length - 1; i++, bytes /= 1024)
				dblBytes = bytes / 1024.0;
		}

		static char output[0x8000];
		std::snprintf(output, sizeof(output), "%.02lf %s", dblBytes, suffix[i]);
		return std::string(output);
	}
}