/*!
 * Shutter.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/shutter.h"


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

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

/*!  ----------------------------------------------------------------------------
 *  @fn from_json
 *
 *  @brief Load a Shutter Element from a Json Entry.
 */
int Shutter::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);
	}
	// zone
	if (json_object_object_get_ex(a_node, k_entry_zone, &the_value_node))
	{
		m_zone = 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);
	}
	// sender
	if (json_object_object_get_ex(a_node, k_entry_sender, &the_value_node))
	{
		m_sender = json_object_get_int(the_value_node);
	}
	// interruptor
	if (json_object_object_get_ex(a_node, k_entry_interruptor, &the_value_node))
	{
		m_interruptor = json_object_get_int(the_value_node);
	}
	// speed up
	if (json_object_object_get_ex(a_node, k_entry_speed_up, &the_value_node))
	{
		m_speed_up = json_object_get_int(the_value_node);
	}
	// speed down
	if (json_object_object_get_ex(a_node, k_entry_speed_down, &the_value_node))
	{
		m_speed_down = json_object_get_int(the_value_node);
	}

	return 0;
}

/*!  ----------------------------------------------------------------------------
 *  @fn to_json
 *
 *  @brief Return Shutter Object as a Json Object.
 */
struct json_object *Shutter::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()));
	// zone
	json_object_object_add(the_root_node, k_entry_zone, json_object_new_string(m_zone.c_str()));
	// state
	json_object_object_add(the_root_node, k_entry_state, json_object_new_boolean(m_state));
	// sender
	json_object_object_add(the_root_node, k_entry_sender, json_object_new_int(m_sender));
	// interruptor
	json_object_object_add(the_root_node, k_entry_interruptor, json_object_new_int(m_interruptor));
	// speed up
	json_object_object_add(the_root_node, k_entry_speed_up, json_object_new_int(m_speed_up));
	// speed down
	json_object_object_add(the_root_node, k_entry_speed_down, json_object_new_int(m_speed_down));

	return the_root_node;
}

/*!  ----------------------------------------------------------------------------
 *  @fn update
 *
 *  @brief Update the Shutter State.
 */
int Shutter::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 Shutter.
 */
uint16_t Shutter::get_id(void)
{
	return m_id;
}

/*!  ----------------------------------------------------------------------------
 *  @fn send_state
 *
 *  @brief Send the Curent State of the Light.
 */
int Shutter::send_state(void)
{
	return 0;
}
