#include <Windows.h>
#include <iostream>
#include <vector>

#include "testSpdQuoteAPI.h"

void testSpdQuoteAPI::MainLoop()
{
	//訂閱檔數上限20檔
    //LogonStarwave('IP', 34567, '身分證字號(大寫字母)', '電子登入密碼', True )
    //取消訂閱商品:UnsubscribeAll()
    LogonStarwave("IP", 34567, "Z323456789", "1234", true);

    while (!exit)
        Sleep(1000);

}

void testSpdQuoteAPI::OnStarwaveConnected()
{
    std::cout << "-------------- OnConnected() --------------" << std::endl;
}

void testSpdQuoteAPI::OnStarwaveDisconnected()
{
    std::cout << "-------------- OnDisConnected() --------------" << std::endl;
    Stop();
}

void testSpdQuoteAPI::OnOrderBook(const char* Exchange,
                 const char* Symbol, 
                 const char* MsgTime, 
                 spdMsgOrderBook* OrderBook)
{
    std::cout << "-------------- OnOrderBook() --------------" << std::endl;
    std::cout << "--- Exchange[" << Exchange << "]  Symbol[" << Symbol << "]  MsgTime[" << MsgTime << "]" << std::endl;
}

void testSpdQuoteAPI::OnTrade(const char* Exchange,
             const char* Symbol,
             const char* MatchTime,
             double MatchPrice,
             int MatchQty,
             bool isTestMatch)
{
    std::cout << "--------OnTrade---------------" << std::endl;
    std::cout << "---Exchange[" << Exchange << "] Symbol[" << Symbol << "]" << std::endl;
    std::cout << "---MatchTime=" << MatchTime << " MatchPrice = " << MatchPrice << " MatchQty = " << MatchQty << std::endl;
    std::cout << "---isTestMatch=" << isTestMatch << std::endl;
}

void testSpdQuoteAPI::OnStarwaveLogonResponse(bool isLoginOK, const char* ReplyMsg)
{
    std::cout << "-------------- OnLogonResponse() --------------" << std::endl;
    std::cout << "--- IsSucceed:" << isLoginOK << " ReplyString:" << ReplyMsg << std::endl;
}

void testSpdQuoteAPI::OnContract(const char* Exchange,
                const char* Symbol,
                const char* Name,
                const char* MaturityDate,
                const char* Category,
                spdContract* Contract)
{
    ContractInfo contract;
    std::string market = "unknown";
    if (Contract->Market == 0)
        market = "fut";
    else if (Contract->Market == 1)
        market = "opt";
    else if (Contract->Market == 2)
        market = "tse";
    else if (Contract->Market == 3)
        market = "otc";
    else if (Contract->Market == 4)
        market = "ffut";
    else if (Contract->Market == 5)
        market = "fopt";

    std::string day_trade = "No";
    if (Contract->DayTrade == 1)
        day_trade = "Yes";
    else if (Contract->DayTrade == 2)
        day_trade = "OnlyBuy";

    std::string callput = "None";
    if (Contract->CallPut == 'C')
        callput = "Call";
    else if (Contract->CallPut == 'P')
        callput = "Put";

    contract.Exchange = Exchange;
    contract.Symbol = Symbol;
    contract.DisplayName = Name;
    contract.MaturityDate = MaturityDate;
    contract.Category = Category;
    contract.BullPx = Contract->BullPx,
    contract.BearPx = Contract->BearPx,
    contract.RefPx = Contract->RefPx,
    contract.ContractMultiplier = Contract->ContractMultiplier,
    contract.StrikePx = Contract->StrikePx,
    contract.Market = market,
    contract.TradeUnit = Contract->TradeUnit,
    contract.DayTrade = day_trade,
    contract.WarningStock = Contract->WarningStock,
    contract.TradeFlag = Contract->TradeFlag,
    contract.IsWarrant = Contract->IsWarrant,
    contract.CallPut = callput;

    if (Contract->Market == 0)
        AddFuture(contract.Symbol.c_str(), &contract);
    else if (Contract->Market == 1)
        AddOption(contract.Symbol.c_str(), &contract);
    else if (Contract->Market == 2 || Contract->Market == 3)
        AddStock(contract.Symbol.c_str(), &contract);
}

void testSpdQuoteAPI::OnContractDownloadComplete(int TotalContracts)
{
    std::cout << "-------------- OnContractDownloadComplete(" << TotalContracts << ")--------------" << std::endl;
    std::cout << "已下載股票商品" << Stocks.size() << "筆" << std::endl;
    std::cout << "已下載期貨商品" << Futures.size() << "筆" << std::endl;
    std::cout << "已下載選擇權商品" << Options.size() << "筆" << std::endl;

    auto iter = Stocks.find("2330");
    if (iter != Stocks.end())
        Subscribe(iter->second.Exchange.c_str(), "2330");
    iter = Futures.find("MXFE4");
    if (iter != Futures.end())
        Subscribe(iter->second.Exchange.c_str(), "MXFE4");
    iter = Futures.find("TXFE4");
    if (iter != Futures.end())
        Subscribe(iter->second.Exchange.c_str(), "TXFE4");
}