/*!
 * clock.cpp
 *
 * Copyright (c) 2016-2018, 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 "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 is_equal_to_timer
 *
 *  @brief return true if the clock is equal to current time.
 */
bool Clock::is_equal_to_timer(const Timer &a_timer)
{
	struct tm the_compared_time;
	double the_diff;
	uint8_t the_wday;
	time_t the_time = time(NULL);

	the_compared_time = *localtime(&the_time);
	the_wday = the_compared_time.tm_wday;

#if 0
	printf ("=>Timer: (%.2d h %.2d) Now: (%.2d h %.2d) week day:%d\n", a_timer.get_hour(),a_timer.get_minute(), 
		the_compared_time.tm_hour, the_compared_time.tm_min, the_compared_time.tm_wday);
#endif	
	the_compared_time.tm_hour = a_timer.get_hour();
	the_compared_time.tm_min = a_timer.get_minute();

	the_diff = difftime(the_time, mktime(&the_compared_time));
	// printf ("%.f seconds of diff between current time and Timer time.\n", the_diff);
	
	if (the_diff != 0)
		return false;

	// the Clock is OK check the recurrence
	return a_timer.is_day_active(the_wday);
}

/*!  ----------------------------------------------------------------------------
 *  @fn is_expired
 *
 *  @brief Return true if the curent action has expired.
 */
bool Clock::is_expired(const Sequence &a_sequence)
{
	time_t the_expired;
	struct tm the_compared_time;
	double the_diff;

	time_t the_time = time(NULL);
	the_compared_time = *localtime(&the_time);

	// printf ("=>Now: (%.2d h %.2d)\n", the_compared_time.tm_hour, the_compared_time.tm_min);


	the_compared_time.tm_hour = a_sequence.get_timer().get_hour();
	the_compared_time.tm_min = a_sequence.get_timer().get_minute();

	the_expired = mktime(&the_compared_time) + (a_sequence.get_duration() * 60);

	the_diff = difftime(the_time, the_expired);

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

	if (the_diff == 0)
		return true;

	return false;
}
