#ifndef COMMON_ZTE_H
#define COMMON_ZTE_H

#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <signal.h>
#include <time.h>
#include <limits.h>
#include <libubox/list.h>
#include <libubox/uloop.h>
#include <ctype.h>
#include <json-c/json.h>
#include <string.h>
#include <libubox/blobmsg.h>
#include <libubox/blobmsg_json.h>
#include <libubus.h>
#include <libxml2/libxml/parser.h>
#include <libxml/xpath.h>
#include <libxml/xpathInternals.h>

#include "libmobile_wrapper.h"

extern int debug;
extern struct device *global_dev;

#define debug_print(...)                  \
	do {                                  \
		if (debug)                        \
			fprintf(stderr, __VA_ARGS__); \
	} while (0)

char *get_ip(char *if_name);
/**
 * Function: print_to_ubus
 *
 * Prints a json_object pointer's json structure to ubus.
 *
 * Parameters:
 * 		parsed_response - A struct json_object pointer to json structure to be printed to ubus.
 * 		ctx - Ubus context containing connection.
 * 		req - Information for from the ubus request.
 *
 * Returns:
 * 		0 On success.
 * 		-1 On failure.
 */
int print_to_ubus(struct json_object *parsed_response, struct ubus_context *ctx, struct ubus_request_data *req);

/**
 * Function: json_to_blob
 *
 * Parses a json_object pointer to a corresponding blob buffer.
 *
 * Parameters:
 * 		response - json_object pointer to be replicated in a blob buffer.
 * 		bb - The blob buffer pointer to hold the results.
 *
 */
void json_to_blob(struct json_object *response, struct blob_buf *bb);

char *xml_parser(struct write_result *result, char *tag);
struct json_object *xml_to_json_converter(struct write_result *result);

/**
 * Function: isdigits
 *
 * Checks if a string is made up entirely out of digits.
 *
 * Parameters:
 * 		str - the string to check for digits.
 *
 * Returns:
 * 		true if the string is made up entirely out of digits.
 * 		false if the string contains any non-digit character.
 */
int isdigits(const char *str);

/**
 * Function: validate_puk_format
 *
 * Checks whether a given PUK code is of valid format or not.
 *
 * Parameters:
 * 		puk - the string to check for structure validity
 *
 * Returns:
 * 		0 if the string is of valid format.
 * 		-1 on invalid format.
 */
int validate_puk_format(char *puk);

/**
 * Function: validate_pin_format
 *
 * Checks whether a given PIN code is of valid format or not.
 *
 * Parameters:
 * 		pin - the string to check for structure validity
 *
 * Returns:
 * 		0 if the string is of valid format.
 * 		-1 on invalid format.
 */
int validate_pin_format(char *pin);

/**
 * Function: pin_status
 *
 * Provides an error response to be used to print a message if the call fails
 * due to pin being disabled. Up to the caller whether the print this message or
 * not (depending on the necessity of pin to be disabled or enabled for the feature).
 *
 * Parameters:
 * 		bb - pointer to the buff.
 *		dev - the device containing the currently active device.
 *
 * Returns:
 * 		0 if the string is of valid format.
 * 		-1 on invalid format.
 */
int pin_status(struct blob_buf *bb, struct device *dev);

/**
 * Function: check_response
 *
 * Determines whether the dongle API response was successful or not. Requires the
 * response to be in the format of {"result": "success"} or {"result": "failure"}.
 *
 * Parameters:
 * 		response - the json response to parse for success or failure.
 *
 * Returns:
 * 		0 if the response contained success.
 * 		-1 if the response contained failure.
 */
int check_response(struct json_object *response);
struct json_object *get_json_string_object_by_key(json_object *json_obj, char *string);

/**
 * Function: remove_newline
 *
 * Replaces the first newline character ('\n') found by a null-terminating byte ('\0').
 *
 * Parameters:
 * 		response - the json response to parse for success or failure.
 *
 * Returns:
 * 		0 if the response contained success.
 * 		-1 if the response contained failure.
 *
 * IMPORTANT NOTE
 *		Will alter the input string!
 */
void remove_newline(char *input);
#endif