//---------------------------------
// Name:			Configuration.h
// Purpose:			Firmware updater configuration
// Toolchain(s):	Microsoft Visual C++
// Creation date:	26-May-06
// Created by:		JJo
//---------------------------------

#ifndef FIRMWARE_UPDATER_CONFIGURATION_H
#define FIRMWARE_UPDATER_CONFIGURATION_H

//---------------------------------
// Includes

#include "Media/SerialPort.h"
#include "Communication/XonXoff.h"

//---------------------------------
// Classes

class CConfigurationException : public CException {

private:
	CString m_cause;

public:
	CConfigurationException() {}
	CConfigurationException(LPCTSTR cause) : m_cause(cause) {}
	CConfigurationException(const CConfigurationException & e) : m_cause(e.m_cause) {}
	~CConfigurationException() {}

	BOOL GetErrorMessage(LPTSTR lpszError, UINT nMaxError, PUINT pnHelpContext = NULL) {

		if (this->m_cause.GetLength() == 0) {

			return FALSE;
		}

		for (UINT i=0; (this->m_cause.GetAt(i) != '\0') && (i < nMaxError-1); i++) {

			lpszError[i] = this->m_cause.GetAt(i) ;
		}

		if (i == nMaxError-1) {

			lpszError[i] = '\0';
		}

		if (pnHelpContext != NULL) {

			*pnHelpContext = 0;
		}

		return TRUE;
	}

	int ReportError(UINT nType = MB_OK, UINT nMessageID = 0) {

		return AfxMessageBox(this->m_cause, nType, nMessageID);
	}

	static void ThrowError(LPCTSTR cause) {

		throw new CConfigurationException(cause);
	}
};

class Configuration {

private:
	Communication * m_pCommunication;
	Media * m_pMedia;
	CFile * m_pFirmware;

public:
	Configuration::Configuration();
	Configuration::~Configuration();

	void Configuration::SetSerialPort(LPCTSTR name,
									  UINT baudRate,
									  BYTE parity,
									  BYTE stopBits);
	void Configuration::SetXonXoff();
	void Configuration::SetFile(LPCTSTR filename);

	Communication * Configuration::GetCommunication();
	CFile * Configuration::GetFile();

	void Configuration::Parse(LPCTSTR arguments);
	static void Configuration::DisplayOptions();
};

#endif