from datetime import datetime

from Connect import XTSConnect
from MarketDataSocketClient import MDSocket_io

# MarketData API Credentials
"""Dealer credentials"""
API_KEY = "22f6f9dad2fe3982419756"
API_SECRET = "Bxtj027$Dr"
source = "WEBAPI"

"""Investor Client credentials"""
# API_KEY = "76179cbae91810ddda7774"
# API_SECRET = "Bylg203#fv"
# source = "WEBAPI"

# Initialise
xt = XTSConnect(API_KEY, API_SECRET, source)

# Login for authorization token
response = xt.marketdata_login()

# Store the token and userid
set_marketDataToken = response['result']['token']
set_muserID = response['result']['userID']
print("Login: ", response)

# Connecting to Marketdata socket
soc = MDSocket_io(set_marketDataToken, set_muserID)

# Instruments for subscribing
Instruments = [{'exchangeSegment': 1, 'exchangeInstrumentID': 2885},
               {'exchangeSegment': 1, 'exchangeInstrumentID': 22}]


# Callback for connection
def on_connect():
    """Connect from the socket."""
    print('Market Data Socket connected successfully!')

    # Subscribe to instruments
    print('Sending subscription request for Instruments - \n' + str(Instruments))
    response = xt.send_subscription(Instruments, 1501)
    print('Sent Subscription request!')
    print("Subscription response: ", response)


# Callback on receiving message
def on_message(data):
    print('I received a message!')


# Callback for message code 1502 FULL
def on_message1502_json_full(data):
    print('I received a 1502 Market depth message!' + data)


# Callback for message code 1504 FULL
def on_message1504_json_full(data):
    print('I received a 1504 Index data message!' + data)


# Callback for message code 1505 FULL
def on_message1505_json_full(data):
    print('I received a 1505 Candle data message!' + data)


# Callback for message code 1510 FULL
def on_message1510_json_full(data):
    print('I received a 1510 Open interest message!' + data)


# Callback for message code 1501 FULL
def on_message1501_json_full(data):
    print('I received a 1501 Level1,Touchline message!' + data)


# Callback for message code 1502 PARTIAL
def on_message1502_json_partial(data):
    print('I received a 1502 partial message!' + data)


# Callback for message code 1504 PARTIAL
def on_message1504_json_partial(data):
    print('I received a 1504 Index data message!' + data)


# Callback for message code 1505 PARTIAL
def on_message1505_json_partial(data):
    print('I received a 1505 Candle data message!' + data)


# Callback for message code 1510 PARTIAL
def on_message1510_json_partial(data):
    print('I received a 1510 Open interest message!' + data)


# Callback for message code 1501 PARTIAL
def on_message1501_json_partial(data):
    print('I received a 1501, Instrument Property Change Event message!' + data)


def on_message1105_json_partial(data):
    print('I received a 1105, Instrument Property Change Event message!' + data)


# Callback for disconnection
def on_disconnect():
    print('Market Data Socket disconnected!')


# Callback for error
def on_error(data):
    """Error from the socket."""
    print('Market Data Error', data)


# Assign the callbacks.
soc.on_connect = on_connect
soc.on_message = on_message
soc.on_message1502_json_full = on_message1502_json_full
soc.on_message1504_json_full = on_message1504_json_full
soc.on_message1505_json_full = on_message1505_json_full
soc.on_message1510_json_full = on_message1510_json_full
soc.on_message1501_json_full = on_message1501_json_full
soc.on_message1502_json_partial = on_message1502_json_partial
soc.on_message1504_json_partial = on_message1504_json_partial
soc.on_message1505_json_partial = on_message1505_json_partial
soc.on_message1510_json_partial = on_message1510_json_partial
soc.on_message1501_json_partial = on_message1501_json_partial
soc.on_message1105_json_partial = on_message1105_json_partial
soc.on_disconnect = on_disconnect
soc.on_error = on_error

# Event listener
el = soc.get_emitter()
el.on('connect', on_connect)
el.on('1501-json-full', on_message1501_json_full)
el.on('1502-json-full', on_message1502_json_full)
el.on('1105-json-partial', on_message1105_json_partial)

# Infinite loop on the main thread. Nothing after this will run.
# You have to use the pre-defined callbacks to manage subscriptions.
soc.connect()
