#include "selfcare_config.h"
#include "common.h"
#include "log.h"


#define SELFCARE_PROXY_DEFAULT_CONF_FILE "./conf/selfcare_proxy.conf"

#define MAX_ERRCODE (5000)
#define MAX_MESSAGE_LEN (100)
static char g_errcode_map_message[MAX_ERRCODE][MAX_MESSAGE_LEN];


static selfcare_config_s g_selfcare_conf;

void _selfcare_config_str(const char* buffer, const char* key, char* val)
{
	char *pos = NULL;
	char *pos_val_start	= NULL;
	/* first address is start of the line */
	pos = strstr(buffer, key);
	if(NULL != pos && buffer == pos)
	{
		pos_val_start = strstr(pos, "=");

		/* remove \r or \n which first appear */
		pos = pos_val_start;
		while(pos)
		{
			if(*pos == '\r' || *pos == '\n' || *pos == '\0')
				break;
			pos++;
		}
		if(pos)
			*pos = '\0';
		
		if(NULL != pos_val_start)
		{
			strcpy(val, pos_val_start + 1);
		}
	}
}

void _selfcare_config_i16(const char* buffer, const char* key, short unsigned int* val)
{
	char *pos = NULL;
	char *pos_val_start	= NULL;
	/* first address is start of the line */
	pos = strstr(buffer, key);
	if(NULL != pos && buffer == pos)
	{
		pos_val_start = strstr(pos, "=");

		/* remove \r or \n which first appear */
		pos = pos_val_start;
		while(pos)
		{
			if(*pos == '\r' || *pos == '\n' || *pos == '\0')
				break;
			pos++;
		}
		if(pos)
			*pos = '\0';
		
		if(NULL != pos_val_start)
		{
			*val = atoi( pos_val_start + 1);
		}
	}
}

void _selfcare_config_i32(const char* buffer, const char* key, unsigned int* val)
{
	char *pos = NULL;
	char *pos_val_start	= NULL;
	/* first address is start of the line */
	pos = strstr(buffer, key);
	if(NULL != pos && buffer == pos)
	{
		pos_val_start = strstr(pos, "=");

		/* remove \r or \n which first appear */
		pos = pos_val_start;
		while(pos)
		{
			if(*pos == '\r' || *pos == '\n' || *pos == '\0')
				break;
			pos++;
		}
		if(pos)
			*pos = '\0';
		
		if(NULL != pos_val_start)
		{
			*val = atoi( pos_val_start + 1);
		}
	}
}

static void _selfcare_config_ipport(const char* buffer,
								   const char* key,
								   unsigned int* ip,
								   char* ip_str,
								   unsigned short* port)
{
	char buffer_ip[16] = {0};
	// char buffer_port[4] = {0};
	char *pos = NULL;
	char *pos_ip_start	= NULL;
	char *pos_ip_end	= NULL;
	/* first address is start of the line */
	pos = strstr(buffer, key);
	if(NULL != pos && buffer == pos)
	{
		pos_ip_start = strstr(pos, "=");
		if(NULL != pos_ip_start)
		{
			pos_ip_end = strstr(pos_ip_start + 1, ":");
			if(NULL != pos_ip_end)
			{
				memcpy(buffer_ip, pos_ip_start + 1, (pos_ip_end - 1) - (pos_ip_start + 1) + 1);
				if(ip_str)
				{
					strcpy(ip_str, buffer_ip);
				}
				*ip = inet_addr(buffer_ip);
				*port = atoi(pos_ip_end + 1);
			}
		}
	}
}
void _selfcare_config_errcodemapmsg(const char* buffer)
{
	char buf_map[MAX_MESSAGE_LEN] = {0};

	_selfcare_config_str(buffer, "errcode_map_message", buf_map);
	if(buf_map[0] != '\0')
	{
		char *p_1 = strstr(buf_map, "[");
		char *p_2 = strstr(buf_map, ",");
		char *p_3 = strstr(buf_map, "]");
		if(p_1 && p_2 && p_3)
		{
			char errcode_str[MAX_MESSAGE_LEN] = {0};
			memcpy(errcode_str, p_1 + 1, p_2 - (p_1 + 1) );

			char errmsg_str[MAX_MESSAGE_LEN] = {0};
			memcpy(errmsg_str, p_2 + 1, p_3 - (p_2 + 1) );
			
			int err_code = atoi(errcode_str);
			strcpy(g_errcode_map_message[err_code%MAX_ERRCODE], errmsg_str);

			LOG_D("[rest_proxy] load errorcodemap. [%d,%s]\n", err_code, errmsg_str);
		}
	}
}

