//------------------------------------------------------------------------------
// <copyright file="FCCMessageEvent.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 FCCMessageEvent class, which models an event that
//   announces incoming or outgoing FCC Messages. This could be a request to
//   send a message to the FCC, or an annoucement that a message was received
//   from the FCC.
// </summary>
//
// <owner>Arash Ushani</owner>
//------------------------------------------------------------------------------

#include "FCCMessageEvent.h"
#include "LqrSMACIdl.h"

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

namespace LiquidRobotics {
namespace EventLib {

#define EVENT_PREFIX "FCCMessage-"

const FCCMessageEvent::PropertyNameType FCCMessageEvent::sourceTag(EVENT_PREFIX "Source");
const FCCMessageEvent::PropertyNameType FCCMessageEvent::destinationTag(EVENT_PREFIX "Destination");
const FCCMessageEvent::PropertyNameType FCCMessageEvent::messageTag(EVENT_PREFIX "Message");

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

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

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

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

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

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

	if (HasSource())
		stm << " Source=0x" << std::hex << (int) GetSource() << ", ";
	else
		stm << " Default Source, ";

	if (HasDestination())
	    stm << "Destination=0x" << std::hex << (int) GetDestination() << ", ";
	else
		stm << "Default Destination, ";


	stm << std::dec << GetMessage()->size() <<  " bytes";

	return stm;
}

IEventBase::Ptr FCCMessageEvent::Duplicate() const
{
	IEventBase::Ptr result;
	result.reset(new FCCMessageEvent(broker));

	((FCCMessageEvent*) result.get())->Copy(*this);
	return result;
}

void FCCMessageEvent::SetSource(uint16_t val)
{
	PropertyValueType source(broker);
	source.SetUInt16(val);
	SetProperty(sourceTag, source);
}

void FCCMessageEvent::SetDestination(uint16_t val)
{
	PropertyValueType dest(broker);
	dest.SetUInt16(val);
	SetProperty(destinationTag, dest);
}

void FCCMessageEvent::SetMessage(const EventPropertyValue::UInt8Vector& val)
{
	PropertyValueType message(broker);
	message.SetUInt8Vector(val);
	SetProperty(messageTag, message);
}

bool FCCMessageEvent::HasSource() const
{
	return ContainsProperty(sourceTag);
}

bool FCCMessageEvent::HasDestination() const
{
	return ContainsProperty(destinationTag);
}

EventPropertyValue::UInt16 FCCMessageEvent::GetSource() const
{
	return GetProperty(sourceTag).GetUInt16();
}

EventPropertyValue::UInt16 FCCMessageEvent::GetDestination() const
{
	return GetProperty(destinationTag).GetUInt16();
}

EventPropertyValue::UInt8VectorPtr FCCMessageEvent::GetMessage() const
{
	return GetProperty(messageTag).GetUInt8Vector();
}

}
}
