#include "Record.h"


/////////////////////////////////// Methods /////////////////////////////

void thermoCamera::Record::startRecord(unsigned int number)
{
	if (_eState == State::write)
	{
		if (!_outPath) throw MyError::notInit;
	}
	if (_eState == State::read)
	{
		if (!_inputPath) throw MyError::notInit;
	}

	switch (_eFlag)
	{
	case thermoCamera::Flag::play:
		break;
	case thermoCamera::Flag::pause:
		_eFlag = Flag::play;
		break;
	case thermoCamera::Flag::stop:
		_eFlag = Flag::play;
		setImageNumber(number);
		break;
	default:
		break;
	}
}

void thermoCamera::Record::stopRecord()
{
	setImageNumber(0);
	switch (_eFlag)
	{
	case thermoCamera::Flag::play:
		_eFlag = Flag::stop;
		break;
	case thermoCamera::Flag::pause:
		_eFlag = Flag::stop;
		break;
	case thermoCamera::Flag::stop:
		break;
	default:
		break;
	}
}

void thermoCamera::Record::pauseRecord()
{
	switch (_eFlag)
	{
	case thermoCamera::Flag::play:
		_eFlag = Flag::pause;
		break;
	case thermoCamera::Flag::pause:
		break;
	case thermoCamera::Flag::stop:
		break;
	default:
		break;
	}
}

void thermoCamera::Record::UpdateName()
{
	setImageNumber(_number);
}

/////////////////////////////////// Setters ///////////////////////////////////

void thermoCamera::Record::setFormatOfImage(const TCHAR* format)
{
	if (!format) throw MyError::badInput;

	#if UNICODE
		_format = new TCHAR[wcslen(format) + 1];
		wcscpy_s(_format, wcslen(format) + 1, format);
	#else
		_format = new TCHAR[strlen(format) + 1];
		strcpy_s(_format, strlen(format) + 1, format);
	#endif
}

void thermoCamera::Record::setInputPath(const TCHAR* inputPath)
{
	if (!inputPath) throw MyError::badInput;
	#if UNICODE
		_inputPath = new TCHAR[wcslen(inputPath) + 1];
		wcscpy_s(_inputPath, wcslen(inputPath) + 1, inputPath);
	#else
		_inputPath = new TCHAR[strlen(inputPath) + 1];
		strcpy_s(_inputPath, strlen(inputPath) + 1, inputPath);
	#endif
	_RecognizeNumber(_inputPath);
}

void thermoCamera::Record::setOutputPath(const TCHAR* outPath)
{
	if (!outPath) throw MyError::badInput;
	#if UNICODE
		_outPath = new TCHAR[wcslen(outPath) + 1];
		wcscpy_s(_outPath, wcslen(outPath) + 1, outPath);
	#else
		_outPath = new TCHAR[strlen(outPath) + 1];
		strcpy_s(_outPath, strlen(outPath) + 1, outPath);
	#endif

}

void thermoCamera::Record::setImageNumber(const unsigned int number)
{
	if(_eState == State::write) _number = number;
	_UpdateCurrentNameImage();
}

/////////////////////////////////// Getters ///////////////////////////////////

const TCHAR* thermoCamera::Record::getCurrentPath()
{
	if (getState() == State::read)
	{
	#if UNICODE
			if (_inputPath) return _inputPath;
			else L"Empty";
	#else
			if (_inputPath) return _inputPath;
			else "Empty";
	#endif
		
	}
	if (getState() == State::write)
	{
	#if UNICODE
			if (_outPath) return _outPath;
			else L"Empty";
	#else
			if (_outPath) return _outPath;
			else "Empty";
	#endif
		
	}

	return NULL;
}

const TCHAR* thermoCamera::Record::getCurrentImagePath()
{
	if (_nameImage) return _nameImage;
	else
		return TEXT("Empty");
}

/////////////////////////////////// Private methods ///////////////////////////////////

void thermoCamera::Record::_UpdateCurrentNameImage()
{
	if (_nameImage) delete[] _nameImage;

	if (_eState == State::write)
	{
		if (_outPath && _format)
		{
			_SetNameAndIncreaseNumber(_outPath);
		}
	}
	else
	{
		if (_inputPath && _format)
		{
			_SetNameAndIncreaseNumber(_inputPath);
		}
	}
}

void thermoCamera::Record::_SetNameAndIncreaseNumber(TCHAR* name)
{
	TCHAR* num = new TCHAR[MAX_PATH];

	#if UNICODE
		swprintf_s(num, MAX_PATH, TEXT("%d"), _number);
	#else
		snprintf(num, sizeof(num), TEXT("%d"), _number);
	#endif

	#if UNICODE
		size_t size = wcslen(name) + wcslen(num) + wcslen(_format) + 1;
		_nameImage = new TCHAR[size];

		wcscpy_s(_nameImage, wcslen(name) + 1, name);
		wcscat_s(_nameImage, size, num);
		wcscat_s(_nameImage, size, _format);
	#else
		size_t size = strlen(name) + strlen(num) + strlen(_format) + 1;
		_nameImage = new TCHAR[size];

		strcpy_s(_nameImage, strlen(name) + 1, name);
		strcat_s(_nameImage, size, num);
		strcat_s(_nameImage, size, _format);
	#endif
		if (num) { delete num; num = NULL; }
	++_number;
}

void thermoCamera::Record::_RecognizeNumber(const TCHAR* path)
{
	if (!path) throw MyError::badInput;
#if UNICODE
	std::wstring str(path);
	// find last occurrence of path separator
	size_t found = str.find_last_of(TEXT("/\\"));
	// split off filename part of string
	std::wstring strFind = str.substr(found + 1);
	found = strFind.find_last_of(TEXT("."));
	strFind = strFind.substr(0, found);

	// set number
	_number = _wtoi(strFind.c_str());

	// set input path
	found = str.find_last_of(TEXT("/\\"));
	str = str.substr(0, found);
	str += TEXT("\\");
	const TCHAR* p = str.c_str();
	wcscpy_s(_inputPath, wcslen(p) + 1, p);
#else
	std::string str(path);
	// find last occurrence of path separator
	size_t found = str.find_last_of(TEXT("/\\"));
	// split off filename part of string
	std::string strFind = str.substr(found + 1);
	found = strFind.find_last_of(TEXT("."));
	strFind = strFind.substr(0, found);

	// set number
	_number = atoi(strFind.c_str());

	// set input path
	found = str.find_last_of(TEXT("/\\"));
	str = str.substr(0, found);
	str += TEXT("\\");
	const TCHAR* p = str.c_str();
	strcpy_s(_inputPath, strlen(p) + 1, p);
#endif
	
}