#pragma once
#include <string>
#include "../NetMgr/TcpClient.h"
#include "../pb/common/constant/RequestMsgType.pb.h"
#include "../pb/common/constant/ResponseMsgType.pb.h"
#include "../pb/common/constant/NotifyMsgType.pb.h"
#include "../pb/common/init/InitConnectReq.pb.h"
#include "../pb/common/init/InitConnectResp.pb.h"
#include "../pb/common/msg/HeartBeat.pb.h"
#include "../pb/common/msg/Request.pb.h"
#include "../pb/common/msg/Notify.pb.h"
#include "../pb/common/msg/Response.pb.h"
#include "../pb/common/response/CommonBoolResponse.pb.h"
#include "../pb/common/response/CommonIntResponse.pb.h"
#include "../pb/common/response/CommonStringResponse.pb.h"

#include "../pb/trade/notify/TradeStockDeliverNotify.pb.h"
#include "../pb/trade/request/TradeLoginRequest.pb.h"
#include "../pb/trade/request/TradeCancelEntrustRequest.pb.h"
#include "../pb/trade/request/TradeEntrustRequest.pb.h"
#include "../pb/trade/request/TradeChangeEntrustRequest.pb.h"
#include "../pb/trade/request/TradeQueryBuyAmountRequest.pb.h"
#include "../pb/trade/request/TradeQueryHistoryDeliverListRequest.pb.h"
#include "../pb/trade/request/TradeQueryHistoryEntrustListRequest.pb.h"
#include "../pb/trade/request/TradeQueryHistoryFundJourListRequest.pb.h"
#include "../pb/trade/request/TradeQueryHoldsListRequest.pb.h"
#include "../pb/trade/request/TradeQueryMarginFundInfoRequest.pb.h"
#include "../pb/trade/request/TradeQueryRealDeliverListRequest.pb.h"
#include "../pb/trade/request/TradeQueryRealEntrustListRequest.pb.h"
#include "../pb/trade/request/TradeQueryRealFundJourListRequest.pb.h"
#include "../pb/trade/request/TradeQuerySellAmountRequest.pb.h"

#include "../pb/trade/response/StockQueryHoldsListResponse.pb.h"
#include "../pb/trade/response/TradeQueryHistoryDeliverListResponse.pb.h"
#include "../pb/trade/response/TradeQueryHistoryEntrustListResponse.pb.h"
#include "../pb/trade/response/TradeQueryHistoryFundJourListResponse.pb.h"
#include "../pb/trade/response/TradeQueryMarginFundInfoResponse.pb.h"
#include "../pb/trade/response/TradeQueryRealDeliverListResponse.pb.h"
#include "../pb/trade/response/TradeQueryRealEntrustListResponse.pb.h"
#include "../pb/trade/response/TradeQueryRealFundJourListResponse.pb.h"

#include "../pb/trade/vo/FareVo.pb.h"
#include "../pb/trade/vo/FundJourVo.pb.h"
#include "../pb/trade/vo/HoldsVo.pb.h"
#include "../pb/trade/vo/OrderVo.pb.h"
/////////////////期货
#include "../pb/trade/request/futures/FuturesEntrustRequest.pb.h"
#include "../pb/trade/request/futures/FuturesActivateEntrustRequest.pb.h"
#include "../pb/trade/request/futures/FuturesCancelEntrustRequest.pb.h"
#include "../pb/trade/request/futures/FuturesInactivateAllEntrustListRequest.pb.h"
#include "../pb/trade/request/futures/FuturesInactivateEntrustRequest.pb.h"
#include "../pb/trade/request/futures/FuturesModifyEntrustRequest.pb.h"
#include "../pb/trade/request/futures/FuturesQueryFundInfoRequest.pb.h"
#include "../pb/trade/request/futures/FuturesQueryHistoryDeliverListRequest.pb.h"
#include "../pb/trade/request/futures/FuturesQueryHistoryEntrustListRequest.pb.h"
#include "../pb/trade/request/futures/FuturesQueryHoldsListRequest.pb.h"
#include "../pb/trade/request/futures/FuturesQueryMaxBuySellAmountRequest.pb.h"
#include "../pb/trade/request/futures/FuturesQueryProductInfoRequest.pb.h"
#include "../pb/trade/request/futures/FuturesQueryRealDeliverListRequest.pb.h"
#include "../pb/trade/request/futures/FuturesQueryRealEntrustListRequest.pb.h"

#include "../pb/trade/response/futures/FuturesQueryBuySellAmountResponse.pb.h"
#include "../pb/trade/response/futures/FuturesQueryFundInfoResponse.pb.h"
#include "../pb/trade/response/futures/FuturesQueryHistoryDeliverListResponse.pb.h"
#include "../pb/trade/response/futures/FuturesQueryHistoryEntrustListResponse.pb.h"
#include "../pb/trade/response/futures/FuturesQueryHoldsListResponse.pb.h"
#include "../pb/trade/response/futures/FuturesQueryProductInfoResponse.pb.h"
#include "../pb/trade/response/futures/FuturesQueryRealDeliverListResponse.pb.h"
#include "../pb/trade/response/futures/FuturesQueryRealEntrustListResponse.pb.h"

#include "../pb/trade/vo/futures/FuturesFundInfoVo.pb.h"
#include "../pb/trade/vo/futures/FuturesHoldsVo.pb.h"
#include "../pb/trade/vo/futures/FuturesOrderVo.pb.h"
#include "../pb/trade/vo/futures/FuturesProductInfoVo.pb.h"


using namespace std;
struct stMacInfo
{
	string sMac;
	string sIp;
	bool bVirtual;
	int nType;
	stMacInfo() {
		bVirtual = false;
		nType = 0;
	}
};
class CTradeApi
{
public:
	CTradeApi();
	~CTradeApi();
public:
	static CTradeApi*	GetInstance()
	{
		if (m_instance == nullptr)
		{
			m_instance = new CTradeApi();
		}
		return m_instance;
	}
	static void Release()
	{
		if (m_instance)
		{
			delete m_instance;
			m_instance = nullptr;
		}
	}
private:
	static CTradeApi* m_instance;
//http
public:
	bool onLogin(string sMobile, string sPwd,bool bHK =false);
	void onSendVerifyCode(string sMobile);
	void onBindDevice(string sMobile, string sDeviceNo, string sVerifyCode);
	void onGetServerList();
	string onDecryptedMsgRSABase64(string strMsg);
	string onEncryptedMsgRSABase64(string strMsg);
	int m_nPakgeId;
//socket
private:
	CTcpClient m_tcpClient;
	bool m_bIsLogin;
	bool m_bTradeIsLogin;
	string m_sToken;
	string m_sVerifyCode;
	string m_sSecretKey;
	string m_sServerIp;
	string m_sGuid;
	char* m_recvBuf;
	long m_recvLen;
	long m_bodyLen;
	bool m_bSubPakage;
	//MsgHead m_head;
	int m_nPort;
	vector<stMacInfo> m_vecMac;
	vector<stMacInfo> _GetMacInfo();
	string _GetLoaclMacInfo();
public:
	string generGuid();
	void OnReadData(const BYTE* pData, int iLength);
	void InitSocket(string strHost, int nPort);
	bool isLogin() { return m_bIsLogin; };
	bool tradeIsLogin() { return m_bTradeIsLogin; };
	string getToken() { return m_sToken; };
	string getVerifyCode() { return m_sVerifyCode; };

	void HandleInitConnectResponse(const PBResponse &pbResp);
	bool HandleCommonBoolResponse(const PBResponse &pbResp);
	long HandleCommonIntResponse(const PBResponse &pbResp);
	string HandleCommonStringResponse(const PBResponse &pbResp);
	void HandleStockQueryHoldsListResponse(const PBResponse &pbResp);
	void HandleTradeQueryMarginFundInfoResponse(const PBResponse &pbResp);
	void HandleTradeQueryRealEntrustListResponse(const PBResponse &pbResp);
	void HandleTradeQueryHistoryEntrustListResponse(const PBResponse &pbResp);
	void HandleTradeQueryRealDeliverListResponse(const PBResponse &pbResp);
	void HandleTradeQueryHistoryDeliverListResponse(const PBResponse &pbResp);
	void HandleTradeQueryRealFundJourListResponse(const PBResponse &pbResp);
	void HandleTradeQueryHistoryFundJourListResponse(const PBResponse &pbResp);

	//---------------期货---------------------------------
	void HandleStockQueryHoldsListResponseFuture(const PBResponse &pbResp);
	void HandleTradeQueryMarginFundInfoResponseFuture(const PBResponse &pbResp);
	void HandleTradeQueryRealEntrustListResponseFuture(const PBResponse &pbResp);
	void HandleTradeQueryHistoryEntrustListResponseFuture(const PBResponse &pbResp);
	void HandleTradeQueryRealDeliverListResponseFuture(const PBResponse &pbResp);
	void HandleTradeQueryHistoryDeliverListResponseFuture(const PBResponse &pbResp);
	void HandleQueryBuySellAmountResponseFuture(const PBResponse &pbResp);
	

