//---------------------------------------------------------------------------------------
// <copyright file="SBGPCTDSensor_Header.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 Header inner class, which carries parsed
//  output from the uh 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>

using namespace std;
using namespace LiquidRobotics::SMACFramework;

namespace LiquidRobotics {
namespace SeabirdGPCTD {

SBGPCTDSensor::Header::Header()
{
}

SBGPCTDSensor::Header::Header(const string& _line)
: line(_line)
, castNumber(0)
, timestamp(0)
, firstSample(0)
, lastSample(0)
, interval(0)
{
    vector<string> columns;
	const string trimVer = boost::trim_copy(line);
	boost::split(columns, trimVer, boost::is_any_of(" \t\r\n,="), boost::token_compress_on);

	if ((columns.size() >= 12)
     && boost::equals(columns[0], "cast")
	 && boost::equals(columns[6], "samples")
	 && boost::equals(columns[8], "to")
	 && boost::equals(columns[10], "int"))
	{
	    try
		{
		    castNumber = boost::lexical_cast<unsigned>(columns[1]);
			timeStr = columns[2]+" "+columns[3]+" "+columns[4]+" "+columns[5];
			firstSample = boost::lexical_cast<unsigned>(columns[7]);
			lastSample = boost::lexical_cast<unsigned>(columns[9]);
			interval = boost::lexical_cast<unsigned>(columns[11]);
			string stop = ", stop = ";
			size_t fnd = line.find(stop);
			if (fnd != string::npos)
			    stopState = line.substr(fnd+stop.length());
		}
		catch(exception& ex)
		{
		    *this = Header();
		}
	}
}

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

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

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

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

	ADD(line);
	ADD(castNumber);
	ADD(timeStr);
	ADD(firstSample);
	ADD(lastSample);
	ADD(interval);
	ADD(stopState);

	return msg.str();
}

}
}
