//------------------------------------------------------------------------------
// <copyright file="ChannelStateEvent.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 ChannelStateEvent 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 "ChannelStateEvent.h"
#include "LqrSMACIdl.h"

using namespace std;
using namespace LiquidRobotics::SMACFramework;

namespace LiquidRobotics {
namespace EventLib {

#define EVENT_PREFIX "ChannelStateEvent-"

const ChannelStateEvent::PropertyNameType ChannelStateEvent::channelStateTag(EVENT_PREFIX "ChannelState");
const ChannelStateEvent::PropertyNameType ChannelStateEvent::signalStrengthTag(EVENT_PREFIX "SignalStrength");
const ChannelStateEvent::PropertyNameType ChannelStateEvent::protocolRevisionTag(EVENT_PREFIX "ProtocolRevision");

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

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

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

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

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

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

	stm << " Channel 0x" << std::hex << (int) GetProtocolRevision()
		<< " is " << (GetChannelState() ? "ON":"OFF");

	if (HasSignalStrength())
		stm << " (" << std::dec << (int) GetSignalStrength() << ")" << endl;

	return stm;
}

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

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

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

void ChannelStateEvent::SetChannelState(bool val)
{
	PropertyValueType channelState(broker);
	channelState.SetBoolean(val);
	SetProperty(channelStateTag, channelState);
}

void ChannelStateEvent::SetSignalStrength(uint8_t val)
{
	PropertyValueType signalStrength(broker);
	signalStrength.SetUInt8(val);
	SetProperty(signalStrengthTag, signalStrength);
}

bool ChannelStateEvent::HasSignalStrength() const
{
	return ContainsProperty(signalStrengthTag);
}

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

bool ChannelStateEvent::GetChannelState() const
{
	return GetProperty(channelStateTag).GetBoolean();
}

EventPropertyValue::UInt8 ChannelStateEvent::GetSignalStrength() const
{
	return GetProperty(signalStrengthTag).GetUInt8();
}

}
}
