// QuoteAPI.cpp : implementation file
//

#include "stdafx.h"
#include "TestWrapperAPI.h"
#include "QuoteAPI.h"
#include "..\TCORE\TCQuoteWrapperAPI\TCQuoteWrapperAPI\TCQuoteWrapperAPI_i.c"
#include <afxctl.h>
#include "CpDataParameters.h"
// CQuoteAPI

IMPLEMENT_DYNAMIC(CQuoteAPI, CCmdTarget)


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

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


BEGIN_MESSAGE_MAP(CQuoteAPI, CCmdTarget)
END_MESSAGE_MAP()

BEGIN_DISPATCH_MAP(CQuoteAPI, CCmdTarget)
	DISP_FUNCTION_ID(CQuoteAPI,"OnCommandMsg",	1,	OnCommandMsg,	VT_EMPTY,	 VTS_I4 VTS_I4 VTS_BSTR)
	DISP_FUNCTION_ID(CQuoteAPI,"OnQuoteData",	2,	OnQuoteData,	VT_EMPTY,	VTS_I4 VTS_I4 VTS_DISPATCH)
	DISP_FUNCTION_ID(CQuoteAPI,"OnSymbolClassificationsUpdate",	4,	OnSymbolClassificationsUpdate,	VT_EMPTY,	 VTS_NONE)
	DISP_FUNCTION_ID(CQuoteAPI,"OnQuoteDataSyn",	13,	OnQuoteDataSyn,	VT_EMPTY,	VTS_I4 VTS_I4 VTS_DISPATCH VTS_BSTR)
END_DISPATCH_MAP()


BEGIN_INTERFACE_MAP(CQuoteAPI, CCmdTarget)
	INTERFACE_PART(CQuoteAPI, DIID__ITCQuoteAPIExEvents, Dispatch)
END_INTERFACE_MAP()

// CQuoteAPI message handlers

//初始化
BOOL CQuoteAPI::Init(CQuoteEventReceiver* pEventReceiver)
{
	::CoInitializeEx(NULL, 0);
	m_pEventReceiver = pEventReceiver;
	
	if(m_pInterface)
		return TRUE;

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

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

	return TRUE;
}

void CQuoteAPI::UnAdvise()
{
	if(m_pInterface)
		AfxConnectionUnadvise(*m_pInterface, DIID__ITCQuoteAPIExEvents, GetIDispatch(FALSE), FALSE, m_dwCookie);
	m_dwCookie=0;
}

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

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

//成功连线后会回传
void CQuoteAPI::OnCommandMsg(long MsgType, long MsgCode, LPCTSTR MsgString)
{
	TRACE(_T("type=[%d] code=[%d] Msg=[%s]"),MsgType,MsgCode,MsgString);
	if (m_pEventReceiver)
	{
		m_pEventReceiver->OnQuoteCommandMsg(MsgType,MsgCode,MsgString);
	}
}

//报价资料
void CQuoteAPI::OnQuoteData(long lSymbolType, long lDataType, LPDISPATCH lpDispData)
{
	if (m_pEventReceiver)
	{
		m_pEventReceiver->OnQuoteData(lSymbolType,lDataType,lpDispData);
	}
}

//报价资料(不并tick)
void CQuoteAPI::OnQuoteDataSyn(long lSymbolType, long lDataType, LPDISPATCH lpDispData, LPCTSTR lpModifiedFields)
{
	if (m_pEventReceiver)
	{
		m_pEventReceiver->OnQuoteDataSyn(lSymbolType,lDataType,lpDispData,lpModifiedFields);
	}
}

//订阅报价
void CQuoteAPI::DoSub(stcSubQuote SubQuote, long nType)
{
	if(m_pInterface && bConnect)
	{
		CpDataParameters objSubInfo;
		if (!objSubInfo.CreateDispatch(_T("TCore.QuoteDataParameters.1")))
		{
			AfxMessageBox(_T("DoSub error"));
			return ;
		}

		objSubInfo.put_Symbol(SubQuote.strSymbol);
		objSubInfo.put_SecurityType(9);
		if (nType != DATA_TYPE_REALTIME)
		{
			objSubInfo.put_StartTime(SubQuote.nStartTime);
			objSubInfo.put_EndTime(SubQuote.nEndTime);
		}
		
		 CString strResult = m_pInterface->SubQuote(nType,objSubInfo.m_lpDispatch);
	}
}

//解订报价
void CQuoteAPI::DoUnSub(stcSubQuote SubQuote, long nType)
{
	if(m_pInterface && bConnect)
	{
		CpDataParameters objSubInfo;
		if (!objSubInfo.CreateDispatch(_T("TCore.QuoteDataParameters.1")))
		{
			AfxMessageBox(_T("DoUnSub error"));
			return ;
		}
		objSubInfo.put_Symbol(SubQuote.strSymbol);
		objSubInfo.put_SecurityType(9);
		if (nType != DATA_TYPE_REALTIME)
		{
			objSubInfo.put_StartTime(SubQuote.nStartTime);
			objSubInfo.put_EndTime(SubQuote.nEndTime);
		}
		CString strResult = m_pInterface->UnsubQuote(nType,objSubInfo.m_lpDispatch);
	}
}

//设定报价资料是否不并tick
void CQuoteAPI::SetSubscriptionLevel(long lLevel)
{
	if(m_pInterface && bConnect)
		m_pInterface->SetSubscriptionLevel(lLevel);
}

//商品分类表更新
void CQuoteAPI::OnSymbolClassificationsUpdate()
{
	if(m_pEventReceiver)
		m_pEventReceiver->OnQuoteSymbolClassificationsUpdate();
}

//取得商品分类表，若超过五层则第五个参数使用"."分隔，ex:GetSymbolClassifications(a,b,c,d,e.f.g)
CString CQuoteAPI::GetSymbolClassifications(LPCTSTR Classify, LPCTSTR Exchange, LPCTSTR Symbol, LPCTSTR Month, LPCTSTR CallPut)
{
	if(m_pInterface && bConnect)
		return m_pInterface->GetSymbolClassifications(Classify, Exchange, Symbol, Month, CallPut);
	return _T("ERROR");
}
