#include "selfcare_endecode.h"
#include "selfcare_httpsrv.h"
#include "json.h"
#include "restapi.h"
#include "log.h"

extern ulong TransStrTimeToSecond(const char *str_time);//YYYY-MM-DD HH:MM:YY
extern int dba_crm_get_product_rent(int from, int package_id);
char* _find_name(json_t *node, const char* name)
{
	if(node == NULL || node->type != JSON_STRING)
	{
		return NULL;
	}
	
	if(strcmp(node->text, name) == 0)
	{
		if(node->child)
		{
			LOG_D("[httpserv] name[%s] val[%s]\n", name, node->child->text);
			return node->child->text;
		}
	}
	
	return _find_name(node->next, name);
}

int _selfcare_set_str(json_t *root, char *buf, const char *name)
{
	int ret = 0;
	char *val = _find_name(root->child, name);
	
	if(val)
	{
		strcpy(buf, val);
		ret = 1;
	}

	return ret;
}
void _selfcare_set_u8(json_t *root, u8 *val, const char *name)
{
	char *tmp = _find_name(root->child, name);
	if(tmp)
	{
		*val = atoi(tmp);
	}
}

void _selfcare_set_u16(json_t *root, u16 *val, const char *name)
{
	char *tmp = _find_name(root->child, name);
	if(tmp)
	{
		*val = atoi(tmp);
	}
}

int _selfcare_set_u32(json_t *root, u32 *val, const char *name)
{
	char *tmp = _find_name(root->child, name);
	if(tmp)
	{
		*val = atol(tmp);
		return 1;
	}
	else
		return 0;
}


void _print_name(json_t *node, const char* name)
{
	char *val = _find_name(node->child, name);
	if(val != NULL)
	{
		LOG_D("[httpserv] name[%s] val[%s]\n", name, val);
	}
	else
	{
		LOG_E("[httpserv] name[%s] not found\n", name);
	}
}

static int _selfcare_decode_authcode(const char *buff, const int len, _rest_msg_s *msg)
{
	msg->msg_type = REST_SEND_AUTHCODE_REQ;

	json_t *root = NULL;
	if(json_parse_document(&root, buff) == JSON_OK)
 	{
		if(root != NULL && root->type == JSON_OBJECT)
		{
			LOG_D("\n");
			_selfcare_set_str(root, msg->msg.send_authcode.msisdn, "telNumber");
			_selfcare_set_str(root, msg->msg.send_authcode.sms_content, "content");
		}
 		LOG_D("[httpserv] decode autchcode suc\n");
 	}
	else
	{
		LOG_E("[httpserv] decode autchcode fail\n");
	}

	return 0;
}
static int _selfcare_decode_query_user_data(const char *buff, const int len, _rest_msg_s *msg)
{
	msg->msg_type = REST_QUERY_USERDATA_REQ;

	json_t *root = NULL;
	if(json_parse_document(&root, buff) == JSON_OK)
 	{
		if(root != NULL && root->type == JSON_OBJECT)
		{
			LOG_D("\n");
			_selfcare_set_str(root, msg->msg.query_user_data.msisdn, "telNumber");
		}
 		LOG_D("[httpserv] decode query_user_data suc\n");
 	}
	else
	{
		LOG_E("[httpserv] decode query_user_data fail\n");
	}

	return 0;
}
static int _selfcare_decode_bundle_subs(const char *buff, const int len, _rest_msg_s *msg)
{
	msg->msg_type = REST_BUNDLE_SUBS_REQ;

	json_t *root = NULL;
	if(json_parse_document(&root, buff) == JSON_OK)
 	{
		if(root != NULL && root->type == JSON_OBJECT)
		{
			LOG_D("\n");
			_selfcare_set_str(root, msg->msg.bundle_subs.msisdn, "telNumber");
			_selfcare_set_u8(root, &msg->msg.bundle_subs.payType, "payType");
			_selfcare_set_u32(root, &msg->msg.bundle_subs.charge, "chargedAmount");
			_selfcare_set_u32(root, &msg->msg.bundle_subs.moVoiceMinute, "moVoiceMinute");
			_selfcare_set_u32(root, &msg->msg.bundle_subs.mtVoiceMinute, "mtVoiceMinute");
			_selfcare_set_u32(root, &msg->msg.bundle_subs.dataVolume, "dataVolume");
			_selfcare_set_u32(root, &msg->msg.bundle_subs.smsNum, "smsVolume");
			_selfcare_set_u32(root, &msg->msg.bundle_subs.validDays, "validPeriod");
		}
 		LOG_D("[httpserv] decode bundle_subs suc\n");
 	}
	else
	{
		LOG_E("[httpserv] decode bundle_subs fail\n");
	}

	return 0;
}
static int _selfcare_decode_bundle_usage(const char *buff, const int len, _rest_msg_s *msg)
{
	msg->msg_type = REST_BUNDLE_USAGE_REQ;

	json_t *root = NULL;
	if(json_parse_document(&root, buff) == JSON_OK)
 	{
		if(root != NULL && root->type == JSON_OBJECT)
		{
			LOG_D("\n");
			_selfcare_set_str(root, msg->msg.bundle_usage.msisdn, "telNumber");
		}
 		LOG_D("[httpserv] decode bundle_usage suc\n");
 	}
	else
	{
		LOG_E("[httpserv] decode bundle_usage fail\n");
	}

	return 0;
}
static int _selfcare_decode_recharge(const char *buff, const int len, _rest_msg_s *msg)
{
	msg->msg_type = REST_RECHARGE_REQ;

	json_t *root = NULL;
	if(json_parse_document(&root, buff) == JSON_OK)
 	{
		if(root != NULL && root->type == JSON_OBJECT)
		{
			LOG_D("\n");
			_selfcare_set_str(root, msg->msg.recharge.msisdn, "telNumber");
			_selfcare_set_u32(root, &msg->msg.recharge.amount, "amount");
			_selfcare_set_u8(root, &msg->msg.recharge.op_type, "opType");
			_selfcare_set_u16(root, &msg->msg.recharge.valid_days, "validyDays");
			if(msg->msg.recharge.op_type == 1) //recharge with cash
			{
				//if(msg->msg.recharge.valid_days<=1)
				//	msg->msg.recharge.valid_days = 7;
			}

		}
 		LOG_D("[httpserv] decode recharge suc\n");
 	}
	else
	{
		LOG_E("[httpserv] decode recharge fail\n");
	}

	return 0;
}
static int _selfcare_decode_transfer(const char *buff, const int len, _rest_msg_s *msg)
{
	msg->msg_type = REST_TRANSFER_REQ;

	json_t *root = NULL;
	if(json_parse_document(&root, buff) == JSON_OK)
 	{
		if(root != NULL && root->type == JSON_OBJECT)
		{
			LOG_D("\n");
			_selfcare_set_str(root, msg->msg.transfer.transferOutMsisdn, "transferOut");
			_selfcare_set_str(root, msg->msg.transfer.transferInMsisdn, "transferIn");
			_selfcare_set_u32(root, &msg->msg.transfer.amount, "amount");
		}
 		LOG_D("[httpserv] decode transfer suc\n");
 	}
	else
	{
		LOG_E("[httpserv] decode transfer fail\n");
	}

	return 0;
}
static int _selfcare_decode_recharge_card(const char *buff, const int len, _rest_msg_s *msg)
{
	msg->msg_type = REST_RECHARGE_CARD_REQ;

	json_t *root = NULL;
	if(json_parse_document(&root, buff) == JSON_OK)
 	{
		if(root != NULL && root->type == JSON_OBJECT)
		{
			LOG_D("\n");
			_selfcare_set_str(root, msg->msg.recharge_card.msisdn, "telNumber");
			_selfcare_set_str(root, msg->msg.recharge_card.cardPwd, "cardPwd");
			//_selfcare_set_u32(root, &msg->msg.recharge_card.amount, "rechargeAmount");
		}
 		LOG_D("[httpserv] decode recharge_card suc\n");
 	}
	else
	{
		LOG_E("[httpserv] decode recharge_card fail\n");
	}

	return 0;
}
static int _selfcare_decode_check_balance(const char *buff, const int len, _rest_msg_s *msg)
{
	msg->msg_type = REST_CHECK_BALANCE_REQ;

	json_t *root = NULL;
	if(json_parse_document(&root, buff) == JSON_OK)
 	{
		if(root != NULL && root->type == JSON_OBJECT)
		{
			LOG_D("\n");
			_selfcare_set_str(root, msg->msg.check_balance.msisdn, "telNumber");
			_selfcare_set_u32(root, &msg->msg.check_balance.amount, "amount");
		}
 		LOG_D("[httpserv] decode check_balance suc\n");
 	}
	else
	{
		LOG_E("[httpserv] decode check_balance fail\n");
	}

	return 0;
}
static int _selfcare_decode_query_balance(const char *buff, const int len, _rest_msg_s *msg)
{
	msg->msg_type = REST_QUERY_BALANCE_REQ;

	json_t *root = NULL;
	if(json_parse_document(&root, buff) == JSON_OK)
 	{
		if(root != NULL && root->type == JSON_OBJECT)
		{
			LOG_D("\n");
			_selfcare_set_str(root, msg->msg.query_balance.msisdn, "telNumber");
		}
 		LOG_D("[httpserv] decode query_balance suc\n");
 	}
	else
	{
		LOG_E("[httpserv] decode query_balance fail\n");
	}

	return 0;
}

static int _selfcare_decode_crm_payment(const char *buff, const int len, _rest_msg_s *msg)
{
	msg->msg_type = REST_CRM_PAYMENT_REQ;

	json_t *root = NULL;
	if(json_parse_document(&root, buff) == JSON_OK)
 	{
		if(root != NULL && root->type == JSON_OBJECT)
		{
			LOG_D("\n");
			_selfcare_set_str(root, msg->msg.recharge.msisdn, "telNumber");
			_selfcare_set_u32(root, &msg->msg.recharge.amount, "amount");
		}
 		LOG_D("[httpserv] decode crm payment suc\n");
 	}
	else
	{
		LOG_E("[httpserv] decode crm payment fail\n");
	}
	return 0;
}

static int _selfcare_decode_sms_deliver(const char *buff, const int len, _rest_msg_s *msg)
{
	msg->msg_type = REST_CRM_SMS_DELIVER_REQ;

	json_t *root = NULL;
	if(json_parse_document(&root, buff) == JSON_OK)
 	{
		if(root != NULL && root->type == JSON_OBJECT)
		{
			LOG_D("\n");
			_selfcare_set_str(root, msg->msg.sms_deliver.msisdn, "telNumber");
			_selfcare_set_str(root, (char *)&msg->msg.sms_deliver.sms_content, "content");
		}
 		LOG_D("[httpserv] decode sms_dliver suc\n");
 	}
	else
	{
		LOG_E("[httpserv] decode sms_dliver fail\n");
	}
	return 0;
}

static int _selfcare_decode_create_account(const char *buff, const int len, _rest_msg_s *msg)
{
	char buf[128];
	msg->msg_type = REST_CRM_CREATE_ACCT_REQ;

	json_t *root = NULL;
	if(json_parse_document(&root, buff) == JSON_OK)
 	{
		if(root != NULL && root->type == JSON_OBJECT)
		{
			LOG_D("\n");
			_selfcare_set_str(root, msg->msg.create_acct.msisdn, "serviceNbr");
			_selfcare_set_u32(root, &msg->msg.create_acct.customer_id, "custId");
			_selfcare_set_u32(root, &msg->msg.create_acct.account_id, "accountId");
			_selfcare_set_u32(root, &msg->msg.create_acct.product_id, "productInstId");
			_selfcare_set_u32(root, &msg->msg.create_acct.plan_id, "packageId");
			_selfcare_set_u32(root, &msg->msg.create_acct.balance, "balance");
			
			_selfcare_set_str(root, buf, "balanceExpDate");			
			msg->msg.create_acct.expiry_date = TransStrTimeToSecond(buf);

			_selfcare_set_str(root, buf, "birthday");
			msg->msg.create_acct.birthday = TransStrTimeToSecond(buf);

			msg->msg.create_acct.rent_charge = dba_crm_get_product_rent(1, msg->msg.create_acct.plan_id);
			

		}
 		LOG_D("[httpserv] decode create_account suc\n");
 	}
	else
	{
		LOG_E("[httpserv] decode create_account fail\n");
	}
	return 0;
}

typedef enum CRM_STATUS
{
	CRM_STATUS_FRESH = 1000,
	CRM_STATUS_NORMAL = 1001,
	CRM_STATUS_SUSPEND = 1205,
	CRM_STATUS_DISABLE = 1202,
	CRM_STATUS_BLACKLIST = 1601,
	CRM_STATUS_RECYCLE = 1101,
		
}_CRM_STATUS;

typedef enum OCS_STATUS
{
	OCS_STATUS_FRESH = 0,
	OCS_STATUS_NORMAL = 1,
	OCS_STATUS_SUSPEND = 2,
	OCS_STATUS_BLACKLIST = 3,
	OCS_STATUS_RECYCLE = 4,
	OCS_STATUS_OPRTRAIL= 5,	
	OCS_STATUS_DISABLE = 6,
}_OCS_STATUS;

int crm_subs_status(u32 crm_status)
{
	int status = CRM_STATUS_NORMAL;

	switch(crm_status)
	{
		case CRM_STATUS_FRESH:
			status = OCS_STATUS_FRESH;
		break;
		case CRM_STATUS_NORMAL:
			status = OCS_STATUS_NORMAL;
		break;
		case CRM_STATUS_SUSPEND:
			status = OCS_STATUS_SUSPEND;			
		break;
		case CRM_STATUS_DISABLE:
			status = OCS_STATUS_DISABLE;			
		break;
		case CRM_STATUS_BLACKLIST:
			status = OCS_STATUS_BLACKLIST;			
		break;
		case CRM_STATUS_RECYCLE:
			status = OCS_STATUS_RECYCLE;			
		break;
		default:
			status = OCS_STATUS_SUSPEND;			
		break;
	}

	return status;
}

static int _selfcare_decode_update_subs(const char *buff, const int len, _rest_msg_s *msg)
{
	char buf[128];
	u32	value;
	msg->msg_type = REST_CRM_UPDATE_SUBS_REQ;

	json_t *root = NULL;
	if(json_parse_document(&root, buff) == JSON_OK)
 	{
		if(root != NULL && root->type == JSON_OBJECT)
		{
			LOG_D("\n");
			_selfcare_set_str(root, msg->msg.update_subs.msisdn, "telNumber");
			if(_selfcare_set_u32(root, &value, "status"))
			{
				msg->msg.update_subs.status = value; //crm_subs_status(value);
				msg->msg.update_subs.optional_flag |= 0x01;				
			}
			
			if(_selfcare_set_u32(root, &msg->msg.update_subs.balance, "balance"))
			{
				msg->msg.update_subs.optional_flag |= 0x02;

			}
			if(_selfcare_set_str(root, buf, "balanceExpDate"))
			{
				msg->msg.update_subs.balance_expiry_date = TransStrTimeToSecond(buf);
				msg->msg.update_subs.optional_flag |= 0x04;
			}
			
			if(_selfcare_set_u32(root, &msg->msg.update_subs.basic_plan_id, "packageId"))
			{
				int rent;
				rent = dba_crm_get_product_rent(1, msg->msg.update_subs.basic_plan_id);			
				msg->msg.update_subs.optional_flag |= 0x08;
				msg->msg.update_subs.basic_plan_rent = rent;
				msg->msg.update_subs.optional_flag |= 0x10;
			}
		}
 		LOG_D("[httpserv]  _selfcare_decode_update_subs suc\n");
 	}
	else
	{
		LOG_E("[httpserv] _selfcare_decode_update_subs fail\n");
	}
	return 0;
}


static int _selfcare_decode_delete_subs(const char *buff, const int len, _rest_msg_s *msg)
{
	//char buf[128];
	u32	cause=0;
	msg->msg_type = REST_CRM_DELETE_SUBS_REQ;

	json_t *root = NULL;
	if(json_parse_document(&root, buff) == JSON_OK)
 	{
		if(root != NULL && root->type == JSON_OBJECT)
		{
			LOG_D("\n");
			_selfcare_set_str(root, msg->msg.delete_subs.msisdn, "telNumber");
			_selfcare_set_u8(root, &msg->msg.delete_subs.account_type, "acctType");
			_selfcare_set_u32(root, &msg->msg.delete_subs.account_id, "acctID");			
			//_selfcare_set_u32(root, &cause, "reason");

			msg->msg.delete_subs.cause = cause;
		}
 		LOG_D("[httpserv]  _selfcare_decode_deletee_subs suc\n");
 	}
	else
	{
		LOG_E("[httpserv] _selfcare_decode_deletee_subs fail\n");
	}
	return 0;
}


	
int selfcare_decode(struct evhttp_request *req,
							const SELFCARE_URL_E req_type,
							_rest_msg_s *msg)
{
	typedef struct
	{
		int (*handle)(const char *buff, const int len, _rest_msg_s *msg);
	}ss;
	ss hd[SELFCARE_TYPE_MAX];
	hd[SELFCARE_AUTHCODE].handle = _selfcare_decode_authcode;
	hd[SELFCARE_QUERY_USERDATA].handle =_selfcare_decode_query_user_data;
	hd[SELFCARE_BUNDLE_SUBS].handle = _selfcare_decode_bundle_subs;
	hd[SELFCARE_BUNDLE_USAGE].handle = _selfcare_decode_bundle_usage;
	hd[SELFCARE_RECHARGE].handle = _selfcare_decode_recharge;
	hd[SELFCARE_TRANSFER].handle = _selfcare_decode_transfer;
	hd[SELFCARE_RECHARGE_CARD].handle = _selfcare_decode_recharge_card;
	hd[SELFCARE_CHECK_BALANCE].handle = _selfcare_decode_check_balance;
	hd[SELFCARE_QUERY_BALANCE].handle = _selfcare_decode_query_balance;
	hd[SELFCARE_CRM_PAYMENT].handle = _selfcare_decode_crm_payment;

	hd[SELFCARE_SMS_DELIVER].handle = _selfcare_decode_sms_deliver;

	hd[SELFCARE_CREATE_ACCT].handle = _selfcare_decode_create_account;
	hd[SELFCARE_UPDATE_SUBS].handle = _selfcare_decode_update_subs;
	hd[SELFCARE_DELETE_SUBS].handle = _selfcare_decode_delete_subs;


	struct evkeyvalq *headers;
	struct evkeyval *header;
	struct evbuffer *buf;

	headers = evhttp_request_get_input_headers(req);
	for (header = headers->tqh_first; header; header = header->next.tqe_next)
	{
		LOG_D(" %s: %s\n", header->key, header->value);
	}

	
	buf = evhttp_request_get_input_buffer(req);
	while (evbuffer_get_length(buf))
	{
		int n;
		char cbuf[1024] = {0};
		n = evbuffer_remove(buf, cbuf, sizeof(cbuf));
		if (n > 0)
		{
			(void)fwrite(cbuf, 1, n, stdout);

			hd[req_type].handle(cbuf, n, msg);
		}
	}

	return 0;
}

void _httpsrv_encode_str(json_t* p_obj, const char* key, const char* value)
{
    json_t  *key_child   = NULL;
    json_t  *key_value   = NULL;
    
    char*  in_key  = (char *)malloc(strlen((char *)value)+1);
    memset (in_key, 0, strlen((char *)value)+1);
    memcpy (in_key, (char *)value, strlen((char *)value)+1);
    
    key_child  = json_new_string(key);
    key_value = json_new_string((char *)in_key);
    json_insert_child(key_child, key_value);
    json_insert_child(p_obj, key_child); 
    
    free(in_key);
}
void _httpsrv_encode_u32(json_t* p_obj, const char* key, int value)
{
	char value_str[8] = {0};
	snprintf(value_str, 8, "%d", value);
	_httpsrv_encode_str(p_obj, key, value_str);
}
void _httpsrv_encode_u64(json_t* p_obj, const char* key, u64 value)
{
	char value_str[8] = {0};
	snprintf(value_str, 8, "%llu", value);
	_httpsrv_encode_str(p_obj, key, value_str);
}
void _httpsrv_encode_time(json_t* p_obj, const char* key, u64 value)
{
	struct tm *pTMNowTime;
	time_t     tTemptime;
	
	tTemptime  = value;
	pTMNowTime = localtime(&tTemptime);
	if( pTMNowTime == NULL )
		return ;

	char value_str[32] = {0};
	snprintf(value_str, 32, "%02d-%02d-%02d %02d:%02d:%02d",
					pTMNowTime->tm_year + 1900,
					pTMNowTime->tm_mon + 1,
					pTMNowTime->tm_mday,
					pTMNowTime->tm_hour,
					pTMNowTime->tm_min,
					pTMNowTime->tm_sec);
	_httpsrv_encode_str(p_obj, key, value_str);
}

static int _httpsrv_encode_autchcode_resp(const _rest_msg_s *udp_msg, char **buf)
{
	json_t* root = json_new_object();
	
	_httpsrv_encode_u32(root, "code", udp_msg->msg.send_authcode_res.error_code);
	_httpsrv_encode_str(root, "message", rest_proxy_errcodemsg(udp_msg->msg.send_authcode_res.error_code));
	

	json_tree_to_string(root, buf);
	
	LOG_D("[httpsrv-encode] send_authcode_res suc. resp[%s]\n", *buf);

	return SUCCESS;
}

static int _httpsrv_encode_queryusrdata_resp(const _rest_msg_s *udp_msg, char **buf)
{
	json_t* root = json_new_object();

	_httpsrv_encode_u32(root, "code", udp_msg->msg.query_user_data_res.error_code);
	_httpsrv_encode_str(root, "message", rest_proxy_errcodemsg(udp_msg->msg.query_user_data_res.error_code));

	_httpsrv_encode_u32(root, "status", udp_msg->msg.query_user_data_res.status);
	_httpsrv_encode_str(root, "remark", udp_msg->msg.query_user_data_res.remark);
	_httpsrv_encode_str(root, "telNumber", udp_msg->msg.query_user_data_res.msisdn);
	_httpsrv_encode_str(root, "userGroupName", udp_msg->msg.query_user_data_res.usrGrpName);
	_httpsrv_encode_u32(root, "balance", udp_msg->msg.query_user_data_res.balance);
	_httpsrv_encode_time(root, "expireDate", udp_msg->msg.query_user_data_res.mo_expiry);
	_httpsrv_encode_u32(root, "VoiceMinute", udp_msg->msg.query_user_data_res.mo_voc_min);
	_httpsrv_encode_u32(root, "remainVoiceMinute", udp_msg->msg.query_user_data_res.remain_mo_voc_min);
	//_httpsrv_encode_u32(root, "mtVoiceMinute", udp_msg->msg.query_user_data_res.mt_voc_min);
	//_httpsrv_encode_u32(root, "remainMtVoiceMinute", udp_msg->msg.query_user_data_res.remain_mt_voc_min);
	_httpsrv_encode_u32(root, "smsVolume", udp_msg->msg.query_user_data_res.total_sms_num);
	_httpsrv_encode_u32(root, "remainSmsVolume", udp_msg->msg.query_user_data_res.remain_sms_num);
	_httpsrv_encode_u64(root, "dataVolume", udp_msg->msg.query_user_data_res.total_data_vol);
	_httpsrv_encode_u64(root, "remainDataVolume", udp_msg->msg.query_user_data_res.remain_data_vol);

	json_tree_to_string(root, buf);
	
	LOG_D("[httpsrv-encode] query_user_data_res suc. resp[%s]\n", *buf);

	return SUCCESS;
}
static int _httpsrv_encode_bundlesubs_resp(const _rest_msg_s *udp_msg, char **buf)
{
	json_t* root = json_new_object();

	_httpsrv_encode_u32(root, "code", udp_msg->msg.bundle_subs_res.error_code);
	_httpsrv_encode_str(root, "message", rest_proxy_errcodemsg(udp_msg->msg.bundle_subs_res.error_code));  

	json_tree_to_string(root, buf);
	
	LOG_D("[httpsrv-encode] bundle_subs_res suc. resp[%s]\n", *buf);

	return SUCCESS;
}
static int _httpsrv_encode_bundleusage_resp(const _rest_msg_s *udp_msg, char **buf)
{
	json_t* root = json_new_object();

	_httpsrv_encode_u32(root, "code", udp_msg->msg.bundle_usage_res.error_code);
	_httpsrv_encode_str(root, "message", rest_proxy_errcodemsg(udp_msg->msg.bundle_usage_res.error_code));  

    json_t *data_key = json_new_string("data");
    json_t *data_arr   = json_new_array();
    json_insert_child(root, data_key);
    json_insert_child(data_key, data_arr);
    json_t* tmp  = json_new_object();
    _httpsrv_encode_u32(tmp, "moVoiceInSecond", udp_msg->msg.bundle_usage_res.moVoiceInSec);
	_httpsrv_encode_u32(tmp, "mtVoiceInSecond", udp_msg->msg.bundle_usage_res.mtVoiceInSec);
    _httpsrv_encode_u32(tmp, "dataVolumeInKB", udp_msg->msg.bundle_usage_res.dataVolInKB);
    _httpsrv_encode_u32(tmp, "smsVolume", udp_msg->msg.bundle_usage_res.smsNum);
    _httpsrv_encode_time(tmp, "expiredTimestamp", udp_msg->msg.bundle_usage_res.expiredTime);
    json_insert_child(data_arr, tmp);

	json_tree_to_string(root, buf);
	
	LOG_D("[httpsrv-encode] bundle_usage_res suc. resp[%s]\n", *buf);

	return SUCCESS;

}
static int _httpsrv_encode_recharge_resp(const _rest_msg_s *udp_msg, char **buf)
{
	json_t* root = json_new_object();

      int error_code = udp_msg->msg.recharge_res.error_code;
        if(error_code == 0)
                error_code = 2001;

        _httpsrv_encode_u32(root, "code", error_code);
   	_httpsrv_encode_str(root, "message", rest_proxy_errcodemsg(udp_msg->msg.recharge_res.error_code));  

	_httpsrv_encode_u32(root, "status", udp_msg->msg.recharge_res.status);
	_httpsrv_encode_u32(root, "balance", udp_msg->msg.recharge_res.balance);
    _httpsrv_encode_time(root, "expiredTimestamp", udp_msg->msg.recharge_res.expiredTime);

	json_tree_to_string(root, buf);
	
	LOG_D("[httpsrv-encode] recharge_res suc. resp[%s]\n", *buf);

	return SUCCESS;
}
static int _httpsrv_encode_transfer_resp(const _rest_msg_s *udp_msg, char **buf)
{
	json_t* root = json_new_object();
	
	_httpsrv_encode_u32(root, "code", udp_msg->msg.transfer_res.error_code);
	_httpsrv_encode_str(root, "message", rest_proxy_errcodemsg(udp_msg->msg.transfer_res.error_code));

    _httpsrv_encode_u32(root, "balance", udp_msg->msg.transfer_res.balance);

	json_tree_to_string(root, buf);
	
	LOG_D("[httpsrv-encode] transfer_res suc. resp[%s]\n", *buf);

	return SUCCESS;
}
static int _httpsrv_encode_rechargecard_resp(const _rest_msg_s *udp_msg, char **buf)
{
	json_t* root = json_new_object();
	
	_httpsrv_encode_u32(root, "code", udp_msg->msg.recharge_card_res.error_code);
	_httpsrv_encode_str(root, "message", rest_proxy_errcodemsg(udp_msg->msg.recharge_card_res.error_code));

	_httpsrv_encode_u32(root, "rechargeAmount", udp_msg->msg.recharge_card_res.rechargeAmount);
	_httpsrv_encode_u32(root, "balance", udp_msg->msg.recharge_card_res.balance);
    _httpsrv_encode_time(root, "expiredTimestamp", udp_msg->msg.recharge_card_res.expiredTime);

	json_tree_to_string(root, buf);
	
	LOG_D("[httpsrv-encode] recharge_card_res suc. resp[%s]\n", *buf);

	return SUCCESS;
}
static int _httpsrv_encode_checkbalance_resp(const _rest_msg_s *udp_msg, char **buf)
{
	json_t* root = json_new_object();
	
	_httpsrv_encode_u32(root, "code", udp_msg->msg.check_balance_res.error_code);
	_httpsrv_encode_str(root, "message", rest_proxy_errcodemsg(udp_msg->msg.check_balance_res.error_code));

	_httpsrv_encode_u32(root, "available", udp_msg->msg.check_balance_res.available);

	json_tree_to_string(root, buf);
	
	LOG_D("[httpsrv-encode] check_balance_res suc. resp[%s]\n", *buf);

	return SUCCESS;
}
static int _httpsrv_encode_querybalance_resp(const _rest_msg_s *udp_msg, char **buf)
{
	json_t* root = json_new_object();
	
	_httpsrv_encode_u32(root, "code", udp_msg->msg.query_balance_res.error_code);
	_httpsrv_encode_str(root, "message", rest_proxy_errcodemsg(udp_msg->msg.query_balance_res.error_code));

	_httpsrv_encode_u32(root, "status", udp_msg->msg.query_balance_res.status);
	_httpsrv_encode_u32(root, "balance", udp_msg->msg.query_balance_res.balance);
	_httpsrv_encode_time(root, "expireDate", udp_msg->msg.query_balance_res.mo_expiry);

	json_tree_to_string(root, buf);
	
	LOG_D("[httpsrv-encode] query_balance_res suc. resp[%s]\n", *buf);

	return SUCCESS;
}

