//------------------------------------------------------------------------------
// <copyright file="GPSListenerSettings.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>
//   Declarations for the GPSListenerSettings class, a serializable entity that
//   holds the configuration values for the GPSListener class
// </summary>
//
// <owner>Mike Cookson</owner>
//------------------------------------------------------------------------------

#ifndef __GPSLISTENERSETTINGS_H__
#define __GPSLISTENERSETTINGS_H__

#include <string>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/serialization/access.hpp>
#include <boost/serialization/nvp.hpp>
#include <boost/serialization/vector.hpp>
#include <boost/serialization/version.hpp>
#include <boost/serialization/split_member.hpp>
#include "EventTransceiver.h"
#include "LqrSMACBooleanParser.h"
#include "LqrSMACSerialPort.h"
#include "LqrSMACSerialPortSettings.h"
#include "LqrSMACSharedLib.h"
#include "LqrSMACStdOutLoggerSettings.h"
#include "PayloadManagerClientSettings.h"

namespace LiquidRobotics {
namespace GPSListenerLib {

class SHARED_LIB_PUBLIC GPSEventSpec
{
public:

	typedef LiquidRobotics::EventLib::EventSettings EventSettings;
	typedef std::string string;

	GPSEventSpec()
	: divisor(5)
	, divisorMode(0)
	, eventSettings("GPSSample", "GPSSample", "LiquidR-SMC")
	{
	}

	GPSEventSpec(time_t _divisor, unsigned _divisorMode, const string& _eventName, const string& _eventTypeName, const string& _eventDomainName)
	: divisor(_divisor)
	, divisorMode(_divisorMode)
	, eventSettings(_eventName, _eventTypeName, _eventDomainName)
	{
	}

	GPSEventSpec(const GPSEventSpec& other)
	: divisor(other.divisor)
	, divisorMode(other.divisorMode)
	, eventSettings(other.eventSettings)
	{
	}

	time_t divisor;
	unsigned divisorMode;
	EventSettings eventSettings;

private:

	friend class boost::serialization::access;

	template<class Archive>
	void load(Archive& ar, const unsigned int version)
	{
		ar & BOOST_SERIALIZATION_NVP(divisor);
		ar & BOOST_SERIALIZATION_NVP(divisorMode);
		ar & BOOST_SERIALIZATION_NVP(eventSettings);
	}

	template<class Archive>
	void save(Archive& ar, const unsigned int version) const
	{
		ar & BOOST_SERIALIZATION_NVP(divisor);
		ar & BOOST_SERIALIZATION_NVP(divisorMode);
		ar & BOOST_SERIALIZATION_NVP(eventSettings);
	}

	BOOST_SERIALIZATION_SPLIT_MEMBER()
};

typedef std::vector<GPSEventSpec> GPSEventSpecArray;

class SHARED_LIB_PUBLIC GPSListenerSettings
{
public:

	typedef std::string string;
	typedef string::size_type size_type;
	typedef LiquidRobotics::EventLib::EventSettings EventSettings;
	typedef LiquidRobotics::EventLib::EventTransceiver::Settings EventTransceiverSettings;
	typedef LiquidRobotics::EventLib::PayloadManagerClientSettings PayloadManagerClientSettings;
	typedef LiquidRobotics::SMACFramework::ISerialPort ISerialPort;
	typedef ISerialPort::TimeSpan TimeSpan;
	typedef LiquidRobotics::SMACFramework::SerialPortSettings SerialPortSettings;
	typedef LiquidRobotics::SMACFramework::StdOutLoggerSettings StdOutLoggerSettings;
	typedef std::vector<unsigned> BaudRateVectorType;

	GPSListenerSettings();
	virtual ~GPSListenerSettings();

	const BaudRateVectorType& GetBaudRates() const;
	void GetBaudRates(BaudRateVectorType& rates) const;
	void SetBaudRates(const BaudRateVectorType& newRates);

	const StdOutLoggerSettings& GetLoggerSettings() const;
	void SetLoggerSettings(const StdOutLoggerSettings& newValue);

	const SerialPortSettings& GetPortSettings() const;
	void SetPortSettings(const SerialPortSettings& newValue);

	const EventTransceiverSettings& GetEventTransceiverSettings() const;
	void SetEventTransceiverSettings(const EventTransceiverSettings& newValue);

	size_type GetMaxString() const;
	void SetMaxString(size_type newValue);

	TimeSpan GetReadTimeout() const;
	void SetReadTimeout(TimeSpan newValue);

	const GPSEventSpecArray& GetGPSEventSpecs() const;
	void SetGPSEventSpecs(const GPSEventSpecArray& newValue);

