/*!
 * 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: 19/06/2017
 */

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

#include <json-c/json.h>

#include "rest/rest-connection.h"

#include "rest/rest-controller.h"

#include <uhttpd/uhttpd.h>


#define kControllerModelKey	"model"
#define kControllerEndpointKey "endpoint"
#define kControllerUbusKey "ubus"

#define kControllerPathKey "path"
#define kControllerMethodKey "method"

/*!  ----------------------------------------------------------------------------
 *  @fn RestController
 *
 *  @brief constructor of the rest controller object.
 */
RestController::RestController(void)
{
}

/*!  ----------------------------------------------------------------------------
 *  @fn RestController
 *
 *  @brief constructor of the rest controller object.
 */
RestController::RestController(const std::string &a_path, const std::string &a_method_get, const std::string &a_method_put, int a_timeout, bool a_raw_response) : WebController(a_path)
#if 0
																																								  m_method_get(a_method_get),
																																								  m_method_put(a_method_put),
																																								  m_timeout(a_timeout),
																																								  mf_raw_response(a_raw_response)
#endif
{
}

/*!  ----------------------------------------------------------------------------
 *  @fn ~RestController
 *
 *  @brief destructor of the rest controller object.
 */
RestController::~RestController(void)
{
}

/*!  ----------------------------------------------------------------------------
 *  @fn new_connection
 *
 *  @brief return a new rest connection attached to this controller.
 */
WebConnection *RestController::new_connection(struct uhttpd_ops *an_ops, struct client *a_client, const std::string &a_parameters)
{
	return new RestConnection(an_ops, a_client, this, a_parameters);
}


/*!  ----------------------------------------------------------------------------
 *  @fn from_json
 *
 *  @brief import a rest controller from a json node..
 */
bool RestController::from_json(struct json_object *a_node)
{
	struct json_object *the_value_node, *the_ubus_node;

	// model
	if (json_object_object_get_ex(a_node, kControllerModelKey, &the_value_node))
	{
		std::string the_model;
		the_model = json_object_get_string(the_value_node);
		if (the_model == "get")
			m_method = UH_HTTP_MSG_GET;
		else if (the_model == "put")
			m_method = UH_HTTP_MSG_PUT;
		else if (the_model == "post")
			m_method = UH_HTTP_MSG_POST;
		else
			m_method = UH_HTTP_MSG_DELETE;
	}

	// endpoint
	if (json_object_object_get_ex(a_node, kControllerEndpointKey, &the_value_node))
	{
		int the_len, i;
		struct json_object *the_ep_node;
		the_len = json_object_array_length(the_value_node);

		for (i = 0; i < the_len; i++)
		{
			std::string the_endpoint;
			the_ep_node = json_object_array_get_idx(the_value_node, i);
			the_endpoint = json_object_get_string(the_ep_node);
			m_endpoint.push_back(the_endpoint);
		}
	}

	// ubus
	if (json_object_object_get_ex(a_node, kControllerUbusKey, &the_ubus_node))
	{
		// path
		if (json_object_object_get_ex(the_ubus_node, kControllerPathKey, &the_value_node))
		{
			m_ubus.m_path = json_object_get_string(the_value_node);
		}
		//method
		if (json_object_object_get_ex(the_ubus_node, kControllerMethodKey, &the_value_node))
		{
			m_ubus.m_method = json_object_get_string(the_value_node);
		}

	}

	return true;
}



/*!  ----------------------------------------------------------------------------
 *  @fn find
 *
 *  @brief return true if the controller match with the url.
 */
bool RestController::find(const std::string &an_url)
{
	return true;
}


/*!  ----------------------------------------------------------------------------
 *  @fn get_method_get
 *
 *  @brief return the get method of the controller.
 */
std::string RestController::get_method_get(void)
{
	return ""; //m_method_get;
}

/*!  ----------------------------------------------------------------------------
 *  @fn get_method_put
 *
 *  @brief return the put method of the controller.
 */
std::string RestController::get_method_put(void)
{
	return ""; //m_method_put;
}

/*!  ----------------------------------------------------------------------------
 *  @fn get_timeout
 *
 *  @brief return the wanted timeout of the controller.
 */
uint16_t RestController::get_timeout(void)
{
	return 0; //m_timeout;
}

/*!  ----------------------------------------------------------------------------
 *  @fn is_raw_response
 *
 *  @brief return the true if the controller didn't need the awox api response template.
 */
bool RestController::is_raw_response(void)
{
	return false; //mf_raw_response;
}

/*!  ----------------------------------------------------------------------------
 *  @fn get_method
 *
 *  @brief return the right method in function of the client connection.
 */
std::string RestController::get_method(uint8_t a_method)
{
#if 0
	switch (a_method)
	{
	case UH_HTTP_MSG_GET:
		return m_method_get;
		break;
	case UH_HTTP_MSG_PUT:
	case UH_HTTP_MSG_POST:
		return m_method_put;
		break;
	default:
		// TODO ERROR
		break;
	};
#endif
	return "";
}
