/*!
 * capabilities_sprinklers.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 <ubuscpp/UBusCall.h>

#include <jsoncpp/json.h>

#include "devices/Devices.h"

#include "capabilities_sprinklers.h"

namespace { 

using namespace UBus;

static ObjectType gCapabilitiesSprinklersModelUbus_types(
	"sprinklers",
	Method("get", UBUS_CPP(CapabilitiesSprinklersModel, Get)),
	Method("delete", UBUS_CPP(CapabilitiesSprinklersModel, Delete)),
	Method("put", UBUS_CPP(CapabilitiesSprinklersModel, Put)),
	Method("post", UBUS_CPP(CapabilitiesSprinklersModel, Post))
);

}

/*!  ----------------------------------------------------------------------------
 *  @fn CapabilitiesSprinkler
 *
 *  @brief Constructor of the UBus Mixer Volume.
 */
CapabilitiesSprinklersModel::CapabilitiesSprinklersModel (Devices *aDevice) : 
	UBusObject (gCapabilitiesSprinklersModelUbus_types, "domo.capabilities.sprinklers"),
	mDevices (aDevice)
{
}


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


/*!  ----------------------------------------------------------------------------
 *  @fn Get
 *
 *  @brief Get the List of the Capabilities.
 */
int CapabilitiesSprinklersModel::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("Sprinklers").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 CapabilitiesSprinklersModel::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 CapabilitiesSprinklersModel::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 CapabilitiesSprinklersModel::Post (struct ubus_context *aCtx, struct ubus_request_data *aReq, 
							 struct blob_attr *aMsg)
{
	int theResult;
	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;
	int theID;
	bool theState;
 	fprintf (stderr,"CapabilitiesSprinklersModel::Post \n");
 	
	if (!theReader.parse (theString, theRoot)) {

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

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

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

  	theOutput["Sprinklers"] = 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;
}
