/*!
 * LightDevice.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 <json-c/json.h>

#include "devices/light.h"

#include "devices/light-device.h"

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

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

/*!  ----------------------------------------------------------------------------
 *  @fn from_json
 *
 *  @brief Load LightDevice Object From a Json Object.
 */
int LightDevice::from_json(struct json_object *a_node)
{
	struct json_object *the_data_node;
	fprintf(stderr, "LightDevice::load_from_json\n");

	// speach name
	Device::from_json(a_node);

	// Get Light
	if (json_object_object_get_ex(a_node, k_entry_data, &the_data_node) && (json_object_get_type(the_data_node) == json_type_array))
	{
		int the_len;
		struct json_object *the_light_node;

		the_len = json_object_array_length(the_data_node);
		for (int i = 0; i < the_len; i++)
		{
			Light the_light;
			the_light_node = json_object_array_get_idx(the_data_node, i);
			the_light.from_json(the_light_node);

			m_lights.push_back(the_light);
		}
	}

	return 0;
}

/*!  ----------------------------------------------------------------------------
 *  @fn to_json
 *
 *  @brief Export LightDevice Object as JSON Object.
 */
struct json_object *LightDevice::to_json(void)
{
	struct json_object *the_root_node, *the_data_node;
	std::vector<Light>::iterator the_light_it;

	the_root_node = json_object_new_object();
	the_data_node = json_object_new_array();

	// speach_name
	json_object_object_add(the_root_node, k_entry_speach_name, json_object_new_string(m_speach_name.c_str()));
	// data
	json_object_object_add(the_root_node, k_entry_data, the_data_node);

	for (the_light_it = m_lights.begin(); the_light_it != m_lights.end(); the_light_it++)
	{
		json_object_array_add(the_data_node, (*the_light_it).to_json());
	}

	return the_root_node;
}

/*!  ----------------------------------------------------------------------------
 *  @fn set
 *
 *  @brief set the new light state.
 */
int LightDevice::set(struct json_object *a_node)
{
	int the_id;
	bool the_state;
	struct json_object *the_value_node;

	// id
	if (json_object_object_get_ex(a_node, k_entry_id, &the_value_node))
	{
		the_id = json_object_get_int(the_value_node);
	}

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

	return set(the_id, the_state);
}

/*!  ----------------------------------------------------------------------------
 *  @fn set
 *
 *  @brief set the new light state.
 */
int LightDevice::set(int an_id, bool a_state)
{
	std::vector<Light>::iterator the_light_it;

	for (the_light_it = m_lights.begin(); the_light_it != m_lights.end(); the_light_it++)
	{
		if ((*the_light_it).get_id() == an_id)
		{
			return (*the_light_it).update(a_state);
		}
	}
	return -1;
}
