//---------------------------------------------------------------------------------
// <copyright file="PayloadRegistrationEvent.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 the PayloadDescriptionEvent class, which carries information
//   about changes in payload registration.
// </summary>
//
// <owner>Mike Cookson</owner>
//---------------------------------------------------------------------------------

#include "PayloadRegistrationEvent.h"
#include "nsextend.h"
#include "LqrSMACEscapeStr.h"

using namespace std;
using namespace LiquidRobotics::EventLib;
using namespace LiquidRobotics::SMACFramework;

namespace LiquidRobotics {
namespace EventLib {

#define TAGPREFIX "PyldReg-"

const PayloadRegistrationEvent::PropertyNameType PayloadRegistrationEvent::boardIDTag(TAGPREFIX "BoardID");
const PayloadRegistrationEvent::PropertyNameType PayloadRegistrationEvent::taskIDTag(TAGPREFIX "TaskID");
const PayloadRegistrationEvent::PropertyNameType PayloadRegistrationEvent::descriptionTag(TAGPREFIX "Description");
const PayloadRegistrationEvent::PropertyNameType PayloadRegistrationEvent::descriptiveJSONTag(TAGPREFIX "DescriptiveJSON");
const PayloadRegistrationEvent::PropertyNameType PayloadRegistrationEvent::iorTag(TAGPREFIX "IOR");
const PayloadRegistrationEvent::PropertyNameType PayloadRegistrationEvent::readableNameTag(TAGPREFIX "ReadableName");

PayloadRegistrationEvent::PayloadRegistrationEvent(IObjectBroker& _broker)
: EventBase(_broker)
{
}

PayloadRegistrationEvent::PayloadRegistrationEvent(const EventSettings& _eventSettings, IObjectBroker& _broker)
: EventBase(_eventSettings, _broker)
{
}

PayloadRegistrationEvent::PayloadRegistrationEvent(const EventName& _eventName, const EventType& _eventType, IObjectBroker& _broker)
: EventBase(_eventName, _eventType, _broker)
{
}

PayloadRegistrationEvent::PayloadRegistrationEvent(const EventName& _eventName, const EventType& _eventType, const IOR& _senderIOR, IObjectBroker& _broker)
: EventBase(_eventName, _eventType, _senderIOR, _broker)
{
}

PayloadRegistrationEvent::PayloadRegistrationEvent(const PayloadRegistrationEvent& other)
: EventBase(other)
{
}

ostream& PayloadRegistrationEvent::Describe(ostream& stm) const
{
	EventBase::Describe(stm);

	string descriptiveJSON = GetDescriptiveJSON();

	if (descriptiveJSON.length() > 23)
		descriptiveJSON = descriptiveJSON.substr(0,20)+"..."+descriptiveJSON.substr(descriptiveJSON.length()-20);

	stm << "boardID=0x" << ultos(GetBoardID(), 16, 2)
	    << " taskID=0x" << ultos(GetTaskID(), 16, 2)
	    << " ior='" << GetIOR() << "'"
	    << " description='" << GetDescription() << "'"
	    << " descriptiveJSON='" << EscapeStr(descriptiveJSON) << "'"
	    << " readableName='" << GetReadableName() << "'";

	return stm;
}

IEventBase::Ptr PayloadRegistrationEvent::Duplicate() const
{
	IEventBase::Ptr ptr;
	ptr.reset(new PayloadRegistrationEvent(*this));

	return ptr;
}

PayloadRegistrationEvent::PayloadDescription::Ptr PayloadRegistrationEvent::Get() const
{
	PayloadDescription::Ptr result;
	result.reset(new PayloadDescription);

	result->SetBoardID(GetBoardID());
	result->SetTaskID(GetTaskID());
	result->SetIOR(GetIOR());
	result->SetDescription(GetDescription());
	result->SetDescriptiveJSON(GetDescriptiveJSON());
	result->SetReadableName(GetReadableName());

	return result;
}

void PayloadRegistrationEvent::Set(const PayloadDescription::Ptr newValue)
{
	SetBoardID(newValue->GetBoardID());
	SetTaskID(newValue->GetTaskID());
	SetIOR(newValue->GetIOR());
	SetDescription(newValue->GetDescription());
	SetDescriptiveJSON(newValue->GetDescriptiveJSON());
	SetReadableName(newValue->GetReadableName());
}

PayloadRegistrationEvent::BoardIDType PayloadRegistrationEvent::GetBoardID() const
{
	BoardIDType result;

	if (ContainsProperty(boardIDTag))
		result = GetProperty(boardIDTag).GetUInt8();
	else
		result = LiquidRobotics::SMAC::NO_BOARD_ID;

	return result;
}

void PayloadRegistrationEvent::SetBoardID(BoardIDType newValue)
{
	PropertyValueType item(broker);
	item.SetUInt8(newValue);

	SetProperty(boardIDTag, item);
}

PayloadRegistrationEvent::TaskIDType PayloadRegistrationEvent::GetTaskID() const
{
	TaskIDType result;

	if (ContainsProperty(taskIDTag))
		result = GetProperty(taskIDTag).GetUInt8();
	else
		result = LiquidRobotics::SMAC::NO_TASK_ID;

	return result;
}

void PayloadRegistrationEvent::SetTaskID(TaskIDType newValue)
{
	PropertyValueType item(broker);
	item.SetUInt8(newValue);

	SetProperty(taskIDTag, item);
}

string PayloadRegistrationEvent::GetDescription() const
{
	string result;

	if (ContainsProperty(descriptionTag))
		result = GetProperty(descriptionTag).GetString();

	return result;
}

void PayloadRegistrationEvent::SetDescription(const string& newValue)
{
	PropertyValueType item(broker);
	item.SetString(newValue);

	SetProperty(descriptionTag, item);
}

string PayloadRegistrationEvent::GetDescriptiveJSON() const
{
	string result;

	if (ContainsProperty(descriptiveJSONTag))
		result = GetProperty(descriptiveJSONTag).GetString();

	return result;
}

void PayloadRegistrationEvent::SetDescriptiveJSON(const string& newValue)
{
	PropertyValueType item(broker);
	item.SetString(newValue);

	SetProperty(descriptiveJSONTag, item);
}

string PayloadRegistrationEvent::GetIOR() const
{
	string result;

	if (ContainsProperty(iorTag))
		result = GetProperty(iorTag).GetString();

	return result;
}

void PayloadRegistrationEvent::SetIOR(const string& newValue)
{
	PropertyValueType item(broker);
	item.SetString(newValue);

	SetProperty(iorTag, item);
}

string PayloadRegistrationEvent::GetReadableName() const
{
	string result;

	if (ContainsProperty(readableNameTag))
		result = GetProperty(readableNameTag).GetString();

	return result;
}

void PayloadRegistrationEvent::SetReadableName(const string& newValue)
{
	PropertyValueType item(broker);
	item.SetString(newValue);

	SetProperty(readableNameTag, item);
}

}
}
