#ifndef _RESTAPI_H
#define _RESTAPI_H

#ifdef __cplusplus
extern "C" {
#endif


#include "common.h"

#define	MAX_BODY_LEN	1024

#define REST_PROXY_PORT	4951

enum RESULT_TYPE 
{
	RES_SUCCEED = 0,
	RES_FAILED,
};

enum OCS_RESULT_TYPE 
{
	OCS_RES_SUCCEED = 2001,
};

enum ERROR_CODE 
{
	ERR_UNKNOWN_SUBS = 1,
	ERR_WRONG_PWD,
	ERR_WRONG_CARD_STATUS,
	ERR_WRONG_CARD_EXPIRED,	
	ERR_TIMEROUT,
	ERR_NO_PREFIX,
};

enum REST_PROXY_MSG_TYPE 
{
	REST_QUERY=1,
	REST_TOPUP=2,
	REST_TRANSFER=3,

	// for selfcare
	REST_SEND_AUTHCODE_REQ=5,   // 用户注册短信接口
	REST_SEND_AUTHCODE_RSP=6,
	REST_QUERY_USERDATA_REQ=7,  // 用户信息查询接口
	REST_QUERY_USERDATA_RSP=8,
	REST_BUNDLE_SUBS_REQ=9,     // 套餐订购接口
	REST_BUNDLE_SUBS_RSP=10,
	REST_BUNDLE_USAGE_REQ=11,   // 已订购套餐查询
	REST_BUNDLE_USAGE_RSP=12,
	REST_RECHARGE_REQ=13,       // 充值接口
	REST_RECHARGE_RSP=14,
	REST_TRANSFER_REQ=15,       // 转账接口
	REST_TRANSFER_RSP=16,
	REST_RECHARGE_CARD_REQ=17,  // 充值卡充值接口
	REST_RECHARGE_CARD_RSP=18,
	REST_CHECK_BALANCE_REQ=19,  // 用户余额校验接口
	REST_CHECK_BALANCE_RSP=20,
	REST_QUERY_BALANCE_REQ=21,  // 余额查询接口
	REST_QUERY_BALANCE_RSP=22,
	REST_QUERY_RECHARGE_CARD_REQ=23,  // query recharge card interface from CRM
	REST_QUERY_RECHARGE_CARD_RSP=24,
	REST_UPDATE_RECHARGE_CARD_REQ=25,  // update recharge card interface with CRM
	REST_UPDATE_RECHARGE_CARD_RSP=26,

	REST_CRM_PAYMENT_REQ=27,
	REST_CRM_PAYMENT_RES=28,

	REST_CRM_SMS_DELIVER_REQ=29,
	REST_CRM_SMS_DELIVER_RES=30,

	REST_CRM_CREATE_ACCT_REQ=31,
	REST_CRM_CREATE_ACCT_RES=32,

	REST_CRM_QUERY_TARIFF_REQ=33,
	REST_CRM_QUERY_TARIFF_RES=34,

	REST_CRM_UPDATE_SUBS_REQ=35,
	REST_CRM_UPDATE_SUBS_RES=36,

	REST_CRM_DELETE_SUBS_REQ=37,
	REST_CRM_DELETE_SUBS_RES=38,
	
	REST_CRM_UPDATE_SESS_INFO_REQ=39,
	REST_CRM_UPDATE_SESS_INFO_RES=40,

	REST_CRM_UPDATE_PLAN_INFO_REQ=41,	
	REST_CRM_UPDATE_PLAN_INFO_RES=42,	

	REST_CRM_RENT_CHARGE=43,
	
	
	//
};

#if 0
enum PAY_TYPE
{
	PAY_TYPE_BALANCE = 1,
	PAY_TYPE_PAYPAL = 2,
	PAY_TYPE_VISA = 3,
	PAY_TYPE_MASTER = 4,
};

enum ACCT_STATUS
{
    AS_FRESH,           /* fresh account, can't dial in or out, 
                              but can recharge. */
    AS_NORMAL,      /* normal account */
    AS_SUSPEND,
    AS_BLACKLIST,
    AS_RELEASED,
    AS_OPRTRIAL,
    //AS_MAX_USER_TYPE,

    AS_EXTERNAL=10,
};
#endif

enum REST_IE_TAG
{
	IE_SRC_REF=1,
	IE_DST_REF,
	IE_MSISDN,
	IE_RESULT,
	IE_ERROR_CODE,
	IE_BALANCE,
	IE_MO_EXPIRY,
	IE_MT_EXPIRY,
	IE_USERNAME,
	IE_PASSWORD,
	IE_MSG_CONTENT,//value=11
	IE_STATUS,
	IE_REMARK,
	IE_GROUP_NAME,
	IE_MO_VOICE_MIN,// in minute
	IE_REMAIN_MO_VOICE_SEC,// in second
	IE_REMAIN_MO_VOICE_MIN,// in minute
	IE_MT_VOICE_MIN,// in minute
	IE_REMAIN_MT_VOICE_SEC,// in second
	IE_REMAIN_MT_VOICE_MIN,// in minute
	IE_SMS_NUM, //value=21
	IE_REMAIN_SMS_NUM,
	IE_DATA_VOL_MB,// in MB
	IE_REMAIN_DATA_VOL_KB,// in KB
	IE_REMAIN_DATA_VOL_MB,// in MB
	IE_PAY_TYPE,
	IE_AMOUNT,
	IE_VALID_DAYS,
	IE_EXPIRY_TIME,
	IE_MSISDN_TRANS_OUT,
	IE_MSISDN_TRANS_IN, //value=31
	IE_BALANCE_AVAILABLE,
	IE_RECHARGE_AMOUNT,
	IE_RECHARGE_TYPE,
	IE_RECHARGE_CARD_STATUS,
	IE_RECHARGE_CARD_FACE_VALUE,
	IE_RECHARGE_CARD_EXPIRED_TS,	
	IE_RECHARGE_CARD_UPDATED_TS,		
	IE_CUSTOMER_ID,
	IE_ACCOUNT_ID,
	IE_PRODUCT_ID, //value=41
	IE_PLAN_ID,
	IE_RENT_CHARGE,
	IE_BIRTHDAY,
	IE_SMS_CONTENT,	
	IE_SERVICE_TYPE,
	IE_TARIIFF_PREFIX,
	IE_TARIFF_UNIT,
	IE_TARIFF_CHARGE,
	IE_TARIFF_DISCOUNT,
	IE_PLAN_VALUE,//value=51
	IE_PLAN_USED_VALUE,
	IE_BUNDLE_ID,	
	IE_CAUSE,
	IE_SESS_FLAG,
	IE_TIMESTAMP,
	IE_CONSUME_VALUE,
	IE_CALLED_NUMBER,
	IE_UE_IP,
	IE_GW_IP,
	IE_CUG_ID,
	IE_VAS_CUG_STATUS,
	IE_SESS_UPDATE_TIME,
	IE_PLAN_VALUE_ADD_THIS_TIME,
	IE_USER_CLASS,
	IE_MAX_NUM,
};

typedef struct rest_header
{
	u16	src_ref;
	u16	dst_ref;
}_rest_header;

typedef struct rest_query
{
	char	msisdn[24];
}_rest_query;

typedef struct rest_query_res
{
	u8	optional_flag;
	u8	result;
	u8	error_code;
	u32	balance;
	u32	mo_expiry;
	u32	mt_expiry;
}_rest_query_res;

typedef struct rest_topup
{
	u8	optional_flag; /* optional=1 ,account_id is presented */
	char	msisdn[24];

	char	username[24];
	char	password[24];
	u32 balance;
	long account_id;
}_rest_topup;


typedef struct rest_topup_res
{
	u8	optional_flag;
	u8	result;
	u8	error_code;
	u32	balance;
	u32	mo_expiry;
	u32	mt_expiry;
}_rest_topup_res;

// for selfcare =============================
typedef struct rest_send_authcode
{
	char	msisdn[24];
	char	sms_content[MAX_BODY_LEN];
}_rest_send_authcode;

typedef struct rest_send_authcode_res
{
	u8	optional_flag;
	u8	result;
	u32	error_code;
}_rest_send_authcode_res;

typedef struct rest_query_user_data
{
	char	msisdn[24];
}_rest_query_user_data;

typedef struct rest_query_user_data_res
{
	u8	optional_flag;
	u8	result;
	u32	error_code;

	char	msisdn[24];
	u8    status;// use ACCT_STATUS
	char remark[64];
	char usrGrpName[32];// usr data==>cos id==>cos title
	u32	balance;
	u64	mo_expiry;
	u32	mo_voc_min;// in Min
	u32	remain_mo_voc_min;
	u32	mt_voc_min;// in Min
	u32	remain_mt_voc_min;
	u32	total_sms_num;// reserved, set to 0 or do not send
	u32	remain_sms_num;
	u32	total_data_vol;// reserved, set to 0 or do not send
	u32	remain_data_vol;
}_rest_query_user_data_res;

typedef struct rest_bundle_subs
{
	char	msisdn[24];
	u8 payType;// 1: balance; 2: paypal; 3: visa; 4: master, use PAY_TYPE
	u32 charge;// Conditional, mandatory if balance
	u32 moVoiceMinute;
	u32 mtVoiceMinute;
	u32 dataVolume;// MB
	u32 smsNum;
	u32 validDays;
}_rest_bundle_subs;


typedef struct rest_bundle_subs_res
{
	u8	optional_flag;
	u8	result;
	u32	error_code;
}_rest_bundle_subs_res;


typedef struct rest_bundle_usage
{
	char	msisdn[24];
}_rest_bundle_usage;


typedef struct rest_bundle_usage_res
{
	u8	optional_flag;
	u8	result;
	u32	error_code;
	u32	moVoiceInSec;// remained mo voice in second
	u32	mtVoiceInSec;// remained mt voice in second
	u32  dataVolInKB;// remained data volume in KB
	u32  smsNum;// remained sms
	u64  expiredTime;// expired time
}_rest_bundle_usage_res;


typedef struct rest_recharge
{
	char	msisdn[24];

	u32	amount;// mandatory, add to balance
	u8   op_type;	//0=charge, 1=recharge
	u16  valid_days;
}_rest_recharge;


typedef struct rest_recharge_res
{
	u8	optional_flag;
	u8	result;
	u32	error_code;
	u8	status;	
	u32	balance;// current balance
	u64	expiredTime;// expired time
}_rest_recharge_res;


typedef struct rest_transfer
{
	char	transferOutMsisdn[24];
	char	transferInMsisdn[24];

	u32	amount;// mandatory
}_rest_transfer;


typedef struct rest_transfer_res
{
	u8	optional_flag;
	u8	result;
	u32	error_code;
	u32	balance;
}_rest_transfer_res;


typedef struct rest_recharge_card
{
	char	msisdn[24];

	char	cardPwd[32];// rechare card password
}_rest_recharge_card;


typedef struct rest_recharge_card_res
{
	u8	optional_flag;
	u8	result;
	u32	error_code;
	u32	rechargeAmount;// mandatory, add to balance
	u32	balance;// current balance
	u64	expiredTime;// expired time
}_rest_recharge_card_res;


typedef struct rest_check_balance
{
	char	msisdn[24];

	u32	amount;// mandatory
}_rest_check_balance;


typedef struct rest_check_balance_res
{
	u8	optional_flag;
	u8	result;
	u32	error_code;
	u8	available;// 0: enough balance; 1: else
}_rest_check_balance_res;


typedef struct rest_query_balance
{
	char	msisdn[24];
}_rest_query_balance;


typedef struct rest_query_balance_res
{
	u8	optional_flag;
	u8	result;
	u32	error_code;
	u8	status;
	u32	balance;// current balance
	u32 mo_expiry;
}_rest_query_balance_res;

typedef struct rest_query_recharge_card
{
	char	cardPwd[32];// rechare card password
}_rest_query_recharge_card;


typedef struct rest_query_recharge_card_res
{
	u8	optional_flag;
	u8	result;        //0=succeed, 1=failed
	u32	error_code;//1=no such instance
	u32	status;//0=fresh, 1= used
	u32	face_value; 
	u32	expiredTime;// expired time
}_rest_query_recharge_card_res;

typedef struct rest_update_recharge_card
{
	char	cardPwd[32];// rechare card password
	u8   status;
	u64 updatedTime;
	char msisdn[32];
}_rest_update_recharge_card;


typedef struct rest_update_recharge_card_res
{
	u8	optional_flag;
	u8	result;
	u32	error_code;
}_rest_update_recharge_card_res;

typedef struct rest_payment
{
	char	msisdn[24];

	u32	amount;// mandatory, add to balance
}_rest_payment;


typedef struct rest_payment_res
{
	u8	optional_flag;
	u8	result;
	u32	error_code;
	u32	balance;// current balance
	u64	expiredTime;// expired time
}_rest_payment_res;

typedef struct rest_sms_deliver_req
{
	char	msisdn[24];
	u16   sms_len;
	char	sms_content[MAX_BODY_LEN];
}_rest_sms_deliver_req;

typedef struct rest_sms_deliver_res
{
	u8	optional_flag;
	u8	result;
	u32	error_code;
}_rest_sms_deliver_res;

typedef struct rest_create_acct_req
{
	char	msisdn[24];
	u32  customer_id;
	u32  account_id;
	u32  product_id;
	u32  plan_id;  /* basic plan */
	u8	user_class;
	u32  balance;
	u32  expiry_date;
	u32	 rent_charge;
	u32  birthday;
	u32  cug_id;
}_rest_create_acct_req;

typedef struct rest_create_acct_res
{
	u8	optional_flag;
	u8	result;
	u32	error_code;
}_rest_create_acct_res;

typedef struct rest_query_tariff_req
{
	char msisdn[24];
	u8	service_type; /*0=mo call, 1=mt call, 2=sms, 3=data */
	char	called_number[24];
}_rest_query_tariff_req;

typedef struct rest_query_tariff_res
{
	u8	optional_flag;
	u8	result;
	u32	error_code;
	char prefix[24];
	u32  unit_time;
	u32  unit_charge;
	u32  discount;
	u32	 plan_id;
	u32  bundle_plan_id;
	u64  total_plan_value;
	u64  used_plan_value;	
}_rest_query_tariff_res;

typedef struct rest_update_subs_req
{
	u32	optional_flag;
	char msisdn[24];
	u32  account_id;
	u8	status; 			/*falg=1*/
	u32	balance;
	u32  balance_expiry_date;
	u32	basic_plan_id;
	u32	basic_plan_rent;
	u8	vas_cug_status;
}_rest_update_subs_req;

typedef struct rest_update_subs_res
{
	u8	optional_flag;
	u8	result;
	u32	error_code;
}_rest_update_subs_res;

typedef struct rest_delete_subs_req
{
	char msisdn[24];
	u8    account_type;
	u32  account_id;
	u8	cause; 
}_rest_delete_subs_req;

typedef struct rest_delete_subs_res
{
	u8	optional_flag;
	u8	result;
	u32	error_code;
}_rest_delete_subs_res;

typedef struct rest_update_sess_info_req
{
	u32	optional_flag;			/*bit 0=called, 1= ue_ip, 2=pgw ip */
	char msisdn[24];
	u8   first_sess;				/*if 1 then clear all session in database */
	u8	service_type; 			/*0=mo call, 1=mt call, 2=mo sms, 3= data*/
	u32	start_time;
	u32  consumed_value;		/*duration of call, bytes of data */
	char called[24];				/*for call/sms */
	u32	ue_ip;
	u32  gw_ip;					
}_rest_update_sess_info_req;


typedef struct rest_update_plan_info_req
{
	u32	optional_flag;			/*bit 0=called, 1= ue_ip, 2=pgw ip */
	char msisdn[24];
	u8	service_type; 			/*0=mo call, 1=mt call, 2=mo sms, 3= data*/
	u32  account_id;	
	u32  plan_id;
	u32  bundle_id;
	u64  plan_total_value;
	u64  plan_used_value;
	u32  sess_update_times;
	u64  this_time_add_value;	
}_rest_update_plan_info_req;

typedef struct rest_rent_charge_req
{
	u32	optional_flag;			
	char msisdn[24];
	u32  balance;
	u32  rent_charge;
}_rest_rent_charge_req;

//======================================

typedef struct rest_msg_s
{
	u8	msg_type;
	_rest_header header;	
	union	
	{
		//OCS->CRM
		_rest_query	query;
		_rest_query_res	query_res;
		_rest_topup	topup;
		_rest_topup_res	topup_res;

                // for selfcare
		_rest_send_authcode send_authcode;
		_rest_send_authcode_res send_authcode_res;
		_rest_query_user_data query_user_data;
		_rest_query_user_data_res query_user_data_res;
		_rest_bundle_subs bundle_subs;
		_rest_bundle_subs_res bundle_subs_res;
		_rest_bundle_usage bundle_usage;
		_rest_bundle_usage_res bundle_usage_res;
		_rest_recharge recharge;
		_rest_recharge_res recharge_res;
		_rest_transfer transfer;
		_rest_transfer_res transfer_res;
		_rest_recharge_card recharge_card;
		_rest_recharge_card_res recharge_card_res;
		_rest_check_balance check_balance;
		_rest_check_balance_res check_balance_res;
		_rest_query_balance query_balance;
		_rest_query_balance_res query_balance_res;
		_rest_query_recharge_card	query_recharge_card;
		_rest_query_recharge_card_res	query_recharge_card_res;
		_rest_update_recharge_card		update_recharge_card;
		_rest_update_recharge_card_res	update_recharge_card_res;

		_rest_payment payment;
		_rest_payment_res payment_res;

		_rest_sms_deliver_req sms_deliver;
		_rest_sms_deliver_res sms_deliver_res;

		_rest_create_acct_req create_acct;
		_rest_create_acct_res create_acct_res;

		_rest_query_tariff_req  query_tariff;
		_rest_query_tariff_res   query_tariff_res;

		_rest_update_subs_req  update_subs;
		_rest_update_subs_res   update_subs_res;

		_rest_delete_subs_req  delete_subs;
		_rest_delete_subs_res   delete_subs_res;

		_rest_update_sess_info_req  update_sess_info;

		_rest_update_plan_info_req   update_plan_info;

		_rest_rent_charge_req	rent_charge;
		
		//
	}msg;
}_rest_msg_s;

int encode_rest_query(_rest_msg_s *ptr, u8 *buf);

int encode_rest_query_res(_rest_msg_s *ptr, u8 *buf);

int encode_rest_topup(_rest_msg_s *ptr, u8 *buf);

int encode_rest_topup_res(_rest_msg_s *ptr, u8 *buf);

int decode_rest_api_msg(u8 *buf, int len, _rest_msg_s *ptr);

// for selfcare======================================
int encode_rest(_rest_msg_s  *ptr, u8 *buf);
int decode_rest_api_msg(u8 *buf, int len, _rest_msg_s *ptr);

int encode_rest_send_authcode(_rest_msg_s *ptr, u8 *buf);
int encode_rest_send_authcode_res(_rest_msg_s *ptr, u8 *buf);
int encode_rest_query_user_data(_rest_msg_s *ptr, u8 *buf);
int encode_rest_query_user_data_res(_rest_msg_s *ptr, u8 *buf);
int encode_rest_bundle_subs(_rest_msg_s *ptr, u8 *buf);
int encode_rest_bundle_subs_res(_rest_msg_s *ptr, u8 *buf);
int encode_rest_bundle_usage(_rest_msg_s *ptr, u8 *buf);
int encode_rest_bundle_usage_res(_rest_msg_s *ptr, u8 *buf);
int encode_rest_recharge(_rest_msg_s *ptr, u8 *buf);
int encode_rest_recharge_res(_rest_msg_s *ptr, u8 *buf);
int encode_rest_transfer(_rest_msg_s *ptr, u8 *buf);
int encode_rest_transfer_res(_rest_msg_s *ptr, u8 *buf);
int encode_rest_recharge_card(_rest_msg_s *ptr, u8 *buf);
int encode_rest_recharge_card_res(_rest_msg_s *ptr, u8 *buf);
int encode_rest_check_balance(_rest_msg_s *ptr, u8 *buf);
int encode_rest_check_balance_res(_rest_msg_s *ptr, u8 *buf);
int encode_rest_query_balance(_rest_msg_s *ptr, u8 *buf);
int encode_rest_query_balance_res(_rest_msg_s *ptr, u8 *buf);
//

#ifdef __cplusplus
}
#endif

#endif
