//----------------------------------------------------------------------------------------------
// <copyright file="SBGPCTDProgramSettings.h" company="LiquidRobotics">
//
// Copyright (c) 2014 Liquid Robotics, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
// This software is associated with a release.  Liquid Robotics makes no
// representation that the design, interfaces or component distribution will
// remain unchanged in future releases.
//
// </copyright>
//
// <summary>
// 	Declaration of SBGPCTDProgramSettings, an serializable entity class that contains the config
//  settings for an instance of SBGPCTDProgram
// </summary>
//
// <owner>Mike Cookson</owner>
//----------------------------------------------------------------------------------------------

#ifndef SBGPCTDPROGRAMSETTINGS_H_
#define SBGPCTDPROGRAMSETTINGS_H_

#include "SBGPCTDControllerSettings.h"
#include "SensorProgramSettings.h"
#include <boost/serialization/access.hpp>
#include <boost/serialization/nvp.hpp>
#include <boost/serialization/version.hpp>
#include "LqrSMACSharedLib.h"
#include <boost/shared_ptr.hpp>
#include <string>

namespace LiquidRobotics {
namespace SeabirdGPCTD {

/*! \brief A serializable entity that holds the runtime-configurable settings for SBGPCTDProgram
 *  \sa SBGPCTDProgram
 */
class SHARED_LIB_PUBLIC SBGPCTDProgramSettings
{
public:

	//! Define a short-cut for EventLib's EventSettings class (don't want "using namespace" in header files)
	typedef LiquidRobotics::EventLib::EventSettings EventSettings;

	//! Define a short-cut for EventLib's PayloadManagerClientSettings class (don't want "using namespace" in header files)
	typedef LiquidRobotics::EventLib::PayloadManagerClientSettings PayloadManagerClientSettings;

	//! Define a short-cut for EventLib's SensorProgramSettings class (don't want "using namespace" in header files)
	typedef LiquidRobotics::EventLib::SensorProgramSettings SensorProgramSettings;

	//! Define a short-cut for SMACFramework's StdOutLoggerSettings class (don't want "using namespace" in header files)
	typedef LiquidRobotics::SMACFramework::StdOutLoggerSettings StdOutLoggerSettings;

	//! Define a short-cut for string (don't want "using namespace" in header files)
	typedef std::string string;

	/*! \brief Class constructor
	 *
	 *  Sets default values for various settings
	 */
	SBGPCTDProgramSettings();

	/*! \brief Class destructor
	 */
	virtual ~SBGPCTDProgramSettings();

	/*! \brief Get the Controller settings
	 *  \returns A read-only reference on the local copy of the Controller settings
	 *  \sa SetControllerSettings()
	 *
	 *  Controller settings are passed to the Controller during construction
	 */
	const SBGPCTDControllerSettings& GetControllerSettings() const;

	/*! \brief Set a new value for the Controller settings
	 *  \sa GetControllerSettings()
	 *
	 *  Controller settings are passed to the Controller during construction
	 */
	void SetControllerSettings(
			const SBGPCTDControllerSettings& newValue	/**< [in] A read-only reference on the new Controller settings to be saved */
			);

	/*! \brief Get the Sensor Program settings
	 *  \returns A read-only reference on the local copy of the Sensor Program settings
	 *  \sa SetSensorProgramSettings()
	 *
	 *  Sensor Program settings are passed to the SensorProgram base class of
	 *  SBGPCTDProgram during construction
	 */
	const SensorProgramSettings& GetSensorProgramSettings() const;

	/*! \brief Set a new value for the Sensor Program settings
	 *  \sa GetSensorProgramSettings()
	 *
	 *  Sensor Program settings are passed to the SensorProgram base class of
	 *  SBGPCTDProgram during construction
	 */
	void SetSensorProgramSettings(
			const SensorProgramSettings& newValue		/**< [in] A read-only reference on the new Sensor Program settings to be saved */
			);

	/*! \brief Get the name, type and domain of the event to send when a sample is read without O2
	 *  \returns A read-only reference on an EventSettings instance that holds the name, type and domain to be used when sending a sample CORBA event (without O2)
	 *  \sa GetO2SampleEventSettings(), SetO2SampleEventSettings(), SetSampleEventSettings(), SBGPCTDSampleEvent
	 */
	const EventSettings& GetSampleEventSettings() const;

	/*! \brief Set a new value for the name, type and domain of the event to send when a sample is read without O2
	 *  \sa GetO2SampleEventSettings(), SetO2SampleEventSettings(), GetSampleEventSettings(), SBGPCTDSampleEvent
	 */
	void SetSampleEventSettings(
			const EventSettings& newValue		/**< [in] A read-only reference on an EventSettings to be saved */
			);

