//---------------------------------------------------------------------------------
// <copyright file="PayloadManagerClientSettings.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 PayloadManagerClientSettings class, an entity class that
//   holds the settings for PayloadManagerClient
// </summary>
//
// <owner>Mike Cookson</owner>
//---------------------------------------------------------------------------------

#include "PayloadManagerClientSettings.h"
#include <stdexcept>
#include <iostream>
#include <fstream>
#include <sstream>
#include <boost/archive/xml_iarchive.hpp>
#include <boost/archive/xml_oarchive.hpp>
#include <boost/foreach.hpp>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>

using namespace std;
using namespace boost;
using namespace boost::archive;
using namespace boost::serialization;

static const char rootLevel[] = "rootlevel";
static const char className[] = "PayloadManagerClientSettings";

namespace LiquidRobotics {
namespace EventLib {

PayloadManagerClientSettings::PayloadManagerClientSettings()
: serviceName("PayloadManager")
, serviceKind("")
, deviceAddedEventName("PayloadDeviceAdded")
, deviceChangedEventName("PayloadDeviceChanged")
, deviceRemovedEventName("PayloadDeviceRemoved")
, commonEventType("PayloadRegistrationEvent")
, commonEventDomain("LiquidR-SMC")
//, payloadDescription (use defaults)
{

}

PayloadManagerClientSettings::PayloadManagerClientSettings(const PayloadManagerClientSettings& other)
: serviceName(other.serviceName)
, serviceKind(other.serviceKind)
, deviceAddedEventName(other.deviceAddedEventName)
, deviceChangedEventName(other.deviceChangedEventName)
, deviceRemovedEventName(other.deviceRemovedEventName)
, commonEventType(other.commonEventType)
, commonEventDomain(other.commonEventDomain)
, payloadDescription(other.payloadDescription)
{
}

PayloadManagerClientSettings& PayloadManagerClientSettings::operator=(const PayloadManagerClientSettings& other)
{
	if (this != &other)
	{
		serviceName = other.serviceName;
		serviceKind = other.serviceKind;
		deviceAddedEventName = other.deviceAddedEventName;
		deviceChangedEventName = other.deviceChangedEventName;
		deviceRemovedEventName = other.deviceRemovedEventName;
		commonEventType = other.commonEventType;
		commonEventDomain = other.commonEventDomain;
		payloadDescription = other.payloadDescription;
	}

	return *this;
}

bool PayloadManagerClientSettings::operator==(const PayloadManagerClientSettings& other) const
{
	return !serviceName.compare(other.serviceName)
		&& !serviceKind.compare(other.serviceKind)
		&& !deviceAddedEventName.compare(other.deviceAddedEventName)
		&& !deviceChangedEventName.compare(other.deviceChangedEventName)
		&& !deviceRemovedEventName.compare(other.deviceRemovedEventName)
		&& !commonEventType.compare(other.commonEventType)
		&& !commonEventDomain.compare(other.commonEventDomain)
		&& (payloadDescription == other.payloadDescription);
}

const string& PayloadManagerClientSettings::GetServiceName() const
{
	return serviceName;
}

void PayloadManagerClientSettings::SetServiceName(const string& newValue)
{
	serviceName = newValue;
}

const string& PayloadManagerClientSettings::GetServiceKind() const
{
	return serviceKind;
}

void PayloadManagerClientSettings::SetServiceKind(const string& newValue)
{
	serviceKind = newValue;
}

const string& PayloadManagerClientSettings::GetDeviceAddedEventName() const
{
	return deviceAddedEventName;
}

void PayloadManagerClientSettings::SetDeviceAddedEventName(const string& newValue)
{
	deviceAddedEventName = newValue;
}

const string& PayloadManagerClientSettings::GetDeviceChangedEventName() const
{
	return deviceChangedEventName;
}

void PayloadManagerClientSettings::SetDeviceChangedEventName(const string& newValue)
{
	deviceChangedEventName = newValue;
}

const string& PayloadManagerClientSettings::GetDeviceRemovedEventName() const
{
	return deviceRemovedEventName;
}

void PayloadManagerClientSettings::SetDeviceRemovedEventName(const string& newValue)
{
	deviceRemovedEventName = newValue;
}

const string& PayloadManagerClientSettings::GetCommonEventType() const
{
	return commonEventType;
}

void PayloadManagerClientSettings::SetCommonEventType(const string& newValue)
{
	commonEventType = newValue;
}

const string& PayloadManagerClientSettings::GetCommonEventDomain() const
{
	return commonEventDomain;
}

void PayloadManagerClientSettings::SetCommonEventDomain(const string& newValue)
{
	commonEventDomain = newValue;
}

const PayloadManagerClientSettings::PayloadDescription& PayloadManagerClientSettings::GetPayloadDescription() const
{
	return payloadDescription;
}

void PayloadManagerClientSettings::SetPayloadDescription(const PayloadDescription& newValue)
{
	payloadDescription = newValue;
}

void PayloadManagerClientSettings::Load(const string& fileName)
{
	struct stat statBuf;
	int status = stat(fileName.c_str(), &statBuf);
	int errSave = errno;

	if (!status)
	{
		if (!statBuf.st_size)
		{
			PayloadManagerClientSettings temp;
			temp.Save(fileName);
		}

		ifstream ifs(fileName.c_str());

		if (!ifs.good())
		{
			stringstream msg;
			msg << className << "::Load: could not open '" << fileName << "' for read";
			throw invalid_argument(msg.str());
		}

		xml_iarchive ia(ifs);
		ia >> make_nvp(rootLevel, *this);
	}
	else
	{
		char tempBuffer[256];
		strerror_r(errSave, tempBuffer, sizeof(tempBuffer));

		stringstream msg;

		msg << className << "::Load: could not stat '" << fileName << "'"
		    << " (errno=" << errSave << " '" << tempBuffer << "')";
		throw invalid_argument(msg.str());
	}
}

void PayloadManagerClientSettings::Save(const string& fileName) const
{
	ofstream ofs(fileName.c_str());

	if (!ofs.good())
	{
		stringstream msg;
		msg << className << "::Save: could not open '" << fileName << "' for write";
		throw invalid_argument(msg.str());
	}

	xml_oarchive oa(ofs);
	oa << make_nvp(rootLevel, *this);
}

ostream& operator<<(ostream& stm, const PayloadManagerClientSettings& settings)
{
	stm << "serviceName='" << settings.GetServiceName() << "'"
	    << " serviceKind='" << settings.GetServiceKind() << "'"
	    << " deviceAddedEventnName='" << settings.GetDeviceAddedEventName() << "'"
	    << " deviceChangedEventName='" << settings.GetDeviceChangedEventName() << "'"
	    << " deviceRemovedEventName='" << settings.GetDeviceRemovedEventName() << "'"
	    << " commonEventTye='" << settings.GetCommonEventType() << "'"
	    << " commonEventDomain='" << settings.GetCommonEventDomain() << "'";

	return stm;
}

}
}
