//-----------------------------------------------------------------------------------
// <copyright file="SendCommandCompleteEvent.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 SendCommandCompleteEvent class, which models an event that
//   announces asynchronous completion of an ICommandable::SendCommand invocation.
// </summary>
//
// <owner>Mike Cookson</owner>
//-----------------------------------------------------------------------------------

#include "SendCommandCompleteEvent.h"
#include "LqrSMACIdl.h"
#include "LqrSMACBooleanParser.h"

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

namespace LiquidRobotics {
namespace EventLib {

#define EVENT_PREFIX "SendCmdComplete-"

const SendCommandCompleteEvent::PropertyNameType SendCommandCompleteEvent::completeTag(EVENT_PREFIX "Complete");
const SendCommandCompleteEvent::PropertyNameType SendCommandCompleteEvent::requesterTag(EVENT_PREFIX "Requester");
const SendCommandCompleteEvent::PropertyNameType SendCommandCompleteEvent::successTag(EVENT_PREFIX "Success");
const SendCommandCompleteEvent::PropertyNameType SendCommandCompleteEvent::transactionIDTag(EVENT_PREFIX "TransactionID");
const SendCommandCompleteEvent::PropertyNameType SendCommandCompleteEvent::resultTag(EVENT_PREFIX "Result");
const SendCommandCompleteEvent::PropertyNameType SendCommandCompleteEvent::resultFileTag(EVENT_PREFIX "ResultFile");
const SendCommandCompleteEvent::PropertyNameType SendCommandCompleteEvent::resultFileOwnedByReceiverTag(EVENT_PREFIX "ReceiverOwnsResultFile");

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

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

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

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

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

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

	stm << " requester='" << Requester() << "'"
	    << " transactionID='" << TransactionID() << "'"
	    << " success=" << BooleanToString(Successful());

	if (HasResult())
	{
		stm << " result=";

		try
		{
			EventPropertyValue propVal(Result(), broker);
			propVal.Describe(stm);
		}
		catch(exception& ex)
		{
			stm << "exception-" << typeid(ex).name() << ": " << ex.what() << "!!";
		}
		catch(CORBA::Exception& ex)
		{
			stm << "exception-" << DecodeCORBAException(ex);
		}
	}

	if (HasResultFile())
		stm << " result-file='" << ResultFile() << "'"
		    << " receiver-owns-file=" << (ResultFileOwnedByReceiver() ? "true" : "false");

	stm << " complete=" << (Complete() ? "true" : "false");
	return stm;
}

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

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

bool SendCommandCompleteEvent::Complete() const
{
	return GetProperty(completeTag).GetBoolean();
}

void SendCommandCompleteEvent::Complete(bool newValue)
{
	EventPropertyValue newPropValue(broker);
	newPropValue.SetBoolean(newValue);

	SetProperty(completeTag, newPropValue);
}

string SendCommandCompleteEvent::Requester() const
{
	return GetProperty(requesterTag).GetString();
}

void SendCommandCompleteEvent::Requester(const string& newValue)
{
	EventPropertyValue newPropValue(broker);
	newPropValue.SetString(newValue);

	SetProperty(requesterTag, newPropValue);
}

string SendCommandCompleteEvent::TransactionID() const
{
	return GetProperty(transactionIDTag).GetString();
}

void SendCommandCompleteEvent::TransactionID(const string& newValue)
{
	EventPropertyValue newPropValue(broker);
	newPropValue.SetString(newValue);

	SetProperty(transactionIDTag, newPropValue);
}

bool SendCommandCompleteEvent::HasResult() const
{
	return ContainsProperty(resultTag);
}

SendCommandCompleteEvent::CORBAAnyVar SendCommandCompleteEvent::Result() const
{
	CORBAAnyVar result(new CORBAAny);
	GetProperty(resultTag).CopyOut(result.inout());
	return result;
}

void SendCommandCompleteEvent::Result(const CORBAAny& newValue)
{
	if (ContainsProperty(resultFileTag))					ClearProperty(resultFileTag);
	if (ContainsProperty(resultFileOwnedByReceiverTag))		ClearProperty(resultFileOwnedByReceiverTag);

	EventPropertyValue newPropValue(newValue, broker);
	SetProperty(resultTag, newPropValue);
}

string SendCommandCompleteEvent::ResultVariantString() const
{
	string result = GetProperty(resultTag).GetStringVariant();
	return result;
}

void SendCommandCompleteEvent::ResultVariantString(const string& newValue)
{
	EventPropertyValue epv(broker);
	epv.SetStringVariant(newValue);
	SetProperty(resultTag, epv);
}

bool SendCommandCompleteEvent::HasResultFile() const
{
	return ContainsProperty(resultFileTag) && ContainsProperty(resultFileOwnedByReceiverTag);
}

string SendCommandCompleteEvent::ResultFile() const
{
	return GetProperty(resultFileTag).GetString();
}

void SendCommandCompleteEvent::ResultFile(const string& newValue, bool receiverTakesFileOwnership)
{
	if (ContainsProperty(resultTag)) 						ClearProperty(resultTag);

	EventPropertyValue newFileName(broker), newOwnership(broker);
	newFileName.SetString(newValue);
	newOwnership.SetBoolean(receiverTakesFileOwnership);

	SetProperty(resultFileTag, newFileName);
	SetProperty(resultFileOwnedByReceiverTag, newOwnership);
}

bool SendCommandCompleteEvent::ResultFileOwnedByReceiver() const
{
	return GetProperty(resultFileOwnedByReceiverTag).GetBoolean();
}

bool SendCommandCompleteEvent::Successful() const
{
	const bool result = !ContainsProperty(successTag) || GetProperty(successTag).GetBoolean();
	return result;
}

void SendCommandCompleteEvent::Successful(bool success)		// if not called, defaults to successful
{
	EventPropertyValue prop(broker);
	prop.SetBoolean(success);
	SetProperty(successTag, prop);
}

}
}
