//------------------------------------------------------------------------------
// <copyright file="SBDPacketEvent.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 SBDPacketEvent class, which carries packet data being
//   sent from the SMC or received into the SMC.
// </summary>
//
// <owner>Mike Cookson</owner>
//------------------------------------------------------------------------------

#include "SBDPacketEvent.h"
#include "LqrSMACIdl.h"
#include "nsextend.h"

using namespace std;
using namespace LiquidRobotics::SMACFramework;

namespace LiquidRobotics {
namespace EventLib {

#define EVENT_PREFIX "SBDPacketEvent_"

const SBDPacketEvent::PropertyNameType SBDPacketEvent::DataTag = EVENT_PREFIX "Data";
const SBDPacketEvent::PropertyNameType SBDPacketEvent::PriorityTag = EVENT_PREFIX "Priority";
const SBDPacketEvent::PropertyNameType SBDPacketEvent::DirectionTag = EVENT_PREFIX "Direction";
const SBDPacketEvent::PropertyNameType SBDPacketEvent::PSNTag = EVENT_PREFIX "PacketSerialNum";
const SBDPacketEvent::PropertyNameType SBDPacketEvent::TransactionIDTag = EVENT_PREFIX "TransactionID";
const SBDPacketEvent::PropertyNameType SBDPacketEvent::TimeToLiveTag = EVENT_PREFIX "TimeToLive";
const SBDPacketEvent::PropertyNameType SBDPacketEvent::QueueStrategyTag = EVENT_PREFIX "QueueStrategy";
const SBDPacketEvent::PropertyNameType SBDPacketEvent::DispositionTag = EVENT_PREFIX "Disposition";
const SBDPacketEvent::PropertyNameType SBDPacketEvent::ReferenceIDTag = EVENT_PREFIX "ReferenceID";

SBDPacketEvent::SBDPacketEvent(IObjectBroker& _broker)
: Base(_broker)
{
}

SBDPacketEvent::SBDPacketEvent(const EventSettings& _eventSettings, IObjectBroker& _broker)
: Base(_eventSettings, _broker)
{
}

SBDPacketEvent::SBDPacketEvent(const EventName& _eventName, const EventType& _eventType, IObjectBroker& _broker)
: Base(_eventName, _eventType, _broker)
{
}

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

SBDPacketEvent::SBDPacketEvent(const SBDPacketEvent& other)
: Base(other)
{
}

ostream& SBDPacketEvent::Describe(ostream& stm) const
{
	Base::Describe(stm);

    if (HasPacketData())
    {
        stm << " data=";
        ByteArray temp;
        GetPacketData(temp);
        stm << temp.ToHexString();
    }

    stm << " direction=";

    switch(GetPacketDirection())
    {
    case RECEIVED_FROM_COMM_CHANNEL:    stm << "RECEIVE";       break;
    case SENDING_TO_COMM_CHANNEL:       stm << "SEND";          break;
    default:                            stm << "UNSPECIFIED";   break;
    }

    if (HasPacketSerialNumber())
        stm << " packetSerialNumber=" << GetPacketSerialNumber();

    if (HasPriority())
        stm << " priority=" << GetPriority();

    if (HasReferenceID())
        stm << " referenceID=0x" << utos(GetReferenceID(),16,4);

    if (HasTransactionID())
        stm << " transactionID=" << GetTransactionID();

    if (HasTimeToLive())
        stm << " timeToLive=" << GetTimeToLive() << " sec";

    if (HasQueueStrategy())
    {
        stm << " queueStrategy=";

        switch(GetQueueStrategy())
        {
        case QUEUE_END:             stm << "END";                                   break;
        case QUEUE_FRONT:           stm << "FRONT";                                 break;
        case QUEUE_LATEST_AT_FRONT: stm << "LATEST_FRONT";                          break;
        default:                    stm << (unsigned) GetQueueStrategy() << "?";    break;
        }
    }

    if (HasDisposition())
    {
        stm << " disposition=";

        switch(GetDisposition())
        {
        case DISP_SUCCESS:      stm << "SUCCESS";                                   break;
        case DISP_TOO_LONG:     stm << "TOO_LONG";                                  break;
        case DISP_TIMEOUT:      stm << "TIMEOUT";                                   break;
        case DISP_ZERO_LENGTH:  stm << "ZERO_LENGTH";                               break;
        default:                stm << (unsigned) GetDisposition() << "?";          break;
        }
    }

	return stm;
}

IEventBase::Ptr SBDPacketEvent::Duplicate() const
{
	IEventBase::Ptr result;
	result.reset(new SBDPacketEvent(*this));

	return result;
}

bool SBDPacketEvent::HasPacketData() const
{
    return ContainsProperty(DataTag);
}

void SBDPacketEvent::GetPacketData(ByteArray& content) const
{
    if (ContainsProperty(DataTag))
    {
    	ByteArray::Ptr contentField = GetProperty(DataTag).GetByteArray();

        if (contentField && contentField->size())
            content.assign(contentField->begin(), contentField->end());
        else
            content.clear();
    }
    else
        content.clear();
}

void SBDPacketEvent::SetPacketData(const ByteArray& content)
{
    if (content.size())
    {
    	EventPropertyValue epv(broker);
        epv.SetByteArray(content);
        SetProperty(DataTag, epv);
    }
    else if (ContainsProperty(DataTag))
        ClearProperty(DataTag);
}

SBDPacketEvent::PacketDirection SBDPacketEvent::GetPacketDirection() const
{
    PacketDirection result;

    if (ContainsProperty(DirectionTag))
        result = (PacketDirection) GetProperty(DirectionTag).GetUInt16();
    else
        result = DIRECTION_NOT_SPECIFIED;

    return result;
}

void SBDPacketEvent::SetPacketDirection(PacketDirection newValue)
{
    switch(newValue)
    {
        case RECEIVED_FROM_COMM_CHANNEL:
        case SENDING_TO_COMM_CHANNEL:
        {
            EventPropertyValue epv(broker);
            epv.SetUInt16(newValue);
            SetProperty(DirectionTag, epv);
            break;
        }

        default:

            if (ContainsProperty(DirectionTag))
                ClearProperty(DirectionTag);

            break;
    }
}

bool SBDPacketEvent::HasPacketSerialNumber() const
{
    return ContainsProperty(PSNTag);
}

uint32_t SBDPacketEvent::GetPacketSerialNumber() const
{
    const uint32_t result = GetProperty(PSNTag).GetUInt32();
    return result;
}

void SBDPacketEvent::SetPacketSerialNumber(uint32_t newValue)
{
    EventPropertyValue epv(broker);
    epv.SetUInt32(newValue);
    SetProperty(PSNTag, epv);
}

bool SBDPacketEvent::HasPriority() const
{
    return ContainsProperty(PriorityTag);
}

uint16_t SBDPacketEvent::GetPriority() const
{
    const uint16_t result = GetProperty(PriorityTag).GetUInt16();
    return result;
}

void SBDPacketEvent::SetPriority(uint16_t newValue)
{
    EventPropertyValue epv(broker);
    epv.SetUInt16(newValue);
    SetProperty(PriorityTag, epv);
}

bool SBDPacketEvent::HasTransactionID() const
{
    return ContainsProperty(TransactionIDTag);
}

uint32_t SBDPacketEvent::GetTransactionID() const
{
    const uint32_t result = GetProperty(TransactionIDTag).GetUInt32();
    return result;
}

void SBDPacketEvent::SetTransactionID(uint32_t newValue)
{
    EventPropertyValue epv(broker);
    epv.SetUInt32(newValue);
    SetProperty(TransactionIDTag, epv);
}

bool SBDPacketEvent::HasTimeToLive() const
{
    return ContainsProperty(TimeToLiveTag);
}

uint32_t SBDPacketEvent::GetTimeToLive() const
{
    const uint32_t result = GetProperty(TimeToLiveTag).GetUInt32();
    return result;
}

void SBDPacketEvent::SetTimeToLive(uint32_t newValue)
{
    EventPropertyValue epv(broker);
    epv.SetUInt32(newValue);
    SetProperty(TimeToLiveTag, epv);
}

bool SBDPacketEvent::HasQueueStrategy() const
{
    return ContainsProperty(QueueStrategyTag);
}

SBDPacketEvent::QueuePosition SBDPacketEvent::GetQueueStrategy() const
{
    const QueuePosition result = (QueuePosition) GetProperty(QueueStrategyTag).GetUInt16();
    return result;
}

void SBDPacketEvent::SetQueueStrategy(QueuePosition newValue)
{
    EventPropertyValue epv(broker);
    epv.SetUInt16(newValue);
    SetProperty(QueueStrategyTag, epv);
}

bool SBDPacketEvent::HasDisposition() const
{
    return ContainsProperty(DispositionTag);
}

SBDPacketEvent::Disposition SBDPacketEvent::GetDisposition() const
{
    const Disposition result = (Disposition) GetProperty(DispositionTag).GetUInt16();
    return result;
}

void SBDPacketEvent::SetDisposition(Disposition newValue)
{
    EventPropertyValue epv(broker);
    epv.SetUInt16(newValue);
    SetProperty(DispositionTag, epv);
}

bool SBDPacketEvent::HasReferenceID() const
{
    return ContainsProperty(ReferenceIDTag);
}

uint16_t SBDPacketEvent::GetReferenceID() const
{
    const uint16_t result = GetProperty(ReferenceIDTag).GetUInt16();
    return result;
}

void SBDPacketEvent::SetReferenceID(uint16_t newValue)
{
    EventPropertyValue epv(broker);
    epv.SetUInt16(newValue);
    SetProperty(ReferenceIDTag, epv);
}

}
}
