//------------------------------------------------------------------------------
// <copyright file="LqrSMACPayloadDescription.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 PayloadDescription, an entity class that stands in for the
//  IDL generated description class.
// </summary>
//
// <owner>Mike Cookson</owner>
//------------------------------------------------------------------------------

#include "LqrSMACPayloadDescription.h"
#include "nsextend.h"

using namespace std;
using namespace LiquidRobotics::SMACFramework;

namespace LiquidRobotics {
namespace SMACFramework {

PayloadDescription::PayloadDescription()
: boardID(0xFF)
, taskID(0xFF)
{
}

PayloadDescription::PayloadDescription(const PayloadDescription& other)
: boardID(other.boardID)
, taskID(other.taskID)
, description(other.description)
, ior(other.ior)
, readableName(other.readableName)
, descriptiveJSON(other.descriptiveJSON)
{
}

PayloadDescription::PayloadDescription(const CORBAEntity& entity)
: boardID(entity.boardID)
, taskID(entity.taskID)
, description(entity.description)
, ior(entity.ior)
, readableName(entity.readableName)
, descriptiveJSON(entity.descriptiveJSON)
{
}

PayloadDescription& PayloadDescription::operator=(const PayloadDescription& other)
{
	if (this != &other)
	{
		boardID = other.boardID;
		taskID = other.taskID;
		description = other.description;
		ior = other.ior;
		readableName = other.readableName;
		descriptiveJSON = other.descriptiveJSON;
	}

	return *this;
}

PayloadDescription& PayloadDescription::operator=(const CORBAEntity& entity)
{
	boardID = entity.boardID;
	taskID = entity.taskID;
	description = entity.description;
	ior = entity.ior;
	readableName = entity.readableName;
	descriptiveJSON = entity.descriptiveJSON;
	return *this;
}

bool PayloadDescription::operator==(const PayloadDescription& other) const
{
	return !compare(other);
}

bool PayloadDescription::operator<(const PayloadDescription& other) const
{
	return compare(other) < 0;
}

int PayloadDescription::compare(const PayloadDescription& other) const
{
	int result = (boardID < other.boardID) ? -1 : ((boardID == other.boardID) ? 0 : 1);

	if (!result)
	{
		result = (taskID < other.taskID) ? -1 : ((taskID == other.taskID) ? 0 : 1);

		if (!result)
		{
			result = description.compare(other.description);

			if (!result)
			{
				result = ior.compare(other.ior);

				if (!result)
				{
					result = readableName.compare(other.readableName);

					if (!result)
						result = descriptiveJSON.compare(other.descriptiveJSON);
				}
			}
		}
	}

	return result;
}

PayloadDescription::CORBAEntity_var PayloadDescription::GetEntity() const
{
	CORBAEntity_var result;
	result = new CORBAEntity;
	result->boardID = boardID;
	result->taskID = taskID;
	result->description = CORBA::string_dup(description.c_str());
	result->ior = CORBA::string_dup(ior.c_str());
	result->readableName = CORBA::string_dup(readableName.c_str());
	result->descriptiveJSON = CORBA::string_dup(descriptiveJSON.c_str());
	return result;
}

void PayloadDescription::SetEntity(const CORBAEntity& newValue)
{
	*this = newValue;
}

PayloadDescription::BoardIDType PayloadDescription::GetBoardID() const
{
	return boardID;
}

void PayloadDescription::SetBoardID(BoardIDType newValue)
{
	boardID = newValue;
}

PayloadDescription::TaskIDType PayloadDescription::GetTaskID() const
{
	return taskID;
}

void PayloadDescription::SetTaskID(TaskIDType newValue)
{
	taskID = newValue;
}

const string& PayloadDescription::GetDescription() const
{
	return description;
}

void PayloadDescription::SetDescription(const string& newValue)
{
	description = newValue;
}

const string& PayloadDescription::GetIOR() const
{
	return ior;
}

void PayloadDescription::SetIOR(const string& newValue)
{
	ior = newValue;
}

const string& PayloadDescription::GetReadableName() const
{
	return readableName;
}

void PayloadDescription::SetReadableName(const string& newValue)
{
	readableName = newValue;
}

const string& PayloadDescription::GetDescriptiveJSON() const
{
	return descriptiveJSON;
}

void PayloadDescription::SetDescriptiveJSON(const string& newValue)
{
	descriptiveJSON = newValue;
}

ostream& operator<<(ostream& out, const PayloadDescription& pyldDesc)
{
	out << "BoardID=0x" << ltos(pyldDesc.GetBoardID(), 16, 2)
		<< " TaskID=0x" << ltos(pyldDesc.GetTaskID(), 16, 2)
		<< " Description='" << pyldDesc.GetDescription() << "'"
		<< " IOR='" << pyldDesc.GetIOR() << "'"
		<< " ReadableName='" << pyldDesc.GetReadableName() << "'"
		<< " DescriptiveJSON='" << pyldDesc.GetDescriptiveJSON() << "'";

	return out;
}

}
}