static int _httpsrv_encode_createaccount_resp(const _rest_msg_s *udp_msg, char **buf)
{
	json_t* root = json_new_object();
	
	_httpsrv_encode_u32(root, "code", udp_msg->msg.create_acct_res.error_code);
	_httpsrv_encode_str(root, "message", rest_proxy_errcodemsg(udp_msg->msg.create_acct_res.error_code));

	json_tree_to_string(root, buf);
	
	LOG_D("[httpsrv-encode] _httpsrv_encode_createaccount_resp suc. resp[%s]\n", *buf);

	return SUCCESS;
}

static int _httpsrv_encode_updatesubs_resp(const _rest_msg_s *udp_msg, char **buf)
{
	json_t* root = json_new_object();
	
	_httpsrv_encode_u32(root, "code", udp_msg->msg.update_subs_res.error_code);
	_httpsrv_encode_str(root, "message", rest_proxy_errcodemsg(udp_msg->msg.update_subs_res.error_code));

	json_tree_to_string(root, buf);
	
	LOG_D("[httpsrv-encode] _httpsrv_encode_updatesubs_resp suc. resp[%s]\n", *buf);

	return SUCCESS;
}

static int _httpsrv_encode_deletesubs_resp(const _rest_msg_s *udp_msg, char **buf)
{
	json_t* root = json_new_object();
	
	_httpsrv_encode_u32(root, "code", udp_msg->msg.delete_subs_res.error_code);
	_httpsrv_encode_str(root, "message", rest_proxy_errcodemsg(udp_msg->msg.delete_subs_res.error_code));

	json_tree_to_string(root, buf);
	
	LOG_D("[httpsrv-encode] _httpsrv_encode_deletesubs_resp suc. resp[%s]\n", *buf);

	return SUCCESS;
}



