// TradeAPI.cpp : implementation file
//

#include "stdafx.h"
#include "TestWrapperAPI.h"
#include "TradeAPI.h"
#include "..\TCORE\TCTradeWrapperAPI\TCTradeWrapperAPI\TCTradeWrapperAPI_i.c"
#include <afxctl.h>
#include "CnopFutOptItem.h"

// TradeAPI

IMPLEMENT_DYNAMIC(TradeAPI, CCmdTarget)


TradeAPI::TradeAPI()
{
	EnableAutomation();
	m_pInterface = NULL;
	bConnect = false;
}

TradeAPI::~TradeAPI()
{
	if(m_pInterface)
	{
		if(!m_pInterface->m_bAutoRelease)
			m_pInterface->ReleaseDispatch();
		delete m_pInterface;
		m_pInterface = NULL;
	}
}


BEGIN_MESSAGE_MAP(TradeAPI, CCmdTarget)
END_MESSAGE_MAP()

BEGIN_DISPATCH_MAP(TradeAPI, CCmdTarget)
	DISP_FUNCTION_ID(TradeAPI,"OnAccountUpdate",	1,	OnAccountUpdate,	VT_EMPTY,	 VTS_I4 VTS_BSTR VTS_I4)
	DISP_FUNCTION_ID(TradeAPI,"OnCommandMsg",	2,	OnCommandMsg,	VT_EMPTY,	VTS_I4 VTS_I4 VTS_BSTR)
	DISP_FUNCTION_ID(TradeAPI,"OnExecutionReport",	3,	OnExecutionReport,	VT_EMPTY,	 VTS_BSTR)
	DISP_FUNCTION_ID(TradeAPI,"OnReportReset",	5,	OnReportReset,	VT_EMPTY,	 VTS_NONE)

END_DISPATCH_MAP()


BEGIN_INTERFACE_MAP(TradeAPI, CCmdTarget)
	INTERFACE_PART(TradeAPI, DIID__ICTCTradeAPIExEvents, Dispatch)
END_INTERFACE_MAP()

// TradeAPI message handlers
BOOL TradeAPI::Init(CTradeEventReceiver* pEventReceiver)
{
	::CoInitializeEx(NULL, 0);
	m_pEventReceiver = pEventReceiver;

	if(m_pInterface)
		return TRUE;

	m_pInterface=new CCTCTradeAPIEx;
	if(!m_pInterface)
		return FALSE;

	if(!m_pInterface->CreateDispatch(_T("TCore.TradeAPIEx.1")))
	{
		delete m_pInterface;
		m_pInterface=NULL;
		return FALSE;
	}
	m_dwCookie=0;
	AfxConnectionAdvise(*m_pInterface, DIID__ICTCTradeAPIExEvents, GetIDispatch(FALSE), FALSE, &m_dwCookie);

	return TRUE;
}

//跟Tcore连线
long TradeAPI::Connect(LPCTSTR HostAddress, LPCTSTR SystemName, LPCTSTR AppID, long ConnectType)
{
	if(m_pInterface)
	{
		bConnect = true;
		m_pInterface->SetAccountSubscriptionLevel(3);
		return m_pInterface->Connect(HostAddress, SystemName, AppID, ConnectType);
	}
	return -1;
}


//断线
void TradeAPI::Disconnect()
{
	if(m_pInterface)
	{
		bConnect = false;
		m_pInterface->Disconnect();
	}
}

//连线状态
void TradeAPI::OnCommandMsg(long MsgType, long MsgCode, LPCTSTR MsgString)
{
	if (m_pEventReceiver)
	{
		m_pEventReceiver->OnTradeCommandMsg(MsgType,MsgCode,MsgString);
	}
}

//帐号与帐务更新
void TradeAPI::OnAccountUpdate(long Type, LPCTSTR AcctMask, long Count)
{
	if (m_pEventReceiver)
	{
		m_pEventReceiver->OnTradeAccountUpdate(Type,AcctMask,Count);
	}
}

//取帐号资料
long TradeAPI::GetAccountData(long Type, long Index, LPCTSTR AcctMask, LPDISPATCH AccountDataItem)
{
	if (m_pInterface)
		return m_pInterface->GetAccountData(Type, Index, AcctMask, AccountDataItem);
	return 0;
}

//下单
long TradeAPI::DoPlaceOrder(ORDER_INFO pOrderInfo)
{
	CnopFutOptItem dspItem;
	dspItem.CreateDispatch(_T("TCore.TradeNewOrderParametersFutOpt.1"));

	dspItem.put_Symbol(pOrderInfo.strSymbol);
	dspItem.put_Account(pOrderInfo.strAccount);
	dspItem.put_BrokerID(pOrderInfo.strBrokerID);
	dspItem.put_Side(_ttol(pOrderInfo.strBuySell));
	dspItem.put_TimeInForce(_ttol(pOrderInfo.strTimeInForce));
	dspItem.put_PositionEffect(_ttol(pOrderInfo.strPositionEffect));
	dspItem.put_OrderType(_ttol(pOrderInfo.strOrderType));
	dspItem.put_OrderQty(_ttol(pOrderInfo.strQty));
	dspItem.put_Price(pOrderInfo.strPrice);
	dspItem.put_UserKey1(_T("Wrapper test"));
	dspItem.put_Synthetic(0);

	if (m_pInterface)
		return m_pInterface->NewOrder2(SECURITY_TYPE_SYMBOL_FUTURE,dspItem.DetachDispatch());
		
	return 0;
}

//取icm设定
CString TradeAPI::GetInstrumentInfo(LPCTSTR bstrType, LPCTSTR bstrSymbolID)
{
	if (m_pInterface)
		return m_pInterface->GetInstrumentInfo(bstrType, bstrSymbolID);
	return _T("");
}

//回报清空回补
void TradeAPI::OnReportReset()
{
	if (m_pEventReceiver && bConnect)
	{
		m_pEventReceiver->OnTradeReportReset();
	}
}

//即时回报
void TradeAPI::OnExecutionReport(LPCTSTR ReportID)
{
	if (m_pEventReceiver && bConnect)
	{
		m_pEventReceiver->OnTradeExecutionReport(ReportID);
	}
}

//取回报资料
long TradeAPI::GetReportData(long ReportType, LPCTSTR ReportID, LPDISPATCH ReportItems)
{
	if (m_pInterface)
		return m_pInterface->GetReportData(ReportType, ReportID, ReportItems);
	return 0;
}

//帐务资料查询
void TradeAPI::DoQueryAccountData(long lType, LPCTSTR BrokerID, LPCTSTR Account)
{
	if (m_pInterface)
		m_pInterface->QueryAccountData(lType,BrokerID,Account);
}
