//Added by shreyasv 08-sep-2020
//SignalR for bonds/bpipe/news 
var notifUser;
var signalrConnection;
var notificationProxy;


$(function () {
    try {
        var status = sessionStorage.getItem("SignalRStatus");
        if (status == null || status !== "False") {
            notifUser = document.getElementById('ctl00_hdnFinIQUserID').value;
            if (typeof ($.hubConnection) !== "function") {
                console.log("Multiple jquery files detected. Stopping SignalR Connection.");
                return;
            }
            signalrConnection = $.hubConnection();
            //signalrConnection.logging = true;
            signalrConnection.qs = { 'User': notifUser, 'Group': 'Notification' };
            console.log("SignalR Connection Starting...");
            notificationProxy = signalrConnection.createHubProxy('MessageHub');
            signalrConnection.url = document.getElementById('ctl00_hdnSignalRBondsURL').value;
            notificationProxy.on('OnNotifications', function (message) {
                if (typeof WorkflowRefreshSignalR !== 'undefined') {
                    WorkflowRefreshSignalR(message);
                }
                getBondsNotifCount(document.getElementById('ctl00_hdnFinIQUserID').value);

            });

            notificationProxy.on('PublishEQPricestoSubsciber', function (message) {
                if (typeof BpipeStreamer !== 'undefined') {
                    BpipeStreamer(message);
                }
            });

            //added by shreyasv 20 jan 2021 DOR prices via SignalR start

            notificationProxy.on('PublishBondsPriceToSubscriber', function (message) {
                if (typeof BondsStreamer !== 'undefined') {
                    BondsStreamer(message);
                }
            });
            //added by shreyasv 20 jan 2021 DOR prices via SignalR end

            notificationProxy.on('PublishDORPricesToSubscriber', function (message) {
                if (typeof DORStreamer !== 'undefined') {
                    DORStreamer(message);
                }
            });

            notificationProxy.on('PublishNews', function (message) {
                NewsBroadcast(message);
            });

            signalrConnection.start({ jsonp: true, transport: ['webSockets', 'longPolling'] }).done(function () {
                console.log("SignalR Connection Done");
            }).fail(function (e) {
                //  console.error('Could not connect ' + e);
            });

            signalrConnection.connectionSlow(function () {
                console.log('We are currently experiencing difficulties with the signalR connection.')
            });

            signalrConnection.disconnected(function () {
                if (signalrConnection.lastError) {
                    sessionStorage.setItem("SignalRStatus", "False");
                    console.log("Disconnected. Reason: " + signalrConnection.lastError.message);
                }
                //setTimeout(function () {
                //    signalrConnection.start();
                //}, 5000); // Restarting connection after 5 seconds.
            });

            signalrConnection.reconnected(function () {
            });

        }
    }

    catch (ex)
    {
        console.log("Error: " + ex);
    }

});