	void TradeCommonReq(const string& sBody);
	string WritePbReqMsg(google::protobuf::Message* pm, string sReqID, RequestMsgType nMsgType);
	string WritePbNotifyMsg(google::protobuf::Message* pm, string sNotifyID, NotifyMsgType nMsgType);
	void TradeInitConnect(InitConnectReq &req);//初始化链接接口
	void TradeLogin(TradeLoginRequest &req);//交易登录接口
	void TradeLogout();//交易登出接口
	void TradeEntrust(TradeEntrustRequest &req);//委托接口
	void TradeChangeEntrust(TradeChangeEntrustRequest &req);//委托接口
	void TradeCancelEntrust(TradeCancelEntrustRequest &req);//撤单接口
	void TradeQueryHoldsList(TradeQueryHoldsListRequest &req);//查询持仓股票列表
	void TradeQueryBuyAmount(TradeQueryBuyAmountRequest &req);//查询最大可买
	void TradeQuerySellAmount(TradeQuerySellAmountRequest &req);//查询最大可卖
	void TradeQueryMarginFundInfo(TradeQueryMarginFundInfoRequest &req);//查询客户资金信息
	void TradeQueryRealEntrustList(TradeQueryRealEntrustListRequest &req);//查询客户当日委托信息
	void TradeQueryHistoryEntrust(TradeQueryHistoryEntrustListRequest &req);//查询客户历史委托信息
	void TradeQueryRealDeliverList(TradeQueryRealDeliverListRequest &req);//查询客户当日成交信息
	void TradeQueryHistoryDeliverList(TradeQueryHistoryDeliverListRequest &req);//查询客户历史成交信息
	void TradeQueryRealFundJourList(TradeQueryRealFundJourListRequest &req);//查询客户当日资金流水
	void TradeQueryHistoryFundJourList(TradeQueryHistoryFundJourListRequest &req);//查询客户历史资金流水
	void TradeSubscribe();//订阅交易推送
	void TradeUnsubscribe();//取消订阅交易推送

	//--------------期货----------------------
	void TradeEntrustFuture(FuturesEntrustRequest &req);//委托接口
	void TradeChangeEntrustFuture(FuturesModifyEntrustRequest &req);//委托接口
	void TradeCancelEntrustFuture(FuturesCancelEntrustRequest &req);//撤单接口
	void TradeQueryHoldsListFuture(FuturesQueryHoldsListRequest &req);//查询持仓股票列表
	void TradeQueryBuySellAmountFuture(FuturesQueryMaxBuySellAmountRequest &req);//查询最大可买
	void TradeQueryMarginFundInfoFuture(FuturesQueryFundInfoRequest &req);//查询客户资金信息
	void TradeQueryRealEntrustListFuture(FuturesQueryRealEntrustListRequest &req);//查询客户当日委托信息
	void TradeQueryHistoryEntrustFuture(FuturesQueryHistoryEntrustListRequest &req);//查询客户历史委托信息
	void TradeQueryRealDeliverListFuture(FuturesQueryRealDeliverListRequest &req);//查询客户当日成交信息
	void TradeQueryHistoryDeliverListFuture(FuturesQueryHistoryDeliverListRequest &req);//查询客户历史成交信息

public:
	void testHeartBeat();
	void testTradeInitConnect();
	void testTradeLogin();
	void testTradeLogout();
	void testTradeEntrust();//委托接口
	void testTradeChangeEntrust();//改单接口
	void testTradeCancelEntrust();//撤单接口
	void testTradeQueryHoldsList();//查询持仓股票列表
	void testTradeQueryBuyAmount();//查询最大可买
	void testTradeQuerySellAmount();//查询最大可卖
	void testTradeQueryMarginFundInfo();//查询客户资金信息
	void testTradeQueryRealEntrustList();//查询客户当日委托信息
	void testTradeQueryHistoryEntrust();//查询客户历史委托信息
	void testTradeQueryRealDeliverList();//查询客户当日成交信息
	void testTradeQueryHistoryDeliverList();//查询客户历史成交信息
	void testTradeQueryRealFundJourList();//查询客户当日资金流水
	void testTradeQueryHistoryFundJourList();//查询客户历史资金流水
	void testTradeSubscribe();//订阅交易推送
	void testTradeUnsubscribe();//取消订阅交易推送

	//--------------期货----------------------
	void testTradeEntrustFuture();//委托接口
	void testTradeChangeEntrustFuture();//改单接口
	void testTradeCancelEntrustFuture();//撤单接口
	void testTradeQueryHoldsListFuture();//查询持仓股票列表
	void testTradeQueryBuySellAmountFuture();//查询最大可买
	void testTradeQueryMarginFundInfoFuture();//查询客户资金信息
	void testTradeQueryRealEntrustListFuture();//查询客户当日委托信息
	void testTradeQueryHistoryEntrustFuture();//查询客户历史委托信息
	void testTradeQueryRealDeliverListFuture();//查询客户当日成交信息
	void testTradeQueryHistoryDeliverListFuture();//查询客户历史成交信息

};

#define  TRADE_API  CTradeApi::GetInstance()

