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

#ifndef _UHTTP_SERVER_H
#define _UHTTP_SERVER_H

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

#include <list>
#include <unordered_map>
#include <string>

/*--------------------------------- Define ----------------------------------*/

/*----------------------------- Dependencies --------------------------------*/

struct ubus_context;
struct uhttpd_ops;
struct json_object;

class WebController;
class WebConnection;

/*--------------------------------- CLASS ----------------------------------*/

class UhttpServer
{
  public:
	typedef std::unordered_map<std::string, WebController *> ControllerContainer;
	typedef ControllerContainer::iterator ControllerIterator;
	typedef std::unordered_map<int, WebConnection *> ConnectionContainer;
	typedef ConnectionContainer::iterator ConnectionIterator;

  public:
	UhttpServer(void);
	~UhttpServer(void);

	static UhttpServer &get_instance(void);

	int setup(struct ubus_context *a_ctx, const struct uhttpd_ops *an_ops, const char *a_config_dir_path);

	struct ubus_context *get_context(void);

	bool check_url(const std::string &an_url);
	void handle_request(struct client *a_cl, const std::string &an_url, struct path_info *a_pi);
	void invoke_request(struct client *a_client);
	void request_free(struct client *a_client);

	int data_send(struct client *a_client, const char *a_data, int a_len);

  private:
	int load_config_dir(const char *a_config_dir_path);
	int add_controller_from_json(struct json_object *a_root_node);
	bool get_controller_fields(struct json_object *a_node, std::string &a_path, std::string &a_model_name,
							   std::string &an_event_name, std::string &an_etag_key,
							   std::string &a_get_method, std::string &a_set_method, bool &a_raw_response);
	struct json_object *load(const std::string &a_file);
	int add_controller(const std::string &an_uri, WebController *a_controller);
	void send_error(struct client *a_cl, int a_code, const char *a_summary, const std::string &a_msg);

  private:
	struct ubus_context *m_ctx;
	struct uhttpd_ops *m_ops;
	std::vector< WebController *> m_controllers;
	ConnectionContainer m_connections;
	std::list<std::string> m_path_list;
};

#endif /* _UHTTP_SERVER_H */