	const string& GetServerName() const;
	void SetServerName(const string& newValue);

	const string& GetServerKind() const;
	void SetServerKind(const string& newValue);

	const bool GetDateSetDirectly() const;
	void SetDateSetDirectly(bool newValue);

	const string& GetDateVerb() const;
	void SetDateVerb(const string& newValue);

	const string& GetHardwareClockVerb() const;
	void SetHardwareClockVerb(const string& newValue);

	const string& GetDefaultOutputFileName() const;
	void SetDefaultOutputFileName(const string& newValue);

	const PayloadManagerClientSettings& PMClientSettings() const;
	void PMClientSettings(const PayloadManagerClientSettings& newValue);

	TimeSpan GetDeltaToSetTime() const;
	void SetDeltaToSetTime(const TimeSpan& newValue);
	bool ShouldSetTime(const timespec& oldTime, const timespec& newTime) const;

	TimeSpan GetDeltaToSetHWClock() const;
	void SetDeltaToSetHWClock(const TimeSpan& newValue);
	bool ShouldSetHWClock(bool& previouslySet, double& lastSetSystemUpTime, double currentSystemUpTime) const;

	double GetRMSAccuracyThreshold() const;
	void SetRMSAccuracyThreshold(double newValue);

	bool GetSetTimeOnPPS() const;
	void SetSetTimeOnPPS(bool newValue);

	unsigned GetPPSGPIO() const;
	void SetPPSGPIO(unsigned newValue);

	const EventSettings& GetSystemGPSOfflineSettings() const;
	void SetSystemGPSOfflineSettings(const EventSettings& newValue);

	const EventSettings& GetSystemGPSOnlineSettings() const;
	void SetSystemGPSOnlineSettings(const EventSettings& newValue);

	const EventSettings& GetHostGPSSettings() const;
	void SetHostGPSSettings(const EventSettings& newValue);

	void Load(const string& fileName);
	void Save(const string& fileName);

private:

	friend class boost::serialization::access;

	template<class Archive>
	void load(Archive& ar, TimeSpan& val, const char* valName)
	{
		TimeSpan::tick_type valMs = 0;
		ar & boost::serialization::make_nvp(valName, valMs);
		val = boost::posix_time::milliseconds(valMs);
	}

	template<class Archive>
	void load(Archive & ar, const unsigned int version)
	{
		using namespace LiquidRobotics::SMACFramework::BooleanParser;

		ar & BOOST_SERIALIZATION_NVP(loggerSettings);
		ar & BOOST_SERIALIZATION_NVP(serialPortSettings);
		ar & BOOST_SERIALIZATION_NVP(maxString);
		load(ar, readTimeout, "readTimeoutMs");

		if (version < 4)
		{
			string gpsEventName, gpsEventType, gpsEventDomain;
			ar & BOOST_SERIALIZATION_NVP(gpsEventName);
			ar & BOOST_SERIALIZATION_NVP(gpsEventType);
			ar & BOOST_SERIALIZATION_NVP(gpsEventDomain);
		}

		ar & BOOST_SERIALIZATION_NVP(serverName);
		ar & BOOST_SERIALIZATION_NVP(serverKind);
		ar & BOOST_SERIALIZATION_NVP(dateSetDirectly);
		ar & BOOST_SERIALIZATION_NVP(dateVerb);

		if (version >= 3)
			ar & BOOST_SERIALIZATION_NVP(hwClockVerb);
		else
			hwClockVerb = hwClockDefault;

		if (version >= 2)
			ar & BOOST_SERIALIZATION_NVP(defaultOuputFileName);

		if (version >= 4)
		{
			load(ar, deltaToSetTime, "deltaToSetTimeMs");
			load(ar, deltaToSetHWClock, "deltaToSetHWClockMs");
			ar & BOOST_SERIALIZATION_NVP(rmsAccuracyThreshold);
			ar & BOOST_SERIALIZATION_NVP(gpsEventSpecs);
		}

		if (version >= 5)
			ar & BOOST_SERIALIZATION_NVP(tryBaudRates);
		else
			tryBaudRates.clear();

		if (version >= 6)
		{
			load_boolean_from_archive(ar, setTimeOnPPS, "setTimeOnPPS");
			ar & BOOST_SERIALIZATION_NVP(ppsGPIO);
		}
		else
		{
			setTimeOnPPS = false;
			ppsGPIO = 0;
		}

		if (version >= 7)
		{
			ar & BOOST_SERIALIZATION_NVP(systemGPSOffline);
			ar & BOOST_SERIALIZATION_NVP(systemGPSOnline);
			ar & BOOST_SERIALIZATION_NVP(hostGPSEventSettings);
		}
		else
		{
			systemGPSOffline = EventSettings("SystemGPSOffline", "EventBase", "LiquidR-SMC");
			systemGPSOnline = EventSettings("SystemGPSOnline", "EventBase", "LiquidR-SMC");
			hostGPSEventSettings = EventSettings("HostGPSSample", "GPSSample", "LiquidR-SMC");
		}

		ar & BOOST_SERIALIZATION_NVP(eventTransceiverSettings);
		ar & BOOST_SERIALIZATION_NVP(pmClientSettings);
	}