	/*! \brief Get the format code to be used in telemetry for data samples without O2
	 * \returns The format code to be used in telemetry for data samples without O2
	 *  \sa GetO2SampleFormatCode(), SetSampleFormatCode(), SetO2SampleFormatCode()
	 */
	unsigned GetSampleFormatCode() const;

	/*! \brief Set a new value for the format code to be used in telemetry for data samples without O2
	 *  \sa GetSampleFormatCode(), GetO2SampleFormatCode(), SetO2SampleFormatCode()
	 */
	void SetSampleFormatCode(
			unsigned newValue			/**< [in] The new format code to be saved */
			);

	/*! \brief Get the name, type and domain of the event to send when a sample is read with O2
	 *  \returns A read-only reference on an EventSettings instance that holds the name, type and domain to be used when sending a sample CORBA event (with O2)
	 *  \sa GetSampleEventSettings(), SetSampleEventSettings(), SetO2SampleEventSettings(), SBGPCTDSampleEvent
	 */
	const EventSettings& GetO2SampleEventSettings() const;

	/*! \brief Set a new value for the name, type and domain of the event to send when a sample is read with O2
	 *  \sa GetSampleEventSettings(), SetSampleEventSettings(), GetO2SampleEventSettings(), SBGPCTDSampleEvent
	 */
	void SetO2SampleEventSettings(
			const EventSettings& newValue		/**< [in] A read-only reference on an EventSettings to be saved */
			);

	/*! \brief Get the format code to be used in telemetry for data samples with O2
	 *  \returns The format code to be used in telemetry for data samples with O2
	 *  \sa GetSampleFormatCode(), SetSampleFormatCode(), SetO2SampleFormatCode()
	 */
	unsigned GetO2SampleFormatCode() const;

	/*! \brief Set a new value for the format code to be used in telemetry for data samples with O2
	 *  \sa GetSampleFormatCode(), GetO2SampleFormatCode(), SetSampleFormatCode()
	 */
	void SetO2SampleFormatCode(unsigned newValue);

	/*! \brief Get the name, type and domain of the event to send when there is a change in telemetry reporting parameters
	 *  \returns A read-only reference on an EventSettings instance that holds the name, type and domain to be used when reporting a change in telemetry reporting parameters
	 *  \sa SetTelemetryChangedEventSettings()
	 */
	const EventSettings& GetTelemetryChangedEventSettings() const;

	/*! \brief Set a new value for the name, type and domain of the event to send when there is a change in telemetry reporting parameters
	 *  \sa GetTelemetryChangedEventSettings()
	 */
	void SetTelemetryChangedEventSettings(const EventSettings& newValue);

	/*! \brief Get the settings for PayloadManagerClient
	 *  \returns A read-only reference to the local copy of PayloadManagerClient settings
	 *  \sa LiquidRobotics::EventLib::BoilerplateCORBAServer
	 *
	 *  PayloadManagerClient gives access to PayloadManager, which is used to
	 *  advertise parameters about the server.
	 *
	 *  This is provided for use with BoilerplateCORBAServer
	 */
	const PayloadManagerClientSettings& GetPayloadManagerClientSettings() const { return GetSensorProgramSettings().GetPayloadManagerClientSettings(); }

	/*! \brief Get the settings for StdOutLogger
	 *  \returns A read-only reference to the local copy of StdOutLoggerSettings
	 *  \sa LiquidRobotics::EventLib::BoilerplateCORBAServer
	 *
	 *  StdOutLogger derived class that is referenced as Logger through its
	 *  base class; it provides log formatting tools, verbosity controls and
	 *  date/time stamp.
	 *
	 *  This is provided for use with BoilerplateCORBAServer
	 */
	const StdOutLoggerSettings& GetLoggerSettings() const { return GetSensorProgramSettings().GetLoggerSettings(); }

	/*! \brief Reads the kind of the CORBA server that will be advertised for this CORBA service
	 *  \returns A read-only reference to the local copy of the string
	 *  \sa LiquidRobotics::EventLib::BoilerplateCORBAServer, LiquidRobotics::SMACFramework::IObjectBroker::AdvertiseInstance()
	 *
	 *  This is provided for use with BoilerplateCORBAServer
	 */
	const string& GetServerKind() const { return GetSensorProgramSettings().GetServerKind(); }

