#ifndef __CURL_ADAPTOR_H__
#define __CURL_ADAPTOR_H__

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <assert.h>
#include "multi.h"
#include "curl.h"
#include "easy.h"
#include "common.h"

/* for For querying monetary or wallet balance */
#define URLPrefix						"" // "http://"
#define URLFetchSub						"/rest-services/subscriber/%s"
#define URLFetchUPBalance			"/rest-services/subscriber/%s/info/userpaymentbalance"
#define URLFetchInfoBalance			"/rest-services/subscriber/%s/info/balance"

/* url: 查询 */
#define URLFetchWalletBalanceGetsubsid 	g_rest_proxy_env.cnf.url_fetchwalletbalance_getsubsid
#define URLFetchWalletBalance			g_rest_proxy_env.cnf.url_fetchwalletbalance

/* url: 充值卡充值 */
#define URLRechargeGetsubsid 			g_rest_proxy_env.cnf.url_recharge_getsubsid
#define URLRecharge						g_rest_proxy_env.cnf.url_recharge

/* url: 余额转账 */
#define URLTransGetsubidSender			g_rest_proxy_env.cnf.url_trans_getsubsid_sender
#define URLTransGetsubidReceiver		g_rest_proxy_env.cnf.url_trans_getsubsid_receiver
#define URLTrans						g_rest_proxy_env.cnf.url_trans

#define RESTFUL_IP_SIZE 32
#define RESTFUL_CUL_BUF_MAX 256

#define RESTFUL_SUBSID_LEN 24
#define RESTFUL_MSISDN_LEN 24
#define RESTFUL_USERNAME 64
#define RESTFUL_PASSWOR 64

typedef struct _SRestfulInfo
{
	char			queryIP[RESTFUL_IP_SIZE];
	unsigned int	queryPort;

	char			recharegeIP[RESTFUL_IP_SIZE];
	unsigned int	recharegePort;    
}SRestFulInfo;

extern SRestFulInfo g_restful;


typedef struct _SubscriberIdentity
{
	unsigned char	identityChoice;	/*1:imsi,2:msisdn*/
	unsigned char	msisdn[8+1];		/* TBCD */
	unsigned char	imsi[8+1];			/* TBCD */					

}SubscriberIdentity;


typedef enum _ECurlType
{
//	URL_FETCH_SUB = 0x00,

	URL_FETCH_UPBalance = 0x00,
	URL_FETCH_InfoBalance,
	URL_FETCH_WalletBalance_Getsubsid,
	URL_FETCH_WalletBalance,	/* for For querying monetary or wallet balance */
	URL_RECHAGRGE_Getsubsid,
	URL_RECHAGRGE,			/* for Voucher recharge */

	URL_TRANS_GetsubsidSender,		/* 转账获取sender的subscriberID的url */
	URL_TRANS_GetsubsidReceiver,	/* 转账获取receiver的subscriberID的url */
	URL_TRANS,						/* 转账的url */
	
	URL_PPC_OfferGetAllV2_1,
	
	URL_MAX_NUM,
}ECurlType;



typedef struct _SCurlRestAdaptorHeader
{
	unsigned short		ppsAppID;
	unsigned short		restproxyAppID;
}SCurlRestAdaptorHeader;

typedef struct _SCurlDataFetchSubscribe
{
	SubscriberIdentity	subID;
}SCurlDataFetchSubscribe;

typedef union _UCurlAdaptorVal
{
	SCurlDataFetchSubscribe	subscribeID;
}UCurlAdaptorVal;

/* restful query struct */
typedef struct _SCurlRestAdaptorQuery
{
	char			msisdn[RESTFUL_MSISDN_LEN];
	char			subsid[RESTFUL_SUBSID_LEN];   
}SCurlRestAdaptorQuery;

/* restful query response struct */
typedef struct _SCurlRestAdatptorQueryRes
{
	char			optional_flag;
	char			result;
	char			error_code;
	unsigned int	balance;
	unsigned int	mo_expiry;
	unsigned int	mt_expiry;
}SCurlRestAdatptorQueryRes;

/* restful topup struct */
typedef struct _SCurlRestAdatptorTopup
{
	char			msisdn[RESTFUL_MSISDN_LEN];
    char			pin[RESTFUL_MSISDN_LEN];
	
    char			subsid[RESTFUL_SUBSID_LEN];
}SCurlRestAdatptorTopup;

/* restful topup response struct */
typedef struct _SCurlRestAdatptorTopupRes
{
	char			optional_flag;
	char			result;
	char			error_code;
	unsigned int	balance;
	unsigned int	mo_expiry;
	unsigned int	mt_expiry;
}SCurlRestAdatptorTopupRes;

typedef struct _SCurlRestAdatptorTransfer
{
	char	msisdn_sender[24];		// 转出的号码
    char 	msisdn_receiver[24];	// 转入的号码
    unsigned int 	money;					// 转账金额

	char	subsid_sender[RESTFUL_SUBSID_LEN]; 	 // 转出账号对应的subscriber-id
	char	subsid_receiver[RESTFUL_SUBSID_LEN]; // 转入账号对应的subscriber-id
}SCurlRestAdatptorTransfer;

typedef struct _SCurlRestAdatptorTransferRes
{
	unsigned char		optional_flag;
	unsigned char		result;
	unsigned char		error_code;
}SCurlRestAdatptorTransferRes;

typedef struct _SCurlAdaptor
{
	ECurlType		urlType;
	char				urlBuf[RESTFUL_CUL_BUF_MAX];

	SCurlRestAdaptorHeader			header;
	union	
	{
		SCurlRestAdaptorQuery		query;
		SCurlRestAdatptorQueryRes	query_res;
		SCurlRestAdatptorTopup		topup;
		SCurlRestAdatptorTopupRes	topup_res;
		SCurlRestAdatptorTransfer	transfer;
		SCurlRestAdatptorTransferRes	transfer_res;
	}msg;	
}SCurlAdaptor;


/*
 * curl adaptor deliver the http request, receive and resolve the reponse, then pass the info to up layer
 * curAdaptor: resolve information
 * retCode: 200->success
 */
typedef int (*CURL_ADAPTOR_APP_CB)(SCurlAdaptor *curAdaptor, int retCode);


/*
 * function: init curl adaptor.
 * appCB: curl upper layer call back
 * restfulIP: restful host ip. host order
 * restfulPort: restful port. host order
 * ret: SUCCESS or FAILURE
 */
int curl_adaptor_init(CURL_ADAPTOR_APP_CB appCB, const char* queryIP, unsigned int queryPort, const char* recharegeIP, unsigned int recharegePort);

/*
 * function:  prepare to call restful api
 * curAdaptor: para that to be send as parameter
 * ret: SUCCESS or FAILURE
 */
int curl_adaptor_add(SCurlAdaptor* curAdaptor);

/*
 * function: relese curl adaptor resource
 * ret: SUCCESS or FAILURE
 */
int curl_adaptor_fini();


#endif /* __CURL_ADAPTOR_H__ */
