//------------------------------------------------------------------------------
// <copyright file="PayloadMessage_Header.cpp" 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>
// </summary>
//
// <owner></owner>
//------------------------------------------------------------------------------

#include "PayloadMessage.h"
#include <sstream>
#include "LqrSMACPrintfBuffer.h"
#include "nsextend.h"

using namespace std;
using namespace LiquidRobotics::SMACFramework;

#define HAVE_MESSAGES_NO_ACK_MASK (0x8000)

namespace LiquidRobotics {
namespace PayloadInterface {

PayloadMessage::Header::Header()
: messageType(0)
, sourceAddress(0)
, destinationAddress(0)
, transactionID(0)
, messageLength(0)
{
}

PayloadMessage::Header::Header(MessageType _messageType, BoardAddress _sourceAddress, BoardAddress _destinationAddress, TransactionID _transactionID, MessageLength _messageLength)
: messageType(_messageType)
, sourceAddress(_sourceAddress)
, destinationAddress(_destinationAddress)
, transactionID(_transactionID)
, messageLength(_messageLength)
{
	TestMessageType(_messageType);
}

PayloadMessage::Header::Header(const Header& other)
: messageType(other.messageType)
, sourceAddress(other.sourceAddress)
, destinationAddress(other.destinationAddress)
, transactionID(other.transactionID)
, messageLength(other.messageLength)
{
}

PayloadMessage::Header::Header(const ByteArray& input)
{
	FromArray(input);
}

PayloadMessage::Header::Header(const ByteArray& input, MessageLength& index, CRC16& crc)
{
	FromArray(input, index, crc);
}

PayloadMessage::Header::~Header()
{
}

PayloadMessage::Header& PayloadMessage::Header::operator=(const Header& other)
{
	if (this != &other)
	{
		messageType = other.messageType;
		sourceAddress = other.sourceAddress;
		destinationAddress = other.destinationAddress;
		transactionID = other.transactionID;
		messageLength = other.messageLength;
	}

	return *this;
}

bool PayloadMessage::Header::operator==(const Header& other) const
{
	return (messageType == other.messageType)
	    && (sourceAddress == other.sourceAddress)
	    && (destinationAddress == other.destinationAddress)
	    && (transactionID == other.transactionID)
	    && (messageLength == other.messageLength);
}

PayloadMessage::MessageType PayloadMessage::Header::GetMessageType() const
{
	return messageType & ~HAVE_MESSAGES_NO_ACK_MASK;
}

void PayloadMessage::Header::SetMessageType(MessageType _messageType)
{
	TestMessageType(_messageType);
	messageType = (messageType & HAVE_MESSAGES_NO_ACK_MASK) | (_messageType & ~HAVE_MESSAGES_NO_ACK_MASK);
}

void PayloadMessage::Header::TestMessageType(MessageType _messageType)
{
	if (_messageType & HAVE_MESSAGES_NO_ACK_MASK)
	{
		PrintfBuffer msg("Message type cannot set mask 0x%.4X", HAVE_MESSAGES_NO_ACK_MASK);
		throw invalid_argument(msg.AsStr());
	}
}

bool PayloadMessage::Header::GetNoAckHaveMessagesFlag() const
{
	return !!(messageType & HAVE_MESSAGES_NO_ACK_MASK);
}

void PayloadMessage::Header::SetNoAckHaveMessagesFlag(bool set)
{
	if (set)
		messageType |= HAVE_MESSAGES_NO_ACK_MASK;
	else
		messageType &= ~HAVE_MESSAGES_NO_ACK_MASK;
}

PayloadMessage::BoardAddress PayloadMessage::Header::GetSourceAddress() const
{
	return sourceAddress;
}

void PayloadMessage::Header::SetSourceAddress(BoardAddress _sourceAddress)
{
	sourceAddress = _sourceAddress;
}

PayloadMessage::BoardAddress PayloadMessage::Header::GetDestinationAddress() const
{
	return destinationAddress;
}

void PayloadMessage::Header::SetDestinationAddress(BoardAddress _destinationAddress)
{
	destinationAddress = _destinationAddress;
}

PayloadMessage::TransactionID PayloadMessage::Header::GetTransactionID() const
{
	return transactionID;
}

void PayloadMessage::Header::SetTransactionID(TransactionID _transactionID)
{
	transactionID = _transactionID;
}

PayloadMessage::MessageLength PayloadMessage::Header::GetMessageLength() const
{
	return messageLength;
}

void PayloadMessage::Header::SetMessageLength(MessageLength _messageLength)
{
	messageLength = _messageLength;
}

void PayloadMessage::Header::ToArray(ByteArray& result) const
{
	CRC16 dummyCRC;

	result.clear();

	PayloadMessage::Put(result, messageLength, dummyCRC);
	PayloadMessage::Put(result, destinationAddress, dummyCRC);
	PayloadMessage::Put(result, sourceAddress, dummyCRC);
	PayloadMessage::Put(result, transactionID, dummyCRC);
	PayloadMessage::Put(result, messageType, dummyCRC);
}

void PayloadMessage::Header::FromArray(const ByteArray& input)
{
	CRC16 dummyCRC;
	MessageLength index = 0;

	FromArray(input, index, dummyCRC);
}

void PayloadMessage::Header::FromArray(const ByteArray& input, MessageLength& index, CRC16& crc)
{
	PayloadMessage::Get(input, index, messageLength, crc);
	PayloadMessage::Get(input, index, destinationAddress, crc);
	PayloadMessage::Get(input, index, sourceAddress, crc);
	PayloadMessage::Get(input, index, transactionID, crc);
	PayloadMessage::Get(input, index, messageType, crc);
}

PayloadMessage::string PayloadMessage::Header::ToString() const
{
	stringstream sstm;

	sstm << *this;
	return sstm.str();
}

SHARED_LIB_PUBLIC ostream& operator<<(ostream& out, const PayloadMessage::Header& header)
{
	out <<  "MessageType=" << PayloadMessage::ToString(header.GetMessageType())
	    << " HaveMessage/NoAck=" << (header.GetNoAckHaveMessagesFlag() ? "true" : "false")
	    << " Source=0x" << ultos(header.GetSourceAddress(), 16, 4)
	    << " Dest=0x" << ultos(header.GetDestinationAddress(), 16, 4)
	    << " TransactionID=0x" << ultos(header.GetTransactionID(), 16, 4)
	    << " MessageLength=" << header.GetMessageLength();

	return out;
}

}
}
