/*!
 * LightDevice.cpp
 *
 * Copyright (c) 2016, 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 "devices/Light.h"

#include "devices/LightDevice.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 load_from_json
 *
 *  @brief Load LightDevice Object From a Json Object.
 */
int LightDevice::load_from_json (Json::Value anElem)
{
	fprintf (stderr, "LightDevice::load_from_json\n");

	mspeach_name = anElem["speach_name"].asString();

	for (Json::Value& theElement : anElem["data"]) {
		Light theLight;
		if (theLight.load_from_json (theElement) == 0) {

			mLights.push_back (theLight);
		}
	}

	return 0;
}


/*!  ----------------------------------------------------------------------------
 *  @fn to_json
 *
 *  @brief Export LightDevice Object as JSON Object.
 */
Json::Value LightDevice::to_json (bool bDataOnly)
{
	Json::Value theResult;
	Json::Value data_json(Json::arrayValue);
	std::vector<Light>::iterator theLight_it;

	fprintf (stderr, "LightDevice::to_json\n");

	for (theLight_it = mLights.begin(); theLight_it != mLights.end(); theLight_it++) {
		
		data_json.append ((*theLight_it).to_json());
	}

	theResult["speach_name"] = mspeach_name;
	theResult["data"] = data_json;

	if (bDataOnly == true)
		return data_json;

	return theResult;
}


/*!  ----------------------------------------------------------------------------
 *  @fn set
 *
 *  @brief set the new light state.
 */
int LightDevice::set (Json::Value anElement)
{
	int theID;
	bool theState; 

	theID = anElement["id"].asInt();
	theState = anElement["state"].asBool();

	return set (theID, theState);
}


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

	for (theLight_it = mLights.begin(); theLight_it != mLights.end(); theLight_it++) {
		
		if ((*theLight_it).getID() == anID) {

			return (*theLight_it).update (aState);
		}
	}

	return -1;
}
