/*!
 * Sprinkler.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: 21/05/2016
 *
 */

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

#include <cstdio>

#include <json-c/json.h>

#include <ubus-cpp/ubus-call.h>

#include "domo.h"

#include "devices/sprinkler.h"

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

#define k_entry_station "station"

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

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

/*!  ----------------------------------------------------------------------------
 *  @fn from_json
 *
 *  @brief Load a Sprinkler Element from a Json Entry.
 */
int Sprinkler::from_json(struct json_object *a_node)
{
	struct json_object *the_value_node;
	// TODO: Should shared with Light.
	// id
	if (json_object_object_get_ex(a_node, k_entry_id, &the_value_node))
	{
		m_id = json_object_get_int(the_value_node);
	}
	// name
	if (json_object_object_get_ex(a_node, k_entry_name, &the_value_node))
	{
		m_name = json_object_get_string(the_value_node);
	}
	// speach_name
	if (json_object_object_get_ex(a_node, k_entry_speach_name, &the_value_node))
	{
		m_speach_name = json_object_get_string(the_value_node);
	}
	// state
	if (json_object_object_get_ex(a_node, k_entry_state, &the_value_node))
	{
		m_state = json_object_get_boolean(the_value_node);
	}

	return 0;
}

/*!  ----------------------------------------------------------------------------
 *  @fn to_json
 *
 *  @brief Return Sprinkler Object as a Json Object.
 */
struct json_object *Sprinkler::to_json(void) const
{
	struct json_object *the_root_node;

	the_root_node = json_object_new_object();

	// id
	json_object_object_add(the_root_node, k_entry_id, json_object_new_int(m_id));
	// name
	json_object_object_add(the_root_node, k_entry_name, json_object_new_string(m_name.c_str()));
	// speach_name
	json_object_object_add(the_root_node, k_entry_speach_name, json_object_new_string(m_speach_name.c_str()));
	// state
	json_object_object_add(the_root_node, k_entry_state, json_object_new_boolean(m_state));

	return the_root_node;	
}

/*!  ----------------------------------------------------------------------------
 *  @fn update
 *
 *  @brief Update the Sprinkler State.
 */
int Sprinkler::update(bool a_state)
{
	if (m_state == a_state)
		return 1;

	m_state = a_state;

	return send_state();
}

/*!  ----------------------------------------------------------------------------
 *  @fn get_id
 *
 *  @brief Return the ID of the Sprinkler.
 */
uint16_t Sprinkler::get_id(void)
{
	return m_id;
}

/*!  ----------------------------------------------------------------------------
 *  @fn send_state
 *
 *  @brief Send the Curent State of the Light.
 */
int Sprinkler::send_state(void)
{
	int the_result;
	std::string the_output_result;
	struct json_object *the_parameter_doc;
	UBusCall the_cmd;

	the_parameter_doc = json_object_new_object();
	json_object_object_add(the_parameter_doc, k_entry_station, json_object_new_int(m_id));
	json_object_object_add(the_parameter_doc, k_entry_state, json_object_new_boolean(m_state));

	the_result = the_cmd.exec("sprinklers", "set", json_object_to_json_string(the_parameter_doc), the_output_result);

	json_object_put(the_parameter_doc);

	return the_result;
}