int selfcare_encode(const SELFCARE_URL_E req_type, const _rest_msg_s *udp_msg, char **buf)
{
	typedef struct
	{
		int (*handle)(const _rest_msg_s *udp_msg, char **buf);
	}ss;
	ss hd[SELFCARE_TYPE_MAX];
	hd[SELFCARE_AUTHCODE].handle        = _httpsrv_encode_autchcode_resp;
	hd[SELFCARE_QUERY_USERDATA].handle  =_httpsrv_encode_queryusrdata_resp;
	hd[SELFCARE_BUNDLE_SUBS].handle     = _httpsrv_encode_bundlesubs_resp;
	hd[SELFCARE_BUNDLE_USAGE].handle    = _httpsrv_encode_bundleusage_resp;
	hd[SELFCARE_RECHARGE].handle        = _httpsrv_encode_recharge_resp;
	hd[SELFCARE_TRANSFER].handle        = _httpsrv_encode_transfer_resp;
	hd[SELFCARE_RECHARGE_CARD].handle   = _httpsrv_encode_rechargecard_resp;
	hd[SELFCARE_CHECK_BALANCE].handle   = _httpsrv_encode_checkbalance_resp;
	hd[SELFCARE_QUERY_BALANCE].handle   = _httpsrv_encode_querybalance_resp;
	hd[SELFCARE_CREATE_ACCT].handle   = _httpsrv_encode_createaccount_resp;
	hd[SELFCARE_UPDATE_SUBS].handle   = _httpsrv_encode_updatesubs_resp;
	hd[SELFCARE_DELETE_SUBS].handle   = _httpsrv_encode_deletesubs_resp;


	hd[req_type].handle(udp_msg, buf);
	
	return SUCCESS;
}

