#include "BMPRecord.h"

/////////////////////////////////// Constructors /////////////////////////////

thermoCamera::BMPRecord::BMPRecord()
{
	setState(State::read);
	_Init();
}

thermoCamera::BMPRecord::BMPRecord(int mode)
{
	if (mode != 0 && mode != 1) throw MyError::badInput;

	if (mode == 0) setState(State::read);
	if (mode == 1) setState(State::write);

	_Init();
}

thermoCamera::BMPRecord::BMPRecord(int mode, const TCHAR* path)
{
	if (mode != 0 && mode != 1) throw MyError::badInput;
	if(!path) throw MyError::badInput;

	if (mode == 0) 
	{
		setState(State::read);
		setInputPath(path);
	}
	if (mode == 1) 
	{
		setState(State::write);
		setOutputPath(path);
	}
	
	_Init();
}

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

void thermoCamera::BMPRecord::Save(HBITMAP hBitMap)
{
	if (!_nameImage) throw MyError::notInit;
	if (!hBitMap) throw MyError::badInput;
	const int num = 200;
	CImage image;
	TCHAR path[num];

	#ifdef  UNICODE
		wcscpy_s(path, sizeof(path), (TCHAR*)_nameImage);
		image.Attach(hBitMap);
		image.Save(path);
	#else
		strcpy_s(path, sizeof(path), (TCHAR*)_nameImage);
		LPCTSTR pszCharacterString = (LPCTSTR)_nameImage;
		image.Attach(hBitMap);
		image.Save(pszCharacterString);
	#endif 


	UpdateName();
}

void thermoCamera::BMPRecord::Load()
{
	UpdateName();
}

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

void thermoCamera::BMPRecord::SetPath(const TCHAR* path)
{
	if (!path) throw MyError::badInput;

	if (getState() == State::read) 
	{
		setInputPath(path);
	}
	if (getState() == State::write) setOutputPath(path);
}

void thermoCamera::BMPRecord::SetMetod(int mode)
{
	if (mode != 0 && mode != 1) throw MyError::badInput;

	if (mode == 0) setState(State::read);
	if (mode == 1) setState(State::write);
}

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

void thermoCamera::BMPRecord::_Init()
{
	SetFormat();
	setImageNumber(1);
}

void thermoCamera::BMPRecord::SetFormat()
{
	#if UNICODE
		const TCHAR* format = L".bmp";
	#else
		const TCHAR* format = ".bmp";
	#endif
	
	setFormatOfImage(format);
}