char *rest_proxy_errcodemsg(const unsigned int errcode)
{
	if(g_errcode_map_message[errcode%MAX_ERRCODE][0] == '\0')
	{
		return "unkow";
	}

	return g_errcode_map_message[errcode%MAX_ERRCODE];
}

int selfcare_config_init(const char *cnf_file)
{
#define BUFFER_LEN 1024
	FILE*  fd;
	char buffer[BUFFER_LEN] = {0};

	if(cnf_file == NULL)
	{
		g_selfcare_conf.cnf_file = SELFCARE_PROXY_DEFAULT_CONF_FILE;
	}

	/* Create input file descriptor */
	fd = fopen (g_selfcare_conf.cnf_file, "r");
	if (fd == NULL)
	{
		perror ("open");
		return 2;
	}

	/* Copy process */
	while((fgets(buffer, BUFFER_LEN, fd)) != NULL)
	{
		if(buffer[0] == '#')
			continue;
		_selfcare_config_ipport(buffer, "udp_localIPPort",	&g_selfcare_conf.udp_local_ip, NULL, &g_selfcare_conf.udp_local_port);
		_selfcare_config_ipport(buffer, "udp_ocsIPPort",	&g_selfcare_conf.udp_ocs_ip, NULL, &g_selfcare_conf.udp_ocs_port);
		_selfcare_config_i16(buffer, "http_localPort",	&g_selfcare_conf.http_local_port);
		_selfcare_config_i32(buffer, "sms_notify_usage_75",	&g_selfcare_conf.sms_notify_type);
		_selfcare_config_str(buffer, "authcode_url", 		g_selfcare_conf.authcode_url);
		_selfcare_config_str(buffer, "query_userdata_url", 	g_selfcare_conf.query_userdata_url);
		_selfcare_config_str(buffer, "bundle_subs_url", 	g_selfcare_conf.bundle_subs_url);
		_selfcare_config_str(buffer, "bundle_usage_url", 	g_selfcare_conf.bundle_usage_url);
		_selfcare_config_str(buffer, "recharge_url", 		g_selfcare_conf.recharge_url);
		_selfcare_config_str(buffer, "transfer_url", 		g_selfcare_conf.transfer_url);
		_selfcare_config_str(buffer, "recharge_card_url", 	g_selfcare_conf.recharge_card_url);
		_selfcare_config_str(buffer, "check_balance_url", 	g_selfcare_conf.check_balance_url);
		_selfcare_config_str(buffer, "query_balane_url", 	g_selfcare_conf.query_balane_url);
		_selfcare_config_str(buffer, "payment_url", 		g_selfcare_conf.payment_url);
		_selfcare_config_str(buffer, "sms_deliver_url", 		g_selfcare_conf.sms_deliver_url);
		_selfcare_config_str(buffer, "create_account_url", 		g_selfcare_conf.create_account_url);
		_selfcare_config_str(buffer, "update_subs_url", 		g_selfcare_conf.update_subs_url);
		_selfcare_config_str(buffer, "delete_subs_url", 		g_selfcare_conf.delete_subs_url);
		

		_selfcare_config_errcodemapmsg(buffer);
		
		memset(buffer, 0x00, BUFFER_LEN);
	}

	fclose (fd);
	return 0;
}

int selfcare_config_uninit(void)
{
	return 0;
}

selfcare_config_s* selfcare_config_get(void)
{
	return &g_selfcare_conf;
}

