//----------------------------------------------------------------------------------
// <copyright file="LqrSMACPayloadDescriptionList.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 PayloadDescriptionList, an entity class that stands in for the
//  IDL generated description class.
// </summary>
//
// <owner>Mike Cookson</owner>
//----------------------------------------------------------------------------------

#include "LqrSMACPayloadDescriptionList.h"
#include <boost/foreach.hpp>

namespace LiquidRobotics {
namespace SMACFramework {

PayloadDescriptionList::PayloadDescriptionList()
{
}

PayloadDescriptionList::PayloadDescriptionList(size_type n)
: Base(n)
{
}

PayloadDescriptionList::PayloadDescriptionList(const PayloadDescriptionList& other)
: Base(other)
{

}

PayloadDescriptionList::PayloadDescriptionList(const CORBAEntity& entity)
{
	copy(entity);
}

PayloadDescriptionList& PayloadDescriptionList::operator=(const PayloadDescriptionList& other)
{
	if (this != &other)
		assign(other.begin(), other.end());

	return *this;
}

PayloadDescriptionList& PayloadDescriptionList::operator=(const CORBAEntity& entity)
{
	SetEntity(entity);
	return *this;
}

bool PayloadDescriptionList::operator==(const PayloadDescriptionList& other) const
{
	return !compare(other);
}

bool PayloadDescriptionList::operator<(const PayloadDescriptionList& other) const
{
	return compare(other) < 0;
}

PayloadDescriptionList::CORBAEntity_var PayloadDescriptionList::GetEntity() const
{
	CORBAEntity_var result;
	result = new CORBAEntity;

	const_iterator it = begin();
	const size_type len = size();
	result->length(len);

	for (_CORBA_ULong idx = 0; idx < (_CORBA_ULong) len; ++idx)
	{
		PayloadDescription::CORBAEntity_var temp = (*it++)->GetEntity();
		result[idx] = temp.in();
	}

	return result;
}

void PayloadDescriptionList::SetEntity(const CORBAEntity& entity)
{
	copy(entity);
}

PayloadDescriptionList& PayloadDescriptionList::Clone(const PayloadDescriptionList& other)
{
	if (this != &other)
	{
		clear();

		BOOST_FOREACH(const PayloadDescription::Ptr& item, other)
		{
			PayloadDescription::Ptr newItem;
			newItem.reset(new PayloadDescription(*item));

			push_back(newItem);
		}
	}

	return *this;
}

void PayloadDescriptionList::copy(const CORBAEntity& entity)
{
	const size_type len = (size_type) entity.length();

	clear();

	for (_CORBA_ULong idx = 0; idx < (_CORBA_ULong) len; ++idx)
	{
		PayloadDescription::Ptr newItem;
		newItem.reset(new PayloadDescription(entity[idx]));
		push_back(newItem);
	}
}

int PayloadDescriptionList::compare(const PayloadDescriptionList& other) const
{
	const_iterator i1 = begin();
	const_iterator i2 = other.begin();
	bool atend1, atend2;
	int result;

	do
	{
		atend1 = (i1 == end());
		atend2 = (i2 == end());

		if (atend1 && !atend2)
			result = -1;
		else if (!atend1 && atend2)
			result = 1;
		else if (!atend1)
		{
			const PayloadDescription::Ptr d1 = *i1;
			const PayloadDescription::Ptr d2 = *i2;

			if (*d1 < *d2)
				result = -1;
			else if (*d1 == *d2)
				result = 0;
			else
				result = 1;

			++i1;
			++i2;
		}
		else
			result = 0;

	} while(!result && !atend1);

	return result;
}

std::ostream& operator<<(std::ostream& out, const PayloadDescriptionList& pyldDesc)
{
	out << pyldDesc.size() << " items {";

	BOOST_FOREACH(const PayloadDescription::Ptr desc, pyldDesc)
		out << " { " << *desc << "}";

	out << " }";
	return out;
}

}
}