	/*! \brief Reads the name of the CORBA server that will be advertised for this CORBA service
	 *  \returns A read-only reference to the local copy of the string
	 *  \sa LiquidRobotics::EventLib::BoilerplateCORBAServer, LiquidRobotics::SMACFramework::IObjectBroker::AdvertiseInstance()
	 *
	 *  This is the name that is provided to ServerExec to create a proxy to
	 *  this service.
	 *
	 *  This is provided for use with BoilerplateCORBAServer
	 */
	const string& GetServerName() const { return GetSensorProgramSettings().GetServerName(); }

	/*! \brief Load the settings from the XML file at the given path
	 *  \sa Save(const string& filenName), load()
	 *
	 *  An exception is thrown if the expected file does not exist.  If it exists, but
	 *  it is empty (zero length), the default format is written: this is helpful when
	 *  changes in the schema have occurred, or the schema is not known.  This method
	 *  also compares the file to what would be written with the Save method variants,
	 *  and if there is a difference, the new format is written to a ".upgraded" file.
	 */
	void Load(
			const string& fileName		/**< [in] The name/path of the XML file to load from */
			);

	/*! \brief Save the settings to the XML file at the given path
	 *  \sa Load(const string& fileName), save()
	 */
	void Save(
			const string& fileName		/**< [in] The name/path of the XML file to save to */
			) const;

private:

	//! Allows the Boost Serialization library to access private load() and save()
	friend class boost::serialization::access;

	/*! \brief Boost serialization method for reading data from an archive file
	 *  \sa Load(const string& fileName), save(), BOOST_CLASS_VERSION()
	 *
	 *  The archive type is kept generic, but this method was created for use with
	 *  an XML archive.  The load() function can inspect the version variable and
	 *  customize its behavior to remain backward-compatible.
	 */
	template<class Archive>
	void load(
			Archive & ar,						/**< [in] Reference on the archive to load from */
			const unsigned int version		/**< [in] Version of the schema being read */
			)
	{
		ar & BOOST_SERIALIZATION_NVP(sensorProgramSettings);
		ar & BOOST_SERIALIZATION_NVP(sampleEventSettings);
		ar & BOOST_SERIALIZATION_NVP(sampleFormatCode);
		ar & BOOST_SERIALIZATION_NVP(o2SampleEventSettings);
		ar & BOOST_SERIALIZATION_NVP(o2SampleFormatCode);
		ar & BOOST_SERIALIZATION_NVP(telemetryChangedEventSettings);
		ar & BOOST_SERIALIZATION_NVP(controllerSettings);
	}

	/*! \brief Boost Serialization library compatible method for storing data to an archive file
	 *  \sa Save(const string& fileName), load(), BOOST_CLASS_VERSION()
	 *
	 *  The archive type is kept generic, but this method was created for use with
	 *  an XML archive.  The save() function always writes the latest version of
	 *  of the schema.
	 */
	template<class Archive>
	void save(
			Archive & ar, 						/**< [in] Reference on the archive to save to */
			const unsigned int version		/**< [in] Version of the schema being written */
			) const
	{
		ar & BOOST_SERIALIZATION_NVP(sensorProgramSettings);
		ar & BOOST_SERIALIZATION_NVP(sampleEventSettings);
		ar & BOOST_SERIALIZATION_NVP(sampleFormatCode);
		ar & BOOST_SERIALIZATION_NVP(o2SampleEventSettings);
		ar & BOOST_SERIALIZATION_NVP(o2SampleFormatCode);
		ar & BOOST_SERIALIZATION_NVP(telemetryChangedEventSettings);
		ar & BOOST_SERIALIZATION_NVP(controllerSettings);
	}

	//! Inidcates to use separate load() and save() methods instead of a single serialize() method;
	//! this is needed particularly if there are multiple schemas to detect during load
	BOOST_SERIALIZATION_SPLIT_MEMBER()

	EventSettings sampleEventSettings;				//!< Name, type and domain of the event that should report data samples without O2
	unsigned sampleFormatCode;						//!< Telemetry format code for samples without O2
	EventSettings o2SampleEventSettings;			//!< Name, type and domain of the event that should report data samples with O2
	unsigned o2SampleFormatCode;					//!< Telemetry format code for samples with O2
	EventSettings telemetryChangedEventSettings;	//!< Name, type and domain of the event that should changes in telemetry reporting parameters
	SensorProgramSettings sensorProgramSettings;	//!< Settings for the base class (ProgramSettings)
	SBGPCTDControllerSettings controllerSettings;	//!< Settings for the controller
};

}
}

//! Indicates which version of the schema is the latest (what is written with save())
BOOST_CLASS_VERSION(LiquidRobotics::SeabirdGPCTD::SBGPCTDProgramSettings, 1)

#endif /* SBGPCTDPROGRAMSETTINGS_H_ */
