/*!
 * timer.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: 11/04/2018
 *
 */

/*------------------------------- INCLUDES ----------------------------------*/

#include <json-c/json.h>

#include "domo.h"

#include "helpers/strings.h"

#include "timer.h"

/*------------------------------- DEFINES ----------------------------------*/

/*!  ----------------------------------------------------------------------------
 *  @fn Timer
 *
 *  @brief Constructor of Timer Object.
 */
Timer::Timer(void) : m_active(false),
                     m_hour(0),
                     m_minute(0),
                     m_recurrence(0)
{
}

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

/*!  ----------------------------------------------------------------------------
 *  @fn from_json
 *
 *  @brief Load Timer Object.
 */
int Timer::from_json(struct json_object *a_node)
{
    struct json_object *the_value_node;

    // active
    if (json_object_object_get_ex(a_node, k_entry_active, &the_value_node))
    {
        m_active = json_object_get_boolean(the_value_node);
    }

    // start time
    if (json_object_object_get_ex(a_node, k_entry_start_time, &the_value_node))
    {
        std::size_t the_separator;
        std::string the_start_time;
        the_start_time = json_object_get_string(the_value_node);
        the_separator = the_start_time.find_last_of(":");

        m_hour = std::stoi(the_start_time.substr(0, the_separator));
        m_minute = std::stoi(the_start_time.substr(the_separator + 1));
    }

    // recurrence
    if (json_object_object_get_ex(a_node, k_entry_recurrence, &the_value_node))
    {
        m_recurrence = json_object_get_int(the_value_node);
    }

    return 0;
}

/*!  ----------------------------------------------------------------------------
 *  @fn to_json
 *
 *  @brief Export Timer Object as JSON Object.
 */
struct json_object *Timer::to_json(void) const
{
    struct json_object *the_root_node;

    the_root_node = json_object_new_object();

    // active
    json_object_object_add(the_root_node, k_entry_active, json_object_new_boolean(m_active));

    //start_time
    std::string the_start_time = string_sprintf("%.2d:%.2d", m_hour, m_minute);
    json_object_object_add(the_root_node, k_entry_start_time, json_object_new_string(the_start_time.c_str()));

    // recurrence
    json_object_object_add(the_root_node, k_entry_recurrence, json_object_new_int(m_recurrence));

    return the_root_node;
}

/*!  ----------------------------------------------------------------------------
 *  @fn is_active
 *
 *  @brief return true if the Timer is active.
 */
bool Timer::is_active(void) const
{
    return m_active;
}

/*!  ----------------------------------------------------------------------------
 *  @fn get_hour
 *
 *  @brief Return the Hour of the Timer.
 */
uint8_t Timer::get_hour(void) const
{
    return m_hour;
}

/*!  ----------------------------------------------------------------------------
 *  @fn get_minute
 *
 *  @brief Return the Minute of the Timer.
 */
uint8_t Timer::get_minute(void) const
{
    return m_minute;
}

/*!  ----------------------------------------------------------------------------
 *  @fn get_recurrence
 *
 *  @brief Return the Recurrence of the Timer.
 */
uint16_t Timer::get_recurrence(void) const
{
    return m_recurrence;
}

/*!  ----------------------------------------------------------------------------
 *  @fn is_day_active
 *
 *  @brief Return true if the current day is active on the timer.
 *        day mask will be:
 *        - 1 Sunday
 *        - 2 Monday
 *        - 4 Tuesday
 *        - 8 Wednesday
 *        -16 Thursday
 *        -32 Friday
 *        -64 Saturday
 */
bool Timer::is_day_active(uint8_t a_wday) const
{
    printf("debug: mask: %d day active: %s\n", (1 << a_wday), (m_recurrence & (1 << a_wday))?"true":"false");

    if (m_recurrence == 0)
    {
        // No recurrence
        return true;
    }
    return  (m_recurrence & (1 << a_wday));
}
