//------------------------------------------------------------------------------
// <copyright file="TelemetryReportKey.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>
//   Implementation of TelemetryReportKey, a serializable entity class that
//   represents the key used for telemetry reports (i.e. how to identify a
//   a type of report and accumulation of data to report)
// </summary>
//
// <owner>Mike Cookson</owner>
//------------------------------------------------------------------------------

#include "TelemetryReportKey.h"
#include "nsextend.h"

using namespace LiquidRobotics::SMACFramework;

namespace LiquidRobotics {
namespace PayloadInterface {

TelemetryReportKey::TelemetryReportKey()
: publisherKey(0,0)
, formatCode(0)
{
}

TelemetryReportKey::TelemetryReportKey(BoardAddress _boardAddress, FormatCode _formatCode)
: publisherKey(_boardAddress)
, formatCode(_formatCode)
{
}

TelemetryReportKey::TelemetryReportKey(BoardID _boardID, TaskID _taskID, FormatCode _formatCode)
: publisherKey(_boardID, _taskID)
, formatCode(_formatCode)
{
}

bool TelemetryReportKey::operator==(const TelemetryReportKey& other) const
{
	return !Compare(other);
}

bool TelemetryReportKey::operator<(const TelemetryReportKey& other) const
{
	return Compare(other) < 0;
}

bool TelemetryReportKey::operator>(const TelemetryReportKey& other) const
{
	return Compare(other) > 0;
}

int TelemetryReportKey::Compare(const TelemetryReportKey& other) const
{
	int result = TelemetryPublisherKey::Compare(publisherKey, other.publisherKey);

	if (!result)
	{
		if (formatCode > other.formatCode)
			result = 1;
		else if (formatCode < other.formatCode)
			result = -1;
	}

	return result;
}

TelemetryReportKey::FormatCode TelemetryReportKey::GetFormatCode() const
{
	return formatCode;
}

const TelemetryPublisherKey& TelemetryReportKey::GetPublisherKey() const
{
	return publisherKey;
}

void TelemetryReportKey::SetFormatCode(FormatCode newValue)
{
	formatCode = newValue;
}

void TelemetryReportKey::SetPublisherKey(const TelemetryPublisherKey& newValue)
{
	publisherKey = newValue;
}

std::ostream& operator<<(std::ostream& stm, const TelemetryReportKey& key)
{
	stm << "publisherKey={ " << key.GetPublisherKey() << " } formatCode=0x" << utos(key.GetFormatCode(), 16, 2);
	return stm;
}

}
}
