// RTCTradeAPIDemo.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include "RTCTradeAPI.h"

using namespace std;

class TradeAPI : public RTCTradeAPISpi
{
	RTCTradeAPI *m_pTrade;
	int m_iRequest;

public:
	void Go()
	{
		char strHost[] = ".";
		char strstrSystemName[] = "";
		char strServiceKey[] = "";

		m_iRequest = 0;
		m_pTrade = RTCTradeAPI::CreateRTCTradeAPI();
		m_pTrade->RegisterSpi(this);
		m_pTrade->RegisterFront(strHost, strstrSystemName, strServiceKey, 1);
		m_pTrade->Join();
	}

	void OnFrontConnected()
	{
		cout << "OnFrontConnected" << endl;
	}

	void OnRspUserLogin(ICERspUserLoginField *pRspUserLogin, ICERspInfoField *pRspInfo, int nRequestID, bool bIsLast)
	{
		if (pRspUserLogin)
			cout << "OnRspUserLogin() BrokerID:" << pRspUserLogin->BrokerID << ",UserID:" << pRspUserLogin->UserID << ",SystemName:" << pRspUserLogin->SystemName << endl;

		if (bIsLast)
		{
			ICEQryTradingAccountField field = { 0 };

			m_pTrade->ReqQryTradingAccount(&field, m_iRequest++);
		}
	}

	void OnRspUserLogout(ICEUserLogoutField *pUserLogout, ICERspInfoField *pRspInfo, int nRequestID, bool bIsLast)
	{
		if (pUserLogout)
			cout << "OnRspUserLogout() BrokerID:" << pUserLogout->BrokerID << ",UserID:" << pUserLogout->UserID << endl;
	}

	void OnRspQryTradingAccount(ICETradingAccountField *pTradingAccount, ICERspInfoField *pRspInfo, int nRequestID, bool bIsLast)
	{
		if (pTradingAccount)
			cout << "OnRspQryTradingAccount() AccountID:" << pTradingAccount->AccountID << ",Available:" << pTradingAccount->Available << endl;

		if (bIsLast)
		{
			ICEQryInvestorPositionField field = {0};

			m_pTrade->ReqQryInvestorPosition(&field, m_iRequest++);
		}
	}

	void OnRspQryInvestorPosition(ICEInvestorPositionField *pInvestorPosition, ICERspInfoField *pRspInfo, int nRequestID, bool bIsLast)
	{
		if (pInvestorPosition)
			cout << "OnRspQryInvestorPosition() InstrumentID:" << pInvestorPosition->InstrumentID << ",Position:" << pInvestorPosition->Position << endl;

		if (bIsLast)
		{
			ICEQryOrderField field = {0};

			m_pTrade->ReqQryOrder(&field, m_iRequest++);
		}
	}

	void OnRspQryOrder(ICEOrderField *pOrder, ICERspInfoField *pRspInfo, int nRequestID, bool bIsLast)
	{
		if (pOrder)
			cout << "OnRspQryOrder() OrderSysID:" << pOrder->OrderSysID << ",InstrumentID:" << pOrder->InstrumentID << endl;

		if (bIsLast)
		{
			ICEQryTradeField field = { 0 };

			m_pTrade->ReqQryTrade(&field, m_iRequest++);
		}
	}

	void OnRspQryTrade(ICETradeField *pTrade, ICERspInfoField *pRspInfo, int nRequestID, bool bIsLast)
	{
		if (pTrade)
			cout << "OnRspQryTrade() OrderSysID:" << pTrade->OrderSysID << ",InstrumentID:" << pTrade->InstrumentID << endl;
	}

	void NewOrder()
	{
		ICEInputOrderField field = { 0 };

		strcpy(field.BrokerID, "");
		strcpy(field.InvestorID, "");
		strcpy(field.InstrumentID, "TC.O.SSE.510050.201912.C.2.95");
		strcpy(field.OrderRef, "123");
		field.VolumeTotalOriginal = 1;
		field.OrderPriceType = ICE_OPT_AnyPrice;
		field.TimeCondition = ICE_TC_IOC;
		field.VolumeCondition = ICE_VC_AV;
		field.LimitPrice = 0;
		field.IsFitOrderFreq = 1;
		m_pTrade->ReqOrderInsert(&field, 1);
	}
};

int _tmain(int argc, _TCHAR* argv[])
{
	TradeAPI test;

	test.Go();
	return 0;
}