#include "Logging.h"

#if UNICODE
template class myLogging::Logger<std::wstring, std::wofstream>;
#else
template class myLogging::Logger<std::string, std::ofstream>;
#endif

template <class A, class B>
myLogging::Logger<A, B>::~Logger()
{
	if (_isLogging)
	{
		_lastEntry();
		if (_file.is_open()) _file.close();
	}
	_isLogging = false;
}

template <class A, class B>
bool myLogging::Logger<A, B>::Init(A path)
{
	
	_file.open(path);
	if (!_file.is_open()) return false;

	_isLogging = true;
	_path = path;

	_file << TEXT("Date,") << TEXT("Type,") << TEXT("Temperature,") << std::endl;

	if (_file.is_open()) _file.close();
}


template <class A, class B>
void myLogging::Logger<A, B>::Logg(int eOut, A str)
{
	if (_isLogging == false) return;

	_file.open(_path, std::fstream::app);
	if (!_file.is_open()) return;

	if (_CheckDay() == false)
	{
		_file.close();
		_lastEntry();

		std::time_t result = std::time(nullptr);
		#if UNICODE
			size_t find = _path.find_last_of(TEXT("\\"));
			TCHAR* txt = _wctime(&result);
			int n = lstrlen(txt);
			txt[--n] = TEXT('\0');

			std::wstring res = txt;
			std::replace(res.begin(), res.end(), ' ', '_');
			std::replace(res.begin(), res.end(), ':', '_');

			_path = _path.substr(0,find);
			_path += L"\\" + res + SUFFIX_FOR_LOGGER;

		#else
			TCHAR txt[MAX_PATH];
			ctime_s(txt, MAX_PATH, &result);
			int n = strlen(txt);
			txt[--n] = '\0';

			std::string res = txt;
			std::replace(res.begin(), res.end(), ' ', '_');
			std::replace(res.begin(), res.end(), ':', '_');

			size_t find = _path.find_last_of("\\");

			_path = _path.substr(0, find);
			_path += "\\" + res + SUFFIX_FOR_LOGGER;
		#endif

		_file.open(_path, std::ios::out);
		if (!_file.is_open()) return;
		_file << TEXT("Date,") << TEXT("Type,") << TEXT("Temperature,") << std::endl;
	}


	#if UNICODE
		std::time_t result = std::time(nullptr);

		std::wstring res = _wctime(&result);
		res = res.substr(0, res.size() - 1);

		_file << res.c_str() << L",";

		if (eOut == (int)Output::info)
		{
			_file << L"info," << str <<  L"," << std::endl;
		}

		else if (eOut == (int)Output::warn)
		{
			_file << L"warning," << str <<  L"," << std::endl;
			++_febrile;
		}
		++_total;
	#else
		std::time_t result = std::time(nullptr);
		TCHAR txt[MAX_PATH];
		ctime_s(txt, MAX_PATH, &result);

		std::string res = txt;
		res = res.substr(0, res.size()-1);

		_file << res.c_str() << ",";

		if (eOut == (int)Output::info)
		{
			_file << "info," << str << "," <<std::endl;
		}

		else if (eOut == (int)Output::warn)
		{
			_file << "warning," << str << "," << std::endl;
			++_febrile;
		}
		++_total;
	#endif

	if (_file.is_open()) _file.close();
}

template <class A, class B>
bool myLogging::Logger<A, B>::_CheckDay()
{
	#if UNICODE
		std::time_t result = std::time(nullptr);
		std::wstring res = _wctime(&result);

		size_t find = 0;
		find = res.find(TEXT(" "));
		std::wstring dayPrev = res.substr(0, find);

		find = _path.find(TEXT("_"));
		std::wstring dayCurrent = _path.substr(0, find);
		find = _path.find_last_of(TEXT("\\"));

		dayCurrent = dayCurrent.substr(find);

		if (dayPrev.compare(dayCurrent))
			return true;
		else
			return false;

	#else
		std::time_t result = std::time(nullptr);
		TCHAR txt[MAX_PATH];
		ctime_s(txt, MAX_PATH, &result);

		std::string res = txt;

		size_t find = 0;
		find = res.find(" ");
		std::string dayPrev = res.substr(0,find);
		
		find = _path.find("_");
		std::string dayCurrent = _path.substr(0, find);
		find = _path.find_last_of("\\");

		dayCurrent = dayCurrent.substr(find);

		if (dayPrev.compare(dayCurrent))
			return true;
		else
			return false;

	#endif
	

	return false;
}

template <class A, class B>
void myLogging::Logger<A, B>::_lastEntry()
{
	if (_isLogging == false) return;

	_file.open(_path, std::fstream::app);
	if (!_file.is_open()) return;

	#if UNICODE
		_file << L"Febrile," << _febrile << L"," << std::endl;
		_file << L"Total," << _total << L"," << std::endl;
	#else
		_file << "Febrile," << _febrile << "," << std::endl;
		_file << "Total," << _total << "," << std::endl;
	#endif
	_febrile = 0;
	_total = 0;

	if (_file.is_open()) _file.close();
}