#pragma once
#include <string>
//------------------------------------------------------------------------------------------------------------------------
// struct spdContract
//------------------------------------------------------------------------------------------------------------------------
typedef struct _spdContract
{
	double  BullPx;
	double  BearPx;
	double  RefPx;
	double  ContractMultiplier;
	double  StrikePx;
	int     Market;
	int     TradeUnit;
	int     DayTrade;
	int     WarningStock;
	bool    TradeFlag;
	bool    IsWarrant;
	char    CallPut;

} spdContract;
//------------------------------------------------------------------------------------------------------------------------
// struct spdMsgOrderBook
//------------------------------------------------------------------------------------------------------------------------
typedef struct _spdMsgOrderBook
{
	double BidPrice1;
	double BidPrice2;
	double BidPrice3;
	double BidPrice4;
	double BidPrice5;
	int    BidQty1;
	int    BidQty2;
	int    BidQty3;
	int    BidQty4;
	int    BidQty5;
	double AskPrice1;
	double AskPrice2;
	double AskPrice3;
	double AskPrice4;
	double AskPrice5;
	int    AskQty1;
	int    AskQty2;
	int    AskQty3;
	int    AskQty4;
	int    AskQty5;
	double DerivedBidPrice;
	int    DerivedBidQty;
	double DerivedAskPrice;
	int    DerivedAskQty;
	bool   IsTestMatch;
} spdMsgOrderBook;
//------------------------------------------------------------------------------------------------------------------------

typedef void(__cdecl* TpyQuoteOnConnected)(void* user_defined);
typedef void(__cdecl* TpyQuoteOnDisconnected)(void* user_defined);
typedef void(__cdecl* TpyQuoteOnOrderBook)(void* user_defined, const char*, const char*, const char*, spdMsgOrderBook*);
typedef void(__cdecl* TpyQuoteOnTrade)(void* user_defined, const char*, const char*, const char*, double, int, bool);
typedef void(__cdecl* TpyQuoteOnLogonResponse)(void* user_defined, bool, const char*);
typedef void(__cdecl* TpyQuoteOnContract)(void* user_defined, const char*, const char*, const char*, const char*, const char*, spdContract*);
typedef void(__cdecl* TpyQuoteOnContractDownloadComplete)(void* user_defined, int);

typedef void* (*fptrPyQuoteAdapter_New)();
typedef void (*fptrPyQuoteAdapter_Delete)(void*);
typedef bool (*fptrPyQuoteAdapter_IsConnected)(void*);
typedef bool (*fptrPyQuoteAdapter_Logon)(void*, const char*, int, const char*, const char*, bool);
typedef bool (*fptrPyQuoteAdapter_Subscribe)(void*, const char*, const char*);
typedef void (*fptrPyQuoteAdapter_UnsubscribeAll)(void*);
typedef void (*fptrPyQuoteAdapter_DownloadContracts)(void*);
typedef void (*fptrPyQuoteAdapter_RegEvents)(void*, void*, TpyQuoteOnConnected, TpyQuoteOnDisconnected, TpyQuoteOnLogonResponse, TpyQuoteOnContract, TpyQuoteOnContractDownloadComplete, TpyQuoteOnOrderBook, TpyQuoteOnTrade);
typedef const char* (*fptrPyQuoteAdapter_GetLastErrorMessage)(void*);

class spdQuoteAPI
{
private:
	void* quoteAPI;

private:
	static bool LoadQuoteAPIFromDLL();
	static void OnQuoteConnected(void* user_defined);
	static void OnQuoteDisconnected(void* user_defined);
	static void OnQuoteOrderBook(void* user_defined, const char* Exchange, const char* Symbol, const char* MsgTime, spdMsgOrderBook* OrderBook);
	static void OnQuoteTrade(void* user_defined, const char* Exchange, const char* Symbol, const char* MatchTime, double MatchPrice, int MatchQty, bool isTestMatch);
	static void OnQuoteLogonResponse(void* user_defined, bool isLoginOK, const char* ReplyMsg);
	static void OnQuoteContract(void* user_defined, const char* Exchange, const char* Symbol, const char* Name, const char* MaturityDate, const char* Category, spdContract* Contract);
	static void OnQuoteContractDownloadComplete(void* user_defined, int TotalContracts);

protected:
	virtual void OnStarwaveConnected() {}
	virtual void OnStarwaveDisconnected() {}
	virtual void OnOrderBook(const char* Exchange, const char* Symbol, const char* MsgTime, spdMsgOrderBook* OrderBook) {}
	virtual void OnTrade(const char* Exchange, const char* Symbol, const char* MatchTime, double MatchPrice, int MatchQty, bool isTestMatch) {}
	virtual void OnStarwaveLogonResponse(bool isLoginOK, const char* ReplyMsg) {}
	virtual void OnContract(const char* Exchange, const char* Symbol, const char* Name, const char* MaturityDate, const char* Category, spdContract* Contract) {}
	virtual void OnContractDownloadComplete(int TotalContracts) {}

public:
	spdQuoteAPI();
	virtual ~spdQuoteAPI();

	bool IsStarwaveConnected(void);
	bool LogonStarwave(const char* IP, int Port, const char* ID, const char* Password, bool DownloadContracts);
	bool Subscribe(const char* Exchange, const char* Symbol);
	void UnsubscribeAll();
	void DownloadContracts(void);
	std::string GetQuoteAPILastErrorMessage();

};
