//----------------------------------------------------------------------------------
// <copyright file="TelemetryFieldList.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 TelemetryFieldList, a list of event field format configuration
//   names (in a library) and event field names that can be used to format an event
//   into a byte stream.
// </summary>
//
// <owner>Mike Cookson</owner>
//----------------------------------------------------------------------------------

#ifndef __PAYLOADINTERFACELIB_TELEMETRYFIELDLIST_H__
#define __PAYLOADINTERFACELIB_TELEMETRYFIELDLIST_H__

#include "LqrSMACSharedLib.h"
#include "LqrSMACXmlDOM.h"
#include <string>
#include <list>
#include <ostream>
#include <boost/shared_ptr.hpp>
#include <boost/serialization/access.hpp>
#include <boost/serialization/split_member.hpp>

namespace boost {
namespace archive {
	class xml_iarchive;
	class xml_oarchive;
}
}

namespace LiquidRobotics {

namespace EventLib {
	class IEventBase;
}

namespace SMACFramework {
	class ByteArray;
	class IObjectBroker;
}

namespace PayloadInterface {

class TelemetryFieldLibrary;

class SHARED_LIB_PUBLIC TelemetryFieldList
: public LiquidRobotics::SMACFramework::XmlDOM::IXmlDOMPersistent
{
public:

	typedef LiquidRobotics::SMACFramework::ByteArray ByteArray;
	typedef LiquidRobotics::EventLib::IEventBase IEventBase;
	typedef LiquidRobotics::SMACFramework::IObjectBroker IObjectBroker;
	typedef std::string string;
	typedef boost::shared_ptr<string> StringPtr;
	typedef boost::archive::xml_iarchive xml_iarchive;
	typedef boost::archive::xml_oarchive xml_oarchive;
	typedef LiquidRobotics::SMACFramework::XmlDOM::XmlNode XmlNode;
	typedef LiquidRobotics::SMACFramework::XmlDOM::XmlNVPBase XmlNVPBase;

	TelemetryFieldList();
	TelemetryFieldList(const TelemetryFieldList& other);

	const TelemetryFieldList& operator=(const TelemetryFieldList& other);

	bool operator==(const TelemetryFieldList& other) const;
	bool operator<(const TelemetryFieldList& other) const;
	bool operator>(const TelemetryFieldList& other) const;

	void Add(const string& libFieldName, size_t nextIndex = (size_t) -1);
	void Add(const string& libFieldName, const string& eventFieldName, size_t nextIndex = (size_t) -1);
	void Clear();
	size_t Count() const;
	std::ostream& Describe(std::ostream& stm) const;
	string GetEventFieldName(size_t idx) const;
	string GetLibFieldName(size_t idx) const;
	void Load(xml_iarchive& arch);
	void Remove(size_t index);
	void Save(xml_oarchive& arch) const;
	bool ToBinary(const TelemetryFieldLibrary& lib, const IEventBase& evt, ByteArray& result, bool append = false) const;

	void Load(XmlNode& node, const XmlNVPBase& nvp);
	void Save(XmlNode& node, const XmlNVPBase& nvp) const;

private:

	static int Compare(const TelemetryFieldList& v1, const TelemetryFieldList& v2);

	struct ListEntry : public LiquidRobotics::SMACFramework::XmlDOM::IXmlDOMPersistent
	{
		ListEntry();
		ListEntry(const string& lib, StringPtr evt);

		static int Compare(const ListEntry& v1, const ListEntry& v2);
		bool operator==(const ListEntry& other) const;
		bool operator<(const ListEntry& other) const;
		bool operator>(const ListEntry& other) const;

		void DeepCopy(const ListEntry& other);

		void Load(XmlNode& node, const XmlNVPBase& nvp);
		void Save(XmlNode& node, const XmlNVPBase& nvp) const;

		friend class boost::serialization::access;

		template<class Archive>
		void load(Archive& ar, const unsigned int version);

		template<class Archive>
		void save(Archive& ar, const unsigned int version) const;

		BOOST_SERIALIZATION_SPLIT_MEMBER()

		string libField;
		StringPtr eventField;
	};

	typedef std::list<ListEntry> FieldList;

	static bool ItemsEqual(const ListEntry& v1, const ListEntry& v2);

	FieldList fields;
};

SHARED_LIB_PUBLIC std::ostream& operator<<(std::ostream& stm, const TelemetryFieldList& fieldList);

}
}

#endif
