/*!
 * Clock.cpp
 *
 * Copyright (c) 2016, NADAL Jean-Baptiste. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 * MA 02110-1301 USA
 * 
 * @Author:  NADAL Jean-Baptiste
 * @Date: 20/05/2016
 *
 */
/*------------------------------- INCLUDES ----------------------------------*/

#include <cstdio>

#include "timers/Clock.h"


/*!  ----------------------------------------------------------------------------
 *  @fn Clock
 *
 *  @brief Constructor of the Clock Object.
 */
Clock::Clock (void)
{
}


/*!  ----------------------------------------------------------------------------
 *  @fn ~Clock
 *
 *  @brief Destructor of the Clock Object.
 */
Clock::~Clock (void)
{
}


/*!  ----------------------------------------------------------------------------
 *  @fn isEqual
 *
 *  @brief Set an Event to the Clock.
 */
int Clock::set (const Event &anEvent)
{
	mEvent = anEvent;

	mNow = time (NULL);

	return 0;
}


/*!  ----------------------------------------------------------------------------
 *  @fn isEqualToCurrentTime
 *
 *  @brief return true if the clock is equal to current time.
 */
bool Clock::isEqualToCurrentTime (void)
{
	struct tm theComparedTime;
	double theDiff;

	theComparedTime = *localtime (&mNow);

	theComparedTime.tm_hour = mEvent.getHour();
	theComparedTime.tm_min = mEvent.getMinute();

	theDiff = difftime (mNow, mktime (&theComparedTime));

	//printf ("%.f seconds of diff between current time and Timer time.\n", theDiff);

	if (theDiff == 0)
		return true;

	return false;
}


/*!  ----------------------------------------------------------------------------
 *  @fn isExpired
 *
 *  @brief Return true if the curent time has expired.
 */
bool Clock::isExpired (void)
{
	time_t theExpired;
	struct tm theComparedTime;
	double theDiff;

	theComparedTime = *localtime (&mNow);

	theComparedTime.tm_hour = mEvent.getHour();
	theComparedTime.tm_min = mEvent.getMinute();

	theExpired = mktime (&theComparedTime) + (mEvent.getDuration() * 60);

	theDiff = difftime (mNow, theExpired);

	//printf ("%.f seconds of diff between %lld and %lld.\n", theDiff, mNow, theExpired);

	if (theDiff == 0)
		return true;

	return false;
}
