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

#include <json-c/json.h>

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

#include "devices/devices-manager.h"

#include "domo.h"

#include "capabilities_lights_model.h"

namespace
{

using namespace UBus;

static ObjectType gCapabilitiesLightsUbus_types(
	"lights",
	Method("get", UBUS_CPP(CapabilitiesLightsModel, get)),
	Method("delete", UBUS_CPP(CapabilitiesLightsModel, del)),
	Method("put", UBUS_CPP(CapabilitiesLightsModel, put)),
	Method("post", UBUS_CPP(CapabilitiesLightsModel, post)));
}

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

/*!  ----------------------------------------------------------------------------
 *  @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 *a_ctx, struct ubus_request_data *a_req, struct blob_attr *a_msg)
{
	int the_result;
	struct blob_buf the_buf = {0};

	blob_buf_init(&the_buf, 0);

	blobmsg_add_json_from_string(&the_buf, m_devices_manager->get(kLightEntry).c_str());

	the_result = ubus_send_reply(a_ctx, a_req, the_buf.head);

	blob_buf_free(&the_buf);

	return the_result;
}

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

	return the_result;
}

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

	return the_result;
}

/*!  ----------------------------------------------------------------------------
 *  @fn post
 *
 *  @brief Get the List of the Capabilities.
 */
int CapabilitiesLightsModel::post(struct ubus_context *a_ctx, struct ubus_request_data *a_req, struct blob_attr *a_msg)
{
	int the_result = 0;
	struct blob_buf the_buf = {0};
	char *the_string;
	struct json_object *the_root_node, *the_output_node;
	fprintf(stderr, "CapabilitiesLights::Post \n");

	the_string = blobmsg_format_json(a_msg, true);

	the_root_node = json_tokener_parse(the_string);
	free(the_string);

	if (the_root_node == NULL)
	{
		fprintf(stderr, "Failed parse the parameters.\n");
		return UBUS_STATUS_INVALID_ARGUMENT;
	}

	if (m_devices_manager->set(kLightEntry, the_root_node) < 0)
	{
		fprintf(stderr, "Failed to set the new state.\n");
		return UBUS_STATUS_INVALID_ARGUMENT;
	}

	// Create the output node.
	the_output_node = json_object_new_object();
	json_object_object_add(the_output_node, kLightEntry, the_root_node);

	blob_buf_init(&the_buf, 0);

	blobmsg_add_json_from_string(&the_buf, json_object_to_json_string(the_output_node));

	the_result = ubus_send_reply(a_ctx, a_req, the_buf.head);

	blob_buf_free(&the_buf);

	json_object_put(the_root_node);
	json_object_put(the_output_node);

	return the_result;
}
