//-------------------------------------------------------------------------------------------------
// <copyright file="LWControllerSettings.cpp" company="LiquidRobotics">
//  Copyright (c) Liquid Robotics Corporation, confidential and proprietary.  All rights reserved. 
// </copyright>
//
// <summary>
//	Implementation of LWControllerSettings class, a boost-serializable entity
//	representing the configurable settings for the controller class.
// </summary>
//
// <owner>Mike Cookson</owner>
//-------------------------------------------------------------------------------------------------

#include "LWControllerSettings.h"
#include "LqrSMACLoadSaveBoostXml.h"

using namespace std;
using namespace LiquidRobotics::SMACFramework;

namespace LiquidRobotics {
namespace LuaWrapper {

LWControllerSettings::LWControllerSettings()
: defaultFormatter("xml")
{
	SerialConfig sc;
	serialConfigMap["example"] = sc;
}

LWControllerSettings::LWControllerSettings(const LWControllerSettings& rhs)
: defaultFormatter(rhs.defaultFormatter)
, serialConfigMap(rhs.serialConfigMap.begin(), rhs.serialConfigMap.end())
{
}

LWControllerSettings& LWControllerSettings::operator=(const LWControllerSettings& rhs)
{
	if (this != &rhs)
	{
		defaultFormatter = rhs.defaultFormatter;
		serialConfigMap.clear();
		serialConfigMap.insert(rhs.serialConfigMap.begin(), rhs.serialConfigMap.end());
	}

	return *this;
}

const string& LWControllerSettings::GetDefaultFormatter() const
{
	return defaultFormatter;
}

void LWControllerSettings::SetDefaultFormatter(const string& newValue)
{
	defaultFormatter = newValue;
}

const LWControllerSettings::SerialConfig& LWControllerSettings::GetSerialConfig(const string& configName) const
{
	SerialConfigMap::const_iterator ci = serialConfigMap.find(configName);

	if (ci == serialConfigMap.end())
		throw invalid_argument("LWControllerSettings::GetSerialConfig: collection is missing indexed item");

	return ci->second;
}

const LWControllerSettings::SerialConfigMap& LWControllerSettings::GetSerialConfigMap() const
{
	return serialConfigMap;
}

bool LWControllerSettings::HasSerialConfig(const string& configName) const
{
	const bool result = serialConfigMap.find(configName) != serialConfigMap.end();
	return result;
}

void LWControllerSettings::SetSerialConfigMap(const SerialConfigMap& newValue)
{
	if (&serialConfigMap != &newValue)
	{
		serialConfigMap.clear();
		serialConfigMap.insert(newValue.begin(), newValue.end());
	}
}

void LWControllerSettings::Load(const string& fileName, bool withMD5)
{
	if (withMD5)
		LoadBoostXmlMD5(fileName, *this, true);
	else
		LoadBoostXml(fileName, *this);
}

void LWControllerSettings::Save(const string& fileName, bool withMD5) const
{
	if (withMD5)
		SaveBoostXmlMD5(fileName, *this, true);
	else
		SaveBoostXml(fileName, *this);
}

}
}
