/*!
 * Event.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: 19/05/2016
 *
 */

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

#include <json-c/json.h>

#include "devices/devices-manager.h"

#include "domo.h"

#include "action.h"

/*!  ----------------------------------------------------------------------------
 *  @fn Action
 *
 *  @brief Constructor of Action Object.
 */
Action::Action(void) : m_device_id(0),
					   m_state(false),
					   m_duration(-1)

{
}

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

/*!  ----------------------------------------------------------------------------
 *  @fn from_json
 *
 *  @brief Load Action Object.
 */
int Action::from_json(struct json_object *a_node)
{
	struct json_object *the_value_node;
	
	// device_id
	if (json_object_object_get_ex(a_node, k_entry_device_id, &the_value_node))
	{
		std::size_t the_separator;
		std::string the_id_elem;
		the_id_elem = json_object_get_string(the_value_node);
		the_separator = the_id_elem.find_last_of("/");

		m_capability = the_id_elem.substr(0, the_separator);
		m_device_id = std::stoi(the_id_elem.substr(the_separator + 1));
	}

	// state
	if (json_object_object_get_ex(a_node, k_entry_state, &the_value_node))
	{
		m_state = json_object_get_boolean(the_value_node);
	}

	// duration
	if (json_object_object_get_ex(a_node, k_entry_duration, &the_value_node))
	{
		m_duration = json_object_get_int(the_value_node);
	}

	return 0;
}

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

	the_root_node = json_object_new_object();

	// device_id
	std::string the_device_id = m_capability + "/" + std::to_string(m_device_id);
	json_object_object_add(the_root_node, k_entry_device_id, json_object_new_string(the_device_id.c_str()));

	// state
	json_object_object_add(the_root_node, k_entry_state, json_object_new_boolean(m_state));

	// duration
	if (m_duration != -1)
	{
		json_object_object_add(the_root_node, k_entry_duration, json_object_new_int(m_duration));
	}

	return the_root_node;
}

/*!  ----------------------------------------------------------------------------
 *  @fn start
 *
 *  @brief Start the Action.
 */
#if 0
int Action::start(DevicesManager *a_device_manager)
{
	set_in_progress(true);

	return a_device_manager->set_state(m_capability, m_device_id, true);
}
#endif

/*!  ----------------------------------------------------------------------------
 *  @fn stop
 *
 *  @brief Stop the Action.
 */
#if 0
int Action::stop(DevicesManager *a_device_manager)
{
	set_in_progress(false);

	return a_device_manager->set_state(m_capability, m_device_id, false);
}
#endif
/*!  ----------------------------------------------------------------------------
 *  @fn dump
 *
 *  @brief dump the Event Object.
 */
#if 0
void Action::dump(void)
{
	fprintf(stdout, "Timer Event:\n");
	fprintf(stdout, "=========== \n");
	fprintf(stdout, "  - active: %s\n", (m_active ? "true" : "false"));
	fprintf(stdout, "  - capability: %s\n", m_capability.c_str());
	fprintf(stdout, "  - id: %d\n", m_id);
	fprintf(stdout, "  - device_id: %d\n", m_device_id);
	fprintf(stdout, "  - Time: %2d:%2d\n", m_hour, m_minute);
	fprintf(stdout, "  - recurrence: %d\n", m_recurrence);
	fprintf(stdout, "  - duration: %d\n", m_duration);
	fprintf(stdout, "  - action: %s\n", m_action.c_str());
}
#endif

/*!  ----------------------------------------------------------------------------
 *  @fn get_device_id
 *
 *  @brief return the Device ID of the Event.
 */
uint16_t Action::get_device_id(void) const
{
	return m_device_id;
}

/*!  ----------------------------------------------------------------------------
 *  @fn get_capability
 *
 *  @brief return the capability of the Event.
 */
const std::string &Action::get_capability(void) const
{
	return m_capability;
}

/*!  ----------------------------------------------------------------------------
 *  @fn get_state
 *
 *  @brief return the state for this action.
 */
bool Action::get_state(void) const
{
	return m_state;
}

/*!  ----------------------------------------------------------------------------
 *  @fn get_duration
 *
 *  @brief Return the Duration of the Event.
 */
int16_t Action::get_duration(void) const
{
	return m_duration;
}

#if 0
/*!  ----------------------------------------------------------------------------
 *  @fn is_in_progress
 *
 *  @brief Return true if the Event Is in Progress.
 */
bool Action::is_in_progress(void) const
{
	return m_in_progress;
}

/*!  ----------------------------------------------------------------------------
 *  @fn set_in_progress
 *
 *  @brief Set the InProgress to a new state.
 */
void Action::set_in_progress(bool a_state)
{
	m_in_progress = a_state;
}
#endif
