//------------------------------------------------------------------------------
// <copyright file="ChannelRequestEvent.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 ChannelRequestEvent class, which models an event
//   that announces the status change of channels controlled by the Connection
//   Manager, such as Channel On/Off or Signal Strength updates
// </summary>
//
// <owner>Arash Ushani</owner>
//------------------------------------------------------------------------------

#include "ChannelRequestEvent.h"
#include "LqrSMACIdl.h"

using namespace std;
using namespace LiquidRobotics::SMACFramework;

namespace LiquidRobotics {
namespace EventLib {

#define EVENT_PREFIX "ChannelRequestEvent-"

const ChannelRequestEvent::PropertyNameType ChannelRequestEvent::protocolRevisionTag(EVENT_PREFIX "ProtocolRevision");
const ChannelRequestEvent::PropertyNameType ChannelRequestEvent::requestTypeTag(EVENT_PREFIX "RequestType");

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

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

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

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

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

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

	stm << " Channel 0x" << std::hex << (int) GetProtocolRevision();

	if (IsActiveRequest())
		stm << " Active Request";
	else if (IsCancelRequest())
		stm << " Cancel Request";

	return stm;
}

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

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

void ChannelRequestEvent::SetProtocolRevision(uint8_t val)
{
	PropertyValueType protocolRevision(broker);
	protocolRevision.SetUInt8(val);
	SetProperty(protocolRevisionTag, protocolRevision);
}

EventPropertyValue::UInt8 ChannelRequestEvent::GetProtocolRevision() const
{
	return GetProperty(protocolRevisionTag).GetUInt8();
}

bool ChannelRequestEvent::IsActiveRequest() const
{
	return GetProperty(requestTypeTag).GetUInt8() == ACTIVE_REQUEST;
}

void ChannelRequestEvent::SetActiveRequest()
{
	PropertyValueType requestType(broker);
	requestType.SetUInt8(ACTIVE_REQUEST);
	SetProperty(requestTypeTag, requestType);
}

bool ChannelRequestEvent::IsCancelRequest() const
{
	return GetProperty(requestTypeTag).GetUInt8() == CANCEL_REQUEST;
}

void ChannelRequestEvent::SetCancelRequest()
{
	PropertyValueType requestType(broker);
	requestType.SetUInt8(CANCEL_REQUEST);
	SetProperty(requestTypeTag, requestType);
}


}
}
