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

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

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

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

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

	// Get Shutter
	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_shutter_node;

		the_len = json_object_array_length(the_data_node);
		for (int i = 0; i < the_len; i++)
		{
			Shutter the_shutter;
			the_shutter_node = json_object_array_get_idx(the_data_node, i);
			the_shutter.from_json(the_shutter_node);

			m_shutters.push_back(the_shutter);
		}
	}

	return 0;
}

/*!  ----------------------------------------------------------------------------
 *  @fn to_json
 *
 *  @brief Export ShutterDevice Object as JSON Object.
 */
struct json_object *ShutterDevice::to_json(void)
{
	struct json_object *the_root_node, *the_data_node;
	std::vector<Shutter>::iterator the_shutter_it;
	fprintf(stderr, "ShutterDevice::to_json\n");

	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_shutter_it = m_shutters.begin(); the_shutter_it != m_shutters.end(); the_shutter_it++)
	{
		json_object_array_add(the_data_node, (*the_shutter_it).to_json());
	}

	return the_root_node;
}

/*!  ----------------------------------------------------------------------------
 *  @fn set
 *
 *  @brief set the new Shutter state.
 */
int ShutterDevice::set(struct json_object *a_node)
{
	return 0;
}

/*!  ----------------------------------------------------------------------------
 *  @fn set
 *
 *  @brief set the new Shutter state.
 */
int ShutterDevice::set(int an_id, bool a_state)
{
	return 0;
}
