//------------------------------------------------------------------------------
// <copyright file="EPVFormatConfig.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 EPVFormatConfig class, a serializable class entity that
//  describes how to convert an EventPropertyValue to a binary sequence.
// </summary>
//
// <owner>Mike Cookson</owner>
//------------------------------------------------------------------------------

#ifndef __EVENTLIB_EPVFORMATCONFIG_H__
#define __EVENTLIB_EPVFORMATCONFIG_H__

#include "LqrSMACByteArray.h"
#include "LqrSMACIObjectBroker.h"
#include "LqrSMACSharedLib.h"
#include "LqrSMACXmlDOM.h"
#include "EventPropertyValue.h"
#include <ostream>
#include <string>
#include <boost/serialization/nvp.hpp>

namespace LiquidRobotics {
namespace EventLib {

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

	typedef LiquidRobotics::SMACFramework::ByteArray::byte byte;
	typedef LiquidRobotics::EventLib::EventPropertyValue EventPropertyValue;
	typedef LiquidRobotics::SMACFramework::IObjectBroker IObjectBroker;
	typedef std::string string;
	typedef LiquidRobotics::SMACFramework::XmlDOM::XmlNode XmlNode;
	typedef LiquidRobotics::SMACFramework::XmlDOM::XmlNVPBase XmlNVPBase;

	enum ContentAlgorithm
	{
		CA_LENGTH1,			// write 1-byte length of field rather than field data
		CA_LENGTH2,			// write 2-byte length of field rather than field data
		CA_LENGTH4,			// write 4-byte length of field rather than field data
		CA_LITERAL,			// stream literal value
		CA_RAW				// write raw field data
	};

	enum Endianness
	{
		ENDIAN_DFLT,		// default byte order for machine
		ENDIAN_BIG,			// numeric values are ordered MSB to LSB
		ENDIAN_LITTLE		// numeric values are ordered LSB to MSB
	};

	static void FromString(const string& str, ContentAlgorithm& result);
	static string ToString(const ContentAlgorithm& val);
	static void FromString(const string& str, Endianness& result);
	static string ToString(const Endianness& val);
	static Endianness DetectEndianness();

	EPVFormatConfig(IObjectBroker& _broker, ContentAlgorithm _ca = CA_RAW);			// construction for raw or length with default padding and truncation
	EPVFormatConfig(IObjectBroker& _broker, ContentAlgorithm _ca, size_t _padding, size_t _truncation); // construction allowing setting of padding and truncation
	EPVFormatConfig(IObjectBroker& _broker, const EventPropertyValue& _literal);	// construction for literal (copies _literal)
	EPVFormatConfig(const EPVFormatConfig& other);

	virtual ~EPVFormatConfig();

	EPVFormatConfig& operator=(const EPVFormatConfig& other);
	bool operator==(const EPVFormatConfig& other) const;

	ContentAlgorithm GetContentAlgorithm() const;
	ContentAlgorithm GetLiteralContentAlgorithm() const;
	void SetContentAlgorithm(ContentAlgorithm newValue, ContentAlgorithm newLiteralContentValue = CA_RAW);

	Endianness GetEndianness() const;
	void SetEndianness(Endianness newValue);

	const EventPropertyValue& GetLiteral() const;
	void SetLiteral(const EventPropertyValue& newValue);

	bool PaddingEnabled() const;
	void ClearPadding();
	size_t GetFieldPadding() const;
	byte GetFieldPaddingValue() const;
	void SetFieldPadding(size_t newValue);
	void SetFieldPadding(size_t newValue, size_t newPaddingValue);
	void SetFieldPaddingValue(byte newValue);

	bool TruncationEnabled() const;
	void ClearTruncation();
	size_t GetFieldTruncation() const;
	void SetFieldTruncation(size_t newValue);

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

private:

	friend class boost::serialization::access;

	template<class Archive>
	void serialize(Archive & ar, const unsigned int version)
	{
		using namespace boost::serialization;

		string castr = ToString(contentAlgorithm);
		ar & make_nvp("contentAlgorithm", castr);
		FromString(castr, contentAlgorithm);

		string castr2 = ToString(literalContentAlgorithm);
		ar & make_nvp("literalContentAlgorithm", castr2);
		FromString(castr2, literalContentAlgorithm);

		if (contentAlgorithm == CA_LITERAL)
		{
			if (!literal)
				literal.reset( new EventPropertyValue(broker) );

			ar & make_nvp("literal", *literal);

			CheckLiteralType();
		}

		bool defaultFormat = (endianness == ENDIAN_DFLT) && !paddingEnabled && !truncationEnabled;

		ar & BOOST_SERIALIZATION_NVP(defaultFormat);

		if (!defaultFormat)
		{
			string estr = ToString(endianness);
			ar & make_nvp("endianness", estr);
			FromString(estr, endianness);

			ar & BOOST_SERIALIZATION_NVP(paddingEnabled);
			ar & BOOST_SERIALIZATION_NVP(fieldPadding);
			ar & BOOST_SERIALIZATION_NVP(fieldPaddingValue);
			ar & BOOST_SERIALIZATION_NVP(truncationEnabled);
			ar & BOOST_SERIALIZATION_NVP(fieldTruncation);
		}
	}

	void CheckLiteralType();

	IObjectBroker& broker;
	ContentAlgorithm contentAlgorithm;
	ContentAlgorithm literalContentAlgorithm;
	Endianness endianness;
	EventPropertyValue::Ptr literal;
	bool paddingEnabled;
	size_t fieldPadding;
	byte fieldPaddingValue;
	bool truncationEnabled;
	size_t fieldTruncation;
};

SHARED_LIB_PUBLIC std::ostream& operator<<(std::ostream& stm, const EPVFormatConfig& settings);

}
}

#endif
