//-------------------------------------------------------------------------------------------
// <copyright file="DataRecorderSettings.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 DataRecorderSettings class, an entity that holds the configurations
//  for the DataRecorder process
// </summary>
//
// <owner>Raj Jha</owner>
//-------------------------------------------------------------------------------------------

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

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

namespace LiquidRobotics {
namespace DataRecorder {

DataRecorderSettings::DataRecorderSettings() :
	outputFormat(1) {
}

DataRecorderSettings::~DataRecorderSettings() {
}

const string& DataRecorderSettings::GetServerName() const {
	return serverName;
}

void DataRecorderSettings::SetServerName(const string& newValue) {
	serverName = newValue;
}

const EventSettings& DataRecorderSettings::GetCommandCompleteEventSettings() const {
	return commandCompleteEventSettings;
}

void DataRecorderSettings::SetCommandCompleteEventSettings(
		const EventSettings& newValue) {
	commandCompleteEventSettings = newValue;
}

const EventSettings& DataRecorderSettings::GetGPSEventSettings() const {
	return gpsEventSettings;
}

void DataRecorderSettings::SetGPSEventSettings(const EventSettings& newValue) {
	gpsEventSettings = newValue;
}

const EventSettings& DataRecorderSettings::GetTelemetryChangedEventSettings() const {
	return telemetryChangedEventSettings;
}

void DataRecorderSettings::SetTelemetryChangedEventSettings(
		const EventSettings& newValue) {
	telemetryChangedEventSettings = newValue;
}

const DataRecorderSettings::EventTransceiverSettings& DataRecorderSettings::GetEventTransceiverSettings() const {
	return eventTransceiverSettings;
}

void DataRecorderSettings::SetEventTransceiverSettings(
		const EventTransceiverSettings& newValue) {
	eventTransceiverSettings = newValue;
}

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

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

		ifstream ifs(fileName.c_str());

		if (!ifs.good()) {
			stringstream msg;
			msg << "DataRecorderSettings::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 << "DataRecorderSettings::Load: could not stat '" << fileName
				<< "'" << " (errno=" << errSave << " '" << tempBuffer << "')";
		throw invalid_argument(msg.str());
	}
}

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

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

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

const DataRecorderSettings::PayloadManagerClientSettings& DataRecorderSettings::GetPayloadManagerClientSettings() const {
	return payloadManagerClientSettings;
}

void DataRecorderSettings::SetPayloadManagerClientSettings(
		const PayloadManagerClientSettings& newValue) {
	payloadManagerClientSettings = newValue;
}

} // namespace LiquidRobotics
} // namespace DataRecorder
