//------------------------------------------------------------------------------------------------
// <copyright file="SBGPCTDControllerPersistSettings.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>
// 	Declaration of SBGPCTDControllerPersistSettings, a serializable entity class that contains the
//  runtime-configurable persistent settings for an instance of SBGPCTDController
// </summary>
//
// <owner>Mike Cookson</owner>
//------------------------------------------------------------------------------------------------

#include "SBGPCTDControllerPersistSettings.h"
#include "LqrSMACLoadSaveBoostXml.h"
#include <sstream>
#include <boost/filesystem.hpp>

using namespace std;
using namespace LiquidRobotics::SMACFramework;

namespace LiquidRobotics {
namespace SeabirdGPCTD {

SBGPCTDControllerPersistSettings::SBGPCTDControllerPersistSettings()
{
	SetDefaults();
}

SBGPCTDControllerPersistSettings::SBGPCTDControllerPersistSettings(const string& _settingsPath)
: settingsPath(boost::filesystem::canonical(_settingsPath).string())
{
	SetDefaults();
}

SBGPCTDControllerPersistSettings::~SBGPCTDControllerPersistSettings()
{
}

bool SBGPCTDControllerPersistSettings::GetAutoRun() const
{
	return autoRun;
}

void SBGPCTDControllerPersistSettings::SetAutoRun(bool newValue)
{
	autoRun = newValue;
}

template<typename T>
void RangeCheck(const T& var, const string& varName, const T minVal, const T maxVal)
{
	if ((var < minVal) || (var > maxVal))
	{
		stringstream msg;
		msg << varName << " cannot be set to " << var << ": the valid range is " << minVal << ".." << maxVal;
		throw invalid_argument(msg.str());
	}
}

unsigned SBGPCTDControllerPersistSettings::GetDutyCycleSec() const
{
	return dutyCycleSec;
}

void SBGPCTDControllerPersistSettings::SetDutyCycleSec(unsigned newValue)
{
	RangeCheck(dutyCycleSec, "Duty cycle (sec)", (unsigned) 0, (unsigned) 24*3600);
	dutyCycleSec = newValue;
}

unsigned SBGPCTDControllerPersistSettings::GetFlushTimeSec() const
{
	return flushTimeSec;
}

void SBGPCTDControllerPersistSettings::SetFlushTimeSec(unsigned newValue)
{
	RangeCheck(newValue, "Flush time (sec)", (unsigned) 0, (unsigned) 110);
	flushTimeSec = newValue;
}

bool SBGPCTDControllerPersistSettings::GetLoggingEnabled() const
{
	return loggingEnabled;
}

void SBGPCTDControllerPersistSettings::SetLoggingEnabled(bool newValue)
{
	loggingEnabled = newValue;
}

bool SBGPCTDControllerPersistSettings::GetRawLoggingEnabled() const
{
	return rawLoggingEnabled;
}

void SBGPCTDControllerPersistSettings::SetRawLoggingEnabled(bool newValue)
{
	rawLoggingEnabled = newValue;
}

unsigned SBGPCTDControllerPersistSettings::GetReportBlockSize() const
{
	return reportBlockSize;
}

void SBGPCTDControllerPersistSettings::SetReportBlockSize(unsigned newValue)
{
	RangeCheck(newValue, "Report block size", (unsigned) 1, (unsigned) 10);
	reportBlockSize = newValue;
}

unsigned SBGPCTDControllerPersistSettings::GetSamplePeriodSec() const
{
	return samplePeriodSec;
}

void SBGPCTDControllerPersistSettings::SetSamplePeriodSec(unsigned newValue)
{
	RangeCheck<unsigned>(newValue, "Sample period (sec)", 1, 3600);
	samplePeriodSec = newValue;
}

void SBGPCTDControllerPersistSettings::Load()
{
	Load(settingsPath);
}

void SBGPCTDControllerPersistSettings::Load(const string& fileName)
{
	try
	{
		LoadBoostXmlMD5(fileName, *this);
	}
	catch(exception& ex)
	{
		SetDefaults();
		throw;
	}
}

void SBGPCTDControllerPersistSettings::Save() const
{
	Save(settingsPath);
}

void SBGPCTDControllerPersistSettings::Save(const string& fileName) const
{
	SaveBoostXmlMD5(fileName, *this);
}

void SBGPCTDControllerPersistSettings::SetDefaults()
{
	autoRun = false;
	dutyCycleSec = 0;
	flushTimeSec = 5;
	reportBlockSize = 8;
	samplePeriodSec = 60;
}

}
}



