//-----------------------------------------------------------------------------------------
// <copyright file="LqrSMACCmdOutputFormatter_TimestampValue.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>
// </summary>
//
// <owner></owner>
//-----------------------------------------------------------------------------------------

#include "LqrSMACCmdOutputFormatter.h"
#include "nsextend.h"
#include <sstream>
#include <stdexcept>
#include <boost/date_time.hpp>
#include <boost/date_time/c_local_time_adjustor.hpp>

using namespace std;
using namespace boost::posix_time;

namespace LiquidRobotics {
namespace SMACFramework {

CmdOutputFormatter::TimestampValue::TimestampValue(bool _localTime)
: localTime(_localTime)
, timeStamp(boost::date_time::not_a_date_time)
{
}

CmdOutputFormatter::TimestampValue::TimestampValue(const TimestampValue& rhs)
: localTime(rhs.localTime)
, timeStamp(rhs.timeStamp)
{
}

CmdOutputFormatter::TimestampValue& CmdOutputFormatter::TimestampValue::operator=(const TimestampValue& rhs)
{
	if (this != &rhs)
	{
		if (localTime != rhs.localTime)
			throw invalid_argument("Cannot reassign initialized value of local time");

		timeStamp = rhs.timeStamp;
	}

	return *this;
}

bool CmdOutputFormatter::TimestampValue::operator==(const TimestampValue& rhs) const
{
	const bool isEqual = (timeStamp == rhs.timeStamp);

	return isEqual;
}

const CmdOutputFormatter::TimestampValue::Timestamp& CmdOutputFormatter::TimestampValue::Get() const
{
	return timeStamp;
}

void CmdOutputFormatter::TimestampValue::Get(timeval& tv) const
{
	using namespace boost::date_time;
	typedef c_local_adjustor<ptime> local_adj;

	ptime time0 = timeStamp;

	if (!localTime)
		time0 = local_adj::utc_to_local(time0);

	boost::gregorian::date epoch(1970,Jan,1);
	ptime timet_start(epoch, hours(0));

	if (!timeStamp.is_not_a_date_time() || (time0 < timet_start))
	{
		tv.tv_sec = 0;
		tv.tv_usec = 0;
	}
	else
	{
		ptime::time_duration_type dur = time0-timet_start;
		ptime::time_duration_type dur2 = dur-seconds(dur.total_seconds());

		tv.tv_sec =  dur.total_seconds();
		tv.tv_usec = dur2.total_microseconds();
	}
}

void CmdOutputFormatter::TimestampValue::Set(const Timestamp& newValue)
{
	timeStamp = newValue;
}

void CmdOutputFormatter::TimestampValue::Set(const timeval& newValue)
{
	using namespace boost::date_time;

	boost::gregorian::date epoch(1970,Jan,1);
	ptime timet_start(epoch, hours(0));
	ptime newStamp = timet_start+seconds(newValue.tv_sec)+microseconds(newValue.tv_usec);

	timeStamp = newStamp;
}

void CmdOutputFormatter::TimestampValue::SetNow()
{
	timeStamp = microsec_clock::local_time();
}

string CmdOutputFormatter::TimestampValue::ToString() const
{
	using namespace boost::gregorian;

	string returnValue;

	if (timeStamp.is_not_a_date_time())
		returnValue = "not set";
	else
	{
		stringstream result;
		greg_year_month_day ymd = timeStamp.date().year_month_day();
		ptime::time_duration_type tod = timeStamp.time_of_day();
		ptime::time_duration_type tod2 = tod-seconds(tod.total_seconds());

		result << ultos(ymd.year,10,4) << "/" << ultos(ymd.month.as_number(),10,2) << "/" << ultos(ymd.day,10,2) << " "
			   << ultos(tod.hours(),10,2) << ":" << ultos(tod.minutes(),10,2) << ":" << ultos(tod.seconds(),10,2)
			   //<< "." << ultos(tod.total_microseconds() % 1000000,10,6);
			   //<< "." << ultos(tod.microseconds(),10,6);
			   << "." << ultos(tod2.total_microseconds(),10,6);

		returnValue = result.str();
	}

	return returnValue;
}

bool CmdOutputFormatter::TimestampValue::WasSet() const
{
	return !timeStamp.is_not_a_date_time();
}


}
}
