//--------------------------------------------------------------------------------
// <copyright file="LqrSMACTimeCounter.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 LqrSMACTimeCounter class, which counts time in milliseconds
//   and provides a way of reading how much time is left
// </summary>
//
// <owner>Mike Cookson</owner>
//--------------------------------------------------------------------------------

#include "LqrSMACTimeCounter.h"
#include "LqrSMACMonotonic.h"
#include <stdexcept>

using namespace std;

namespace LiquidRobotics {
namespace SMACFramework {

TimeCounter::TimeCounter(unsigned ms, bool _running)
: running(_running)
, startTime(_running ? GetMonotonicTimerSeconds() : ms/1000.0)
{
}

TimeCounter::~TimeCounter()
{
}

unsigned TimeCounter::GetCount() const
{
	unsigned result = (unsigned) floor(1000*GetCountSec());
	return result;
}

double TimeCounter::GetCountSec() const
{
	Lock lock(const_cast<TimeCounter&>(*this));
	double result = running ? (GetMonotonicTimerSeconds()-startTime) : startTime;
	return result;
}

bool TimeCounter::IsRunning() const
{
	return running;
}

void TimeCounter::Pause()
{
	Lock lock(*this);

	if (running)
	{
		startTime = GetCount();
		running = false;
	}
}

void TimeCounter::Reset(unsigned ms, bool _running)
{
	ResetSec(ms/1000.0, _running);
}

void TimeCounter::ResetSec(double sec, bool _running)
{
	Lock lock(*this);
	running = _running;
	startTime = running ? GetMonotonicTimerSeconds()-sec : sec;
}

void TimeCounter::Resume()
{
	Lock lock(*this);

	if (!running)
	{
		running = true;
		startTime = GetMonotonicTimerSeconds()-startTime;
	}
}

void TimeCounter::SetCount(unsigned ms)
{
	SetCountSec(1000.0*ms);
}

void TimeCounter::SetCountSec(double sec)
{
	Lock lock(*this);
	startTime = running ? GetMonotonicTimerSeconds()-sec : sec;
}

unsigned TimeCounter::Remaining(unsigned limit) const
{
	const unsigned result = (unsigned) floor(1000.0*RemainingSec(limit/1000.0));
	return result;
}

double TimeCounter::RemainingSec(double limit) const
{
	if (limit < 0) throw invalid_argument("TimeCounter::RemainingSec: limit must be a non-negative number");

	const double current = GetCountSec();
	const double result = (current > limit) ? 0 : (limit-current);
	return result;
}

}
}
