/*!
 * Light.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: 21/05/2016
 *
 */

/*------------------------------- INCLUDES ----------------------------------*/

#include <cstdio>

#include <ubuscpp/UBusCall.h>

#include "devices/Light.h"


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


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


/*!  ----------------------------------------------------------------------------
 *  @fn load_from_json
 *
 *  @brief Load a Light Element from a Json Entry.
 */
int Light::load_from_json (Json::Value anElem)
{
	mID = anElem["id"].asInt();
	mName = anElem["name"].asString();
	mSpeach_name = anElem["speach_name"].asString();
	mZone = anElem["zone"].asString();
	mState = anElem["state"].asBool();
	mSender = anElem["sender"].asInt();
	mInterruptor = anElem["interruptor"].asInt();

	return 0;
}


/*!  ----------------------------------------------------------------------------
 *  @fn to_json
 *
 *  @brief Return Light Object as a Json Object.
 */
Json::Value Light::to_json (void) const
{
	Json::Value aResult(Json::objectValue);
	fprintf (stderr, "Light::to_json\n");
	aResult["id"] = mID;
	aResult["name"] = mName;
	aResult["speach_name"] = mSpeach_name;
	aResult["zone"] = mZone;
	aResult["state"] = mState;
	aResult["sender"] = mSender;
	aResult["interruptor"] = mInterruptor;

	return aResult;
}


/*!  ----------------------------------------------------------------------------
 *  @fn update
 *
 *  @brief Update the Light State.
 */
int Light::update (bool aState)
{
	if (mState == aState)
		return 1;

	mState = aState;

	return sendState ();
}


/*!  ----------------------------------------------------------------------------
 *  @fn getID
 *
 *  @brief Return the ID of the Light.
 */
uint16_t Light::getID (void)
{
	return mID;
}


/*!  ----------------------------------------------------------------------------
 *  @fn sendState
 *
 *  @brief Send the Curent State of the Light.
 */
int Light::sendState (void)
{
	UBusCall theCmd;
	Json::Value theParam;
	Json::StyledWriter theWriter;
	std::string theResult;
	
	theParam["sender"] = mSender;
	theParam["interruptor"] = mInterruptor;
	theParam["state"] = mState;

	return theCmd.Exec ("chacon", "set", theWriter.write(theParam).c_str(),theResult);
}
