// quantOpenApiDemo.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include "./NetMgr/SSLAgent.h"
#include <iostream>
#include "json/json.h"
#include "crypto/md5.h"
#include "comdef.h"
#include "./crypto/CryptoTool.h"
#include "./NetMgr/TcpClient.h"
#include "./TradeApi/TradeApi.h"
#include "Common.h"
#include "./NetMgr/MsgHead.h"
void testRSA()
{
	string sMessage = "hello world";
	//string sFilePub = "pub";
	//string sFilePri = "pri";
	string sFilePub = "pub.txt";
	string sFilePri = "private.txt";
	string sSeed= "seed";
	//CCryptoTool::GenerateRSAKey(1024, sFilePri.c_str(), sFilePub.c_str(), sSeed.c_str());
	cout << "Origin Text:\t" << sMessage << endl << endl;
	string encryptedText = CCryptoTool::RSAEncryptString(sFilePub.c_str(), sMessage.c_str());  
	cout << "Encrypted Text:\t" << encryptedText << endl << endl;
	string decryptedText = CCryptoTool::RSADecryptString(sFilePri.c_str(), encryptedText.c_str()); 
	cout << "Decrypted Text:\t" << decryptedText << endl << endl;
}
void testAes()
{
	string sMessage = "12345678901234561234567890123456";
	string sKey = "1234567890123456";
	cout << "Origin Text:\t" << sMessage << endl << endl;
	string sOut = CCryptoTool::encryptAES(sMessage, sKey);
	cout << "Encrypted Text:\t" << sOut << endl << endl;
	string decryptedText = CCryptoTool::decryptAES(sOut.c_str(),sOut.length(),sKey);
	cout << "Decrypted Text:\t" << decryptedText << endl << endl;

}
void PrintCmd()
{
	cout << "======帮助菜单=====" << endl;
	cout << "h:菜单" << endl;
	cout << "q:退出" << endl;
	//cout << "t:test" << endl;
	cout << "1:login on https(HTTP登录)" << endl;
	cout << "2:InitConnect(初始化链接)" << endl;
	cout << "3:TradeLogin(交易登录)" << endl;
	cout << "4:TradeLogout(交易登出)" << endl;
	cout << "5:TradeEntrust(委托下单)" << endl;
	cout << "6:TradeCancelEntrust(委托撤单)" << endl;
	cout << "7:TradeQueryHoldsList(查询持仓股票列表)" << endl;
	cout << "8:TradeQueryBuyAmount(查询最大可买)" << endl;
	cout << "9:TradeQuerySellAmount(查询最大可卖)" << endl;
	cout << "10:TradeQueryMarginFundInfo(查询客户资金信息)" << endl;
	cout << "11:TradeQueryRealEntrustList(查询客户当日委托信息)" << endl;
	cout << "12:TradeQueryHistoryEntrust(查询客户历史委托信息)" << endl;
	cout << "13:TradeQueryRealDeliverList(查询客户当日成交信息)" << endl;
	cout << "14:TradeQueryHistoryDeliverList(查询客户历史成交信息)" << endl;
	cout << "15:TradeQueryRealFundJourList(查询客户当日资金流水)" << endl;
	cout << "16:TradeQueryHistoryFundJourList(查询客户历史资金流水)" << endl;
	cout << "17:TradeSubscribe(订阅交易推送)" << endl;
	cout << "18:TradeUnsubscribe(取消订阅交易推送)" << endl;
	cout << "19:测试心跳" << endl;
	cout << "20:TradeChangeEntrust(改单)" << endl;
	cout << "21:FuturesEntrustRequest(期货委托下单)" << endl;
	cout << "22:FuturesModifyEntrustRequest(期货改单)" << endl;
	cout << "23:FuturesCancelEntrustRequest(期货委托撤单)" << endl;
	cout << "24:FuturesQueryHoldsListRequest(期货查询持仓股票列表)" << endl;
	cout << "25:FuturesQueryMaxBuySellAmountRequest(期货查询最大可买可卖)" << endl;
	cout << "26:FuturesQueryFundInfoRequest(期货查询客户资金信息)" << endl;
	cout << "27:FuturesQueryRealEntrustListRequest(期货查询客户当日委托信息)" << endl;
	cout << "28:FuturesQueryHistoryEntrustListRequest(期货查询客户历史委托信息)" << endl;
	cout << "29:FuturesQueryRealDeliverListRequest(期货查询客户当日成交信息)" << endl;
	cout << "30:FuturesQueryHistoryDeliverListRequest(期货查询客户历史成交信息)" << endl;


}
void test1()
{
	string message = "hello world";
	//message = CryptoPP::sha1(message);
	//std::string result;
	CryptoPP::FileSource privFile(PRIVATE_FILE, true, new CryptoPP::Base64Decoder);
	//RSASS<PKCS1v15, SHA1>::Signer priv(privFile);
	CryptoPP::RSASSA_PKCS1v15_SHA_Signer signer(privFile);
	size_t length = signer.MaxSignatureLength();
	CryptoPP::SecByteBlock signature(length);
	string strSeed = "seed";
	CryptoPP::RandomPool randPool;
	randPool.IncorporateEntropy((unsigned char *)strSeed.c_str(), 0);
	size_t szlength =signer.SignMessage(randPool, (const byte*)message.c_str(), message.length(), signature);
	signature.resize(szlength);
	cout << "signature data" << endl;
	cout << signature << endl;

	CryptoPP::FileSource pubFile(PUB_FILE, true, new CryptoPP::Base64Decoder);
	CryptoPP::RSASSA_PKCS1v15_SHA_Verifier verifier(pubFile);

	// Verify
	bool bRet = verifier.VerifyMessage((const byte*)message.c_str(),
		message.length(), signature, signature.size());

	// Result
	if (true == bRet) {
		cout << "Signature on message verified" << endl;
	}
	else {
		cout << "Message verification failed" << endl;
	}
	cout << "==========" << endl;
	string sOut=CCryptoTool::RSASignString(PRIVATE_FILE, message);
	bRet = CCryptoTool::RSASignVerifyString(PUB_FILE, message, sOut);
	if (true == bRet) {
		cout << "Signature on message verified" << endl;
	}
	else {
		cout << "Message verification failed" << endl;
	}


}
int _tmain(int argc, _TCHAR* argv[])
{
	setlocale(LC_ALL, "chs");
	PrintCmd();
	while (1)
	{
		string sCmd;
		cin >> sCmd;
		if (sCmd == "h")
		{
			PrintCmd();
		}
		if (sCmd == "q")
		{
			exit(0);
		}
		//if (sCmd == "t")
		//{
		//	testAes();
		//	//test();
		//	//test1();
		//}
		if (sCmd == "1")
		{
			TRADE_API->onLogin(USER_MOBILE,USER_PWD,true);
			if (TRADE_API->isLogin())
			{
				TRADE_API->onGetServerList();
			}
		}
		if (sCmd == "2")
		{
			if (TRADE_API->isLogin())
			{
				TRADE_API->testTradeInitConnect();
			}
		}
		if (sCmd == "3")
		{
			TRADE_API->testTradeLogin();
		}
		if (sCmd == "4")
		{
			TRADE_API->testTradeLogout();
		}
		if (sCmd == "5")
		{
			TRADE_API->testTradeEntrust();
		}
		if (sCmd == "6")
		{
			TRADE_API->testTradeCancelEntrust();
		}
		if (sCmd == "7")
		{
			TRADE_API->testTradeQueryHoldsList();
		}
		if (sCmd == "8")
		{
			TRADE_API->testTradeQueryBuyAmount();
		}
		if (sCmd == "9")
		{
			TRADE_API->testTradeQuerySellAmount();
		}
		if (sCmd == "10")
		{
			TRADE_API->testTradeQueryMarginFundInfo();
		}
		if (sCmd == "11")
		{
			TRADE_API->testTradeQueryRealEntrustList();
		}
		if (sCmd == "12")
		{
			TRADE_API->testTradeQueryHistoryEntrust();
		}
		if (sCmd == "13")
		{
			TRADE_API->testTradeQueryRealDeliverList();
		}
		if (sCmd == "14")
		{
			TRADE_API->testTradeQueryHistoryDeliverList();
		}
		if (sCmd == "15")
		{
			TRADE_API->testTradeQueryRealFundJourList();
		}
		if (sCmd == "16")
		{
			TRADE_API->testTradeQueryHistoryFundJourList();
		}
		if (sCmd == "17")
		{
			TRADE_API->testTradeSubscribe();
		}
		if (sCmd == "18")
		{
			TRADE_API->testTradeUnsubscribe();
		}
		if (sCmd == "19")
		{
			TRADE_API->testHeartBeat();
		}
		if (sCmd == "20")
		{
			TRADE_API->testTradeChangeEntrust();
		}
		////////////////期货API
		if (sCmd == "21")
		{
			TRADE_API->testTradeEntrustFuture();
		}
		if (sCmd == "22")
		{
			TRADE_API->testTradeChangeEntrustFuture();
		}
		if (sCmd == "23")
		{
			TRADE_API->testTradeCancelEntrustFuture();
		}
		if (sCmd == "24")
		{
			TRADE_API->testTradeQueryHoldsListFuture();
		}
		if (sCmd == "25")
		{
			TRADE_API->testTradeQueryBuySellAmountFuture();
		}
		if (sCmd == "26")
		{
			TRADE_API->testTradeQueryMarginFundInfoFuture();
		}
		if (sCmd == "27")
		{
			TRADE_API->testTradeQueryRealEntrustListFuture();
		}
		if (sCmd == "28")
		{
			TRADE_API->testTradeQueryHistoryEntrustFuture();
		}
		if (sCmd == "29")
		{
			TRADE_API->testTradeQueryRealDeliverListFuture();
		}
		if (sCmd == "30")
		{
			TRADE_API->testTradeQueryHistoryDeliverListFuture();
		}

		Sleep(1000);
	}
	return 0;
}

