#include "TIMImager.h"

PIseries::TIMImager::TIMImager()
{
	_initPrivateAttributes();
}

PIseries::TIMImager::TIMImager(const char* xmlpath)
{
	_initPrivateAttributes();

	if (setXMLPath(xmlpath)) _xmlIsSet = true;
}

bool PIseries::TIMImager::Initialize()
{
	if (_isInit) return true;

	if (_isXMLpathSet() == false)
	{
		char path[MaxSize];
		_initialize(path);
	}

	if (_setIRDeviceParams(&_params) == false) return false;

	_device = evo::IRDeviceDS::createInstance(_params);

	if (_device == NULL) return false;
	

	if(_irimager == NULL) _irimager = new evo::IRImager();
	if (_irimager->init(&_params, _device->getFrequency(), _device->getWidth(), _device->getHeight()))
	{
		setWidth(_irimager->getWidth());
		setHeight(_irimager->getHeight());

		if (_irimager_handler == NULL)
		{
			_irimager_handler = new ImagerHandler(_irimager);
			if (!_irimager_handler) return false;
			if (!_irimager_handler->LoadDataHandler(&thermalImage)) return false;
			_device->setClient(_irimager_handler);
			_clientForDeviceWasSetted = true;

		}
		else return false;
	}
	else return false;

	_isInit = true;
	return true;
}

bool PIseries::TIMImager::StartStreaming()
{
	if (!_isInit) return false;
	if (!_device) return false;


	int r = _device->startStreaming();

	return (r < 0) ? false : true;
}

bool PIseries::TIMImager::StopStreaming()
{
	if (!_isInit) return false;
	if (!_device) return false;

	_device->stopStreaming();

	return true;
}

bool PIseries::TIMImager::_initialize(char* path)
{
	OPENFILENAME ofn;
	char filename[100];

	ZeroMemory(&ofn, sizeof(OPENFILENAME));

	char txt[] =
		"All files\0*.*\0"
		"XML Files\0*.XML\0\0";

	ofn.lStructSize = sizeof(OPENFILENAME);
	ofn.lpstrTitle = TEXT("Choose xml file");
	ofn.hwndOwner = NULL;
	ofn.lpstrFile = path;
	ofn.lpstrFile[0] = '\0';
	ofn.nMaxFile = 100;
	ofn.lpstrFilter = txt;
	ofn.nFilterIndex = 2;

	GetOpenFileName(&ofn);

	if (path)
	{
		setXMLPath(path);
		return true;
	}
	else
	{
		return false;
	}
}

bool PIseries::TIMImager::setXMLPath(const char* xmlpath)
{
	if (_setXMLPath(xmlpath))
	{
		if (_setIRDeviceParams(&_params)) return true;
	}
	return false;
}

bool PIseries::TIMImager::_setXMLPath(const char* src)
{
	size_t argSize = MaxSize;

	#if UNICODE	
		size_t OutSize = 0;
		mbstowcs_s(&OutSize, _xmlPath, src, argSize);
		return (OutSize > 0) ? true : false;
	#else 
		int success = strcpy_s(_xmlPath, argSize, src);
		return (success == 0) ? true : false;
	#endif
}

bool PIseries::TIMImager::_setIRDeviceParams(evo::IRDeviceParams* params)
{
	#if not UNICODE
		size_t OutSize = 0;
		WCHAR xmlPath[MaxSize];
		mbstowcs_s(&OutSize, xmlPath, _xmlPath, MaxSize);
		if (OutSize == 0) return false;
	#endif

	#if not UNICODE 
		return (evo::IRDeviceParamsReader::readXML(xmlPath, *params)) ? true : false;
	#else		
		return (evo::IRDeviceParamsReader::readXML(_xmlPath, *params)) ? true : false;
	#endif
}

void PIseries::TIMImager::_initPrivateAttributes()
{
	_isInit = false;
	_xmlIsSet = false;
	_clientForDeviceWasSetted = false;
	_irimager = NULL;
	_device = NULL;
	_irimager_handler = NULL;
	_params = { 0,0,NULL,NULL,NULL,0,0,0,0,0,0,0,0.f,0.f,0,0,0.f };
	_xmlPath[0] = { '\0' };
}

bool PIseries::TIMImager::_isXMLpathSet()
{
	return (_xmlIsSet) ? true : false;
}