	template<class Archive>
	void save(Archive& ar, const TimeSpan& val, const char* valName) const
	{
		TimeSpan::tick_type valMs = val.total_milliseconds();
		ar & boost::serialization::make_nvp(valName, valMs);
	}

	template<class Archive>
	void save(Archive & ar, const unsigned int version) const
	{
		using namespace LiquidRobotics::SMACFramework::BooleanParser;

		ar & BOOST_SERIALIZATION_NVP(loggerSettings);
		ar & BOOST_SERIALIZATION_NVP(serialPortSettings);
		ar & BOOST_SERIALIZATION_NVP(maxString);
		save(ar, readTimeout, "readTimeoutMs");
		ar & BOOST_SERIALIZATION_NVP(serverName);
		ar & BOOST_SERIALIZATION_NVP(serverKind);
		ar & BOOST_SERIALIZATION_NVP(dateSetDirectly);
		ar & BOOST_SERIALIZATION_NVP(dateVerb);
		ar & BOOST_SERIALIZATION_NVP(hwClockVerb);							// V3
		ar & BOOST_SERIALIZATION_NVP(defaultOuputFileName);					// V2
		save(ar, deltaToSetTime, "deltaToSetTimeMs");						// V4
		save(ar, deltaToSetHWClock, "deltaToSetHWClockMs");					// V4
		ar & BOOST_SERIALIZATION_NVP(rmsAccuracyThreshold);					// V4
		ar & BOOST_SERIALIZATION_NVP(gpsEventSpecs);						// V4
		ar & BOOST_SERIALIZATION_NVP(tryBaudRates);							// V5
		save_boolean_to_archive(ar, setTimeOnPPS, "setTimeOnPPS");			// V6
		ar & BOOST_SERIALIZATION_NVP(ppsGPIO);								// V6
		ar & BOOST_SERIALIZATION_NVP(systemGPSOffline);						// V7
		ar & BOOST_SERIALIZATION_NVP(systemGPSOnline);						// V7
		ar & BOOST_SERIALIZATION_NVP(hostGPSEventSettings);					// V7
		ar & BOOST_SERIALIZATION_NVP(eventTransceiverSettings);
		ar & BOOST_SERIALIZATION_NVP(pmClientSettings);
	}

	BOOST_SERIALIZATION_SPLIT_MEMBER()

	SerialPortSettings serialPortSettings;
	EventTransceiverSettings eventTransceiverSettings;
	size_type maxString;
	TimeSpan readTimeout;
	string serverName;
	string serverKind;
	bool dateSetDirectly;
	string dateVerb;
	const string hwClockDefault;
	string hwClockVerb;
	string defaultOuputFileName;
	PayloadManagerClientSettings pmClientSettings;
	StdOutLoggerSettings loggerSettings;
	TimeSpan deltaToSetTime;
	TimeSpan deltaToSetHWClock;					// 0 means setting the hardware clock only initially
	double rmsAccuracyThreshold;				// accuracy readings for GPS above this value are rejected
	GPSEventSpecArray gpsEventSpecs;
	BaudRateVectorType tryBaudRates;			// baud rates to try when establishing the correct baud rate
	bool setTimeOnPPS;							// true if updating time only on PPS transition, false if updating on time reading (default)
	unsigned ppsGPIO;							// the GPIO number for PPS
	EventSettings systemGPSOffline;				// event settings to announce when the system GPS is offline
	EventSettings systemGPSOnline;				// event settings to announce when the system GPS is online
	EventSettings hostGPSEventSettings;			// event settings for the incoming event with the Host's GPS readings
};

}
}

BOOST_CLASS_VERSION(LiquidRobotics::GPSListenerLib::GPSEventSpec, 1)
BOOST_CLASS_VERSION(LiquidRobotics::GPSListenerLib::GPSListenerSettings, 7)

#endif /* GPSLISTENERSETTINGS_H_ */
