//----------------------------------------------------------------------------------
// <copyright file="EventSettings.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 EventSettings class, a serializable entity that holds the
//   name and type settings that identify an event.
// </summary>
//
// <owner>Mike Cookson</owner>
//----------------------------------------------------------------------------------

#include "EventSettings.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[] = "EventSettings";

namespace LiquidRobotics {
namespace EventLib {

EventSettings::EventSettings()
: eventName("event-name")
//, eventType
{

}

EventSettings::EventSettings(const string& _eventName, const string& _eventTypeName, const string& _eventDomainName)
: eventName(_eventName)
, eventType(_eventTypeName, _eventDomainName)
{
}

EventSettings::EventSettings(const string& _eventName, const EventType& _eventType)
: eventName(_eventName)
, eventType(_eventType)
{
}

EventSettings::EventSettings(const EventSettings& other)
: eventName(other.eventName)
, eventType(other.eventType)
{
}

EventSettings& EventSettings::operator=(const EventSettings& other)
{
	if (this != &other)
	{
		eventName = other.eventName;
		eventType = other.eventType;
	}

	return *this;
}

bool EventSettings::operator==(const EventSettings& other) const
{
	bool result = (this == &other);

	if (!result)
		result = !eventName.compare(other.eventName)
		      && (eventType == other.eventType);

	return result;
}

bool EventSettings::operator!=(const EventSettings& other) const
{
	const bool isEqual = (*this == other);

	return !isEqual;
}

const string& EventSettings::GetName() const
{
	return eventName;
}

void EventSettings::SetName(const string& newValue)
{
	eventName = newValue;
}

const string& EventSettings::GetTypeName() const
{
	return eventType.GetTypeName();
}

void EventSettings::SetTypeName(const string& newValue)
{
	eventType.SetTypeName(newValue);
}

const string& EventSettings::GetDomainName() const
{
	return eventType.GetDomainName();
}

void EventSettings::SetDomainName(const string& newValue)
{
	eventType.SetDomainName(newValue);
}

const EventType& EventSettings::GetType() const
{
	return eventType;
}

void EventSettings::SetType(const EventType& newValue)
{
	eventType = newValue;
}

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

	if (!status)
	{
		if (!statBuf.st_size)
		{
			EventSettings 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 EventSettings::Save(const string& fileName)
{
	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);
}

bool EventSettings::Empty() const
{
	return eventName.empty() || eventType.GetTypeName().empty();
}

ostream& operator<<(ostream& stm, const EventSettings& settings)
{
	stm << settings.GetName() << ":" << settings.GetType();
	return stm;
}

}
}
