/*!
 * capabilities_lights.cpp
 *
 * Copyright (c) 2015, 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: 24/03/2015
 *
 */

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

extern "C" {
#include <libubox/blobmsg_json.h>
}



#include <jsoncpp/json.h>

#include "devices/Devices.h"

#include "capabilities_lights.h"

namespace { 

using namespace UBus;

static ObjectType gCapabilitiesLightsUbus_types(
	"lights",
	Method("get", UBUS_CPP(CapabilitiesLightsModel, Get)),
	Method("delete", UBUS_CPP(CapabilitiesLightsModel, Delete)),
	Method("put", UBUS_CPP(CapabilitiesLightsModel, Put)),
	Method("post", UBUS_CPP(CapabilitiesLightsModel, Post))
);

}

/*!  ----------------------------------------------------------------------------
 *  @fn CapabilitiesLightsModel
 *
 *  @brief Constructor of the UBus Mixer Volume.
 */
CapabilitiesLightsModel::CapabilitiesLightsModel (Devices *aDevice) : 
	UBusObject (gCapabilitiesLightsUbus_types, "domo.capabilities.lights"),
	mDevices (aDevice)
{
}


/*!  ----------------------------------------------------------------------------
 *  @fn ~CapabilitiesLightsModel
 *
 *  @brief Destructor of the UBus Mixer Volume.
 */
CapabilitiesLightsModel::~CapabilitiesLightsModel (void)
{
	
}


/*!  ----------------------------------------------------------------------------
 *  @fn Get
 *
 *  @brief Get the List of the Capabilities.
 */
int CapabilitiesLightsModel::Get (struct ubus_context *aCtx, struct ubus_request_data *aReq, 
							 struct blob_attr *aMsg)
{
	int theResult;
	struct blob_buf theBuf = {0};

	blob_buf_init (&theBuf, 0);

	blobmsg_add_json_from_string (&theBuf, mDevices->get("Lights").c_str());
	
	theResult = ubus_send_reply (aCtx, aReq, theBuf.head);
	
	blob_buf_free (&theBuf);
	
	return theResult;
}


/*!  ----------------------------------------------------------------------------
 *  @fn Delete
 *
 *  @brief Get the List of the Capabilities.
 */
int CapabilitiesLightsModel::Delete (struct ubus_context*, struct ubus_request_data*, struct blob_attr*)
{
	int theResult = 0;
	
	return theResult;
}


/*!  ----------------------------------------------------------------------------
 *  @fn Put
 *
 *  @brief Get the List of the Capabilities.
 */
int CapabilitiesLightsModel::Put (struct ubus_context*, struct ubus_request_data*, struct blob_attr*)
{
	int theResult = 0;
	
	return theResult;
}


/*!  ----------------------------------------------------------------------------
 *  @fn Post
 *
 *  @brief Get the List of the Capabilities.
 */
int CapabilitiesLightsModel::Post (struct ubus_context *aCtx, struct ubus_request_data *aReq, 
							 struct blob_attr *aMsg)
{
	int theResult = 0;
	struct blob_buf theBuf = {0};
	char *theString = blobmsg_format_json (aMsg, true);
	Json::Reader theReader;
	Json::StyledWriter theWriter;
	Json::Value theRoot;
	Json::Value theOutput;
	Json::Value theElement;
    fprintf (stderr,"CapabilitiesLights::Post \n");
    
	if (!theReader.parse (theString, theRoot)) {

    	fprintf (stderr, "Failed parse the parameters.\n");
    	return UBUS_STATUS_INVALID_ARGUMENT;
  	}

  	if (mDevices->set ("Lights", theRoot) < 0) {

  		fprintf (stderr, "Failed to set the new state.\n");
  		return -1;
  	}

	theOutput["Lights"] = theElement;

	blob_buf_init (&theBuf, 0);

	blobmsg_add_json_from_string (&theBuf, theWriter.write(theOutput).c_str());
	
	theResult = ubus_send_reply (aCtx, aReq, theBuf.head);
	
	blob_buf_free (&theBuf);
	
	return theResult;
}
