//-------------------------------------------------------------------------------------------
// <copyright file="SBGPCTDSensor_ConfigData.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 methods of SBGPCTDSensor's ConfigData inner class, which carries parsed
//  output from the getcd command
// </summary>
//
// <owner>Mike Cookson</owner>
//-------------------------------------------------------------------------------------------

#include "SBGPCTDSensor.h"
#include "LqrSMACEscapeStr.h"
#include <cmath>
#include <string>
#include <vector>
#include <boost/algorithm/string.hpp>
#include <boost/lexical_cast.hpp>
#include <iostream>
#include "pugixml.hpp"

using namespace std;
using namespace LiquidRobotics::SMACFramework;

namespace LiquidRobotics {
namespace SeabirdGPCTD {

SBGPCTDSensor::ConfigData::ConfigData()
{
}

SBGPCTDSensor::ConfigData::ConfigData(const string& _line)
: line(_line)
{
	pugi::xml_document doc;
	doc.load_buffer(line.c_str(), line.length());

	for (pugi::xml_node n = doc.first_child(); n; n = n.next_sibling())
		if (boost::iequals(n.name(), "ConfigurationData"))
		{
			for (pugi::xml_attribute a = n.first_attribute(); a; a = a.next_attribute())
			{
				if (boost::iequals(a.name(), "DeviceType"))
					deviceType = a.value();
				else if (boost::iequals(a.name(), "SerialNumber"))
					serialNumber = a.value();
			}

			for (pugi::xml_node c = n.first_child(); c; c = c.next_sibling())
			{
				if (boost::iequals(c.name(), "SampleDataFormat"))
					outputFormat = c.child_value();
				else if (boost::iequals(c.name(), "RS232ForceOn"))
					rs232ForceOn = c.child_value();
				else if (boost::iequals(c.name(), "TxRealTime"))
					txRealTime = c.child_value();
				else if (boost::iequals(c.name(), "SampleInterval"))
					sampleInterval = c.child_value();
				else if (boost::iequals(c.name(), "SampleMode"))
					sampleMode = c.child_value();
				else if (boost::iequals(c.name(), "AutoRun"))
					autoRun = c.child_value();
				else if (boost::iequals(c.name(), "SBE43"))
					sbe43 = c.child_value();
				else if (boost::iequals(c.name(), "MinCondFreq"))
					minCondFreq = c.child_value();
				else if (boost::iequals(c.name(), "CustomPumpMode"))
					customPumpMode = c.child_value();
			}

			break;
		}
}

inline void add(stringstream& stm, const string& name, const string& value)
{
	if (stm.tellp()) stm.put(' ');
	stm << name << "='" << EscapeStr(value) << "'";
}

#define ADD(var) add(msg,#var,var)

string SBGPCTDSensor::ConfigData::ToString() const
{
	stringstream msg;

	ADD(line);
	ADD(deviceType);
	ADD(serialNumber);
	ADD(outputFormat);
	ADD(rs232ForceOn);
	ADD(txRealTime);
	ADD(sampleInterval);
	ADD(sampleMode);
	ADD(autoRun);
	ADD(sbe43);
	ADD(minCondFreq);
	ADD(customPumpMode);

	return msg.str();
}

}
}
