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


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


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


/*!  ----------------------------------------------------------------------------
 *  @fn load_from_json
 *
 *  @brief Load SprinklerDevice Object From a Json Object.
 */
int SprinklerDevice::load_from_json (Json::Value anElem)
{
	fprintf (stderr, "SprinklerDevice::load_from_json\n");

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

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

			mSprinkler.push_back (theSprinkler);
		}
	}

	return 0;
}


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

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

	for (theSprinkler_it = mSprinkler.begin(); theSprinkler_it != mSprinkler.end(); theSprinkler_it++) {
		
		data_json.append ((*theSprinkler_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 Sprinkler state.
 */
int SprinklerDevice::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 Sprinkler state.
 */
int SprinklerDevice::set (int anID, bool aState)
{
	std::vector<Sprinkler>::iterator theSprinkler_it;
	
	for (theSprinkler_it = mSprinkler.begin(); theSprinkler_it != mSprinkler.end(); theSprinkler_it++) {
		
		if ((*theSprinkler_it).getID() == anID) {
			
			return (*theSprinkler_it).update (aState);
		}
	}
	
	return -1;
}
