/*
 * Copyright (C) 2019 iopsys Software Solutions AB
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License version 2.1
 * as published by the Free Software Foundation
 *
 *	  Author MOHAMED Kallel <mohamed.kallel@pivasoftware.com>
 *	  Author Imen Bhiri <imen.bhiri@pivasoftware.com>
 *	  Author Feten Besbes <feten.besbes@pivasoftware.com>
 *	  Author Omar Kallel <omar.kallel@pivasoftware.c>
 *	  Author Amin Ben Ramdhane <amin.benramdhane@pivasoftware.com>
 *
 */

#ifndef __DMBBF_H__
#define __DMBBF_H__

#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#include <stdlib.h>
#include <syslog.h>
#include <libubox/list.h>
#include <json-c/json.h>
#include "dmuci.h"
#include "dmmem.h"
#include "dmapi.h"

int get_number_of_entries(struct dmctx *ctx, void *data, char *instance, int (*browseinstobj)(struct dmctx *ctx, struct dmnode *node, void *data, char *instance)); // To be removed later!!!!!!!!!!!!

char *handle_instance(struct dmctx *dmctx, DMNODE *parent_node, struct uci_section *s, const char *inst_opt, const char *alias_opt);
char *handle_instance_without_section(struct dmctx *dmctx, DMNODE *parent_node, int inst_nbr);

struct uci_section *create_dmmap_obj(struct dmctx *dmctx, unsigned char instance_level,
		const char *obj_file, const char *obj_name, struct uci_section *config_sec,
		char **instance);
int generic_browse(struct dmctx *dmctx, DMNODE *parent_node, void *prev_data, char *prev_instance);
char *uci_handle_instance(struct dmctx *dmctx, DMNODE *parent_node, struct dm_data *data);

int get_empty(char *refparam, struct dmctx *ctx, void *data, char *instance, char **value);

void fill_blob_param(struct blob_buf *bb, const char *path, const char *data, const char *type, uint32_t dm_flags);
void fill_blob_event(struct blob_buf *bb, const char *path, const char *type, void *data);
void fill_blob_operate(struct blob_buf *bb, const char *path, const char *data, const char *type, void *in_out);

int string_to_bool(const char *v, bool *b);
int dm_entry_get_value(struct dmctx *dmctx);
int dm_entry_get_name(struct dmctx *ctx);
int dm_entry_get_supported_dm(struct dmctx *ctx);
int dm_entry_get_instances(struct dmctx *ctx);
int dm_entry_add_object(struct dmctx *dmctx);
int dm_entry_delete_object(struct dmctx *dmctx);
int dm_entry_set_value(struct dmctx *dmctx);
int dm_entry_object_exists(struct dmctx *dmctx);
int dm_entry_operate(struct dmctx *dmctx);
int dm_entry_event(struct dmctx *dmctx);
int dm_entry_references_db(struct dmctx *ctx);
int dm_entry_get_reference_param(struct dmctx *dmctx);
int dm_entry_get_reference_value(struct dmctx *dmctx);
int dm_link_inst_obj(struct dmctx *dmctx, DMNODE *parent_node, void *data, char *instance);

static inline int DM_LINK_INST_OBJ(struct dmctx *dmctx, DMNODE *parent_node, void *data, char *instance)
{
	dmctx->faultcode = dm_link_inst_obj(dmctx, parent_node, data, instance);
	if (dmctx->stop || parent_node->num_of_entries >= BBF_MAX_OBJECT_INSTANCES) {
		if (parent_node->num_of_entries >= BBF_MAX_OBJECT_INSTANCES) {
			BBFDM_ERR("%s has reached max %d number of entries", parent_node->current_object, BBF_MAX_OBJECT_INSTANCES);
		}
		return DM_STOP;
	}
	return DM_OK;
}

#ifndef TRACE
#define TRACE(MESSAGE, ...) do { \
	syslog(LOG_INFO, "[%s:%d] " MESSAGE, __FUNCTION__, __LINE__, ##__VA_ARGS__); /* Flawfinder: ignore */ \
} while(0)
#endif


#ifndef TRACE_FILE
#define TRACE_FILE(MESSAGE, ...) do { \
    FILE *log_file = fopen("/tmp/bbfdm.log", "a"); \
    if (log_file) { \
        fprintf(log_file, "[%s:%d] " MESSAGE "\n", __FUNCTION__, __LINE__, ##__VA_ARGS__); \
        fclose(log_file); \
    } \
} while(0)
#endif

// Macros for different log levels
#define BBF_ERR(MESSAGE, ...) \
	BBFDM_ERR(MESSAGE, ##__VA_ARGS__)

#define BBF_WARNING(MESSAGE, ...) \
	BBFDM_WARNING(MESSAGE, ##__VA_ARGS__)

#define BBF_INFO(MESSAGE, ...) \
	BBFDM_INFO(MESSAGE, ##__VA_ARGS__)

#define BBF_DEBUG(MESSAGE, ...) \
	BBFDM_DEBUG(MESSAGE, ##__VA_ARGS__)

#endif //__DMBBF_H__
