/*!
 * ASP.NET SignalR JavaScript Library 2.4.3
 * http://signalr.net/
 *
 * Copyright (c) .NET Foundation. All rights reserved.
 * Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
 *
 */

/// <reference path="..\..\SignalR.Client.JS\Scripts\jquery-1.6.4.js" />
/// <reference path="jquery.signalR.js" />
(function ($, window, undefined) {
    /// <param name="$" type="jQuery" />
    "use strict";

    if (typeof ($.signalR) !== "function") {
        throw new Error("SignalR: SignalR is not loaded. Please ensure jquery.signalR-x.js is referenced before ~/signalr/js.");
    }

    var signalR = $.signalR;

    function makeProxyCallback(hub, callback) {
        return function () {
            // Call the client hub method
            callback.apply(hub, $.makeArray(arguments));
        };
    }

    function registerHubProxies(instance, shouldSubscribe) {
        var key, hub, memberKey, memberValue, subscriptionMethod;

        for (key in instance) {
            if (instance.hasOwnProperty(key)) {
                hub = instance[key];

                if (!(hub.hubName)) {
                    // Not a client hub
                    continue;
                }

                if (shouldSubscribe) {
                    // We want to subscribe to the hub events
                    subscriptionMethod = hub.on;
                } else {
                    // We want to unsubscribe from the hub events
                    subscriptionMethod = hub.off;
                }

                // Loop through all members on the hub and find client hub functions to subscribe/unsubscribe
                for (memberKey in hub.client) {
                    if (hub.client.hasOwnProperty(memberKey)) {
                        memberValue = hub.client[memberKey];

                        if (!$.isFunction(memberValue)) {
                            // Not a client hub function
                            continue;
                        }

                        // Use the actual user-provided callback as the "identity" value for the registration.
                        subscriptionMethod.call(hub, memberKey, makeProxyCallback(hub, memberValue), memberValue);
                    }
                }
            }
        }
    }

    $.hubConnection.prototype.createHubProxies = function () {
        var proxies = {};
        this.starting(function () {
            // Register the hub proxies as subscribed
            // (instance, shouldSubscribe)
            registerHubProxies(proxies, true);

            this._registerSubscribedHubs();
        }).disconnected(function () {
            // Unsubscribe all hub proxies when we "disconnect".  This is to ensure that we do not re-add functional call backs.
            // (instance, shouldSubscribe)
            registerHubProxies(proxies, false);
        });

        proxies['activityHub'] = this.createHubProxy('activityHub'); 
        proxies['activityHub'].client = { };
        proxies['activityHub'].server = {
            enterChannel: function (channelId) {
                return proxies['activityHub'].invoke.apply(proxies['activityHub'], $.merge(["EnterChannel"], $.makeArray(arguments)));
             },

            enterQuestion: function (questionId) {
                return proxies['activityHub'].invoke.apply(proxies['activityHub'], $.merge(["EnterQuestion"], $.makeArray(arguments)));
             },

            enterSurvey: function (surveyId) {
                return proxies['activityHub'].invoke.apply(proxies['activityHub'], $.merge(["EnterSurvey"], $.makeArray(arguments)));
             },

            invalidateSurveySchedule: function (surveyId, channelId) {
                return proxies['activityHub'].invoke.apply(proxies['activityHub'], $.merge(["InvalidateSurveySchedule"], $.makeArray(arguments)));
             },

            leaveChannel: function (channelId) {
                return proxies['activityHub'].invoke.apply(proxies['activityHub'], $.merge(["LeaveChannel"], $.makeArray(arguments)));
             },

            leaveQuestion: function (questionId) {
                return proxies['activityHub'].invoke.apply(proxies['activityHub'], $.merge(["LeaveQuestion"], $.makeArray(arguments)));
             },

            leaveSurvey: function (surveyId) {
                return proxies['activityHub'].invoke.apply(proxies['activityHub'], $.merge(["LeaveSurvey"], $.makeArray(arguments)));
             }
        };

        proxies['airtimeStatsHub'] = this.createHubProxy('airtimeStatsHub'); 
        proxies['airtimeStatsHub'].client = { };
        proxies['airtimeStatsHub'].server = {
            broadcastStats: function () {
                return proxies['airtimeStatsHub'].invoke.apply(proxies['airtimeStatsHub'], $.merge(["BroadcastStats"], $.makeArray(arguments)));
             },

            requestStats: function () {
                return proxies['airtimeStatsHub'].invoke.apply(proxies['airtimeStatsHub'], $.merge(["RequestStats"], $.makeArray(arguments)));
             }
        };

        proxies['managementHub'] = this.createHubProxy('managementHub'); 
        proxies['managementHub'].client = { };
        proxies['managementHub'].server = {
            monitorTranscoderEvents: function () {
                return proxies['managementHub'].invoke.apply(proxies['managementHub'], $.merge(["MonitorTranscoderEvents"], $.makeArray(arguments)));
             },

            raisedTranscoderEvent: function (eventType, eventData) {
                return proxies['managementHub'].invoke.apply(proxies['managementHub'], $.merge(["RaisedTranscoderEvent"], $.makeArray(arguments)));
             }
        };

        proxies['registrationHub'] = this.createHubProxy('registrationHub'); 
        proxies['registrationHub'].client = { };
        proxies['registrationHub'].server = {
            enterRegistration: function (instanceId) {
                return proxies['registrationHub'].invoke.apply(proxies['registrationHub'], $.merge(["EnterRegistration"], $.makeArray(arguments)));
             },

            leaveRegistration: function (instanceId) {
                return proxies['registrationHub'].invoke.apply(proxies['registrationHub'], $.merge(["LeaveRegistration"], $.makeArray(arguments)));
             },

            notifyRegistrationCompleted: function (personId, membershipId, instanceId) {
                return proxies['registrationHub'].invoke.apply(proxies['registrationHub'], $.merge(["NotifyRegistrationCompleted"], $.makeArray(arguments)));
             }
        };

        return proxies;
    };

    signalR.hub = $.hubConnection("/signalr", { useDefaultPath: false });
    $.extend(signalR, signalR.hub.createHubProxies());

}(window.jQuery, window));
