/// class PD.Shared.Maintenance.MaintenanceMonitor

PD.Shared.Maintenance.MaintenanceMonitor = Class.extend({

    //===================================
    // FIELDS
    //===================================

    _dialogHasBeenAcknowledged: null,

    //===================================
    // CONSTRUCTORS
    //===================================

    init: function () {

        var maintenance = $.connection.maintenance;

        maintenance.client.broadcastMaintenanceModeWarning = _.bind(this._onBroadcastMaintenanceModeWarning, this);
        maintenance.client.broadcastApplicationExit = _.bind(this._onBroadcastApplicationExit, this);
        maintenance.client.broadcastMaintenanceStart = _.bind(this._onBroadcastMaintenanceStart, this);
        maintenance.client.broadcastMaintenanceEnded = _.bind(this._onBroadcastMaintenanceEnded, this);

        $.connection.hub.start({ pingInterval: null });

    },

    _showMaintenanceNotification: function (e) {

        var notificationIsAcknowledged = this._maintenanceNotificationIsAcknowledged(e.maintenanceId);

        if (!notificationIsAcknowledged) {
            this._showMaintenanceNotificationDialog(e.message, e.maintenanceId);
        }

        if (notificationIsAcknowledged) {
            this._showMaintenanceNotificationToaster(e.message);
        }
        
    },

    _onBroadcastMaintenanceStart: function (e) {

        if ($(".-pd-maintenance-earmark").length === 0) {

            $("BODY").append("<div class='-pd-maintenance-earmark'><div>Under</div><div>Maintenance</div></div>");

            $(".-pd-maintenance-earmark").on("click", function (e) {

                $(e.currentTarget).css({
                    display: "none"
                });

                setTimeout(function () {

                    $(e.currentTarget).css({
                        display: ""
                    });

                }, 3000);

            });

        }

    },

    _onBroadcastMaintenanceEnded: function (e) {

        if ($(".-pd-maintenance-earmark").length > 0) {
            $(".-pd-maintenance-earmark").remove();
        }

    },

    _onBroadcastMaintenanceModeWarning: function (e) {

        this._showMaintenanceNotification({
            message: e.timeUntil,
            maintenanceId: e.maintenanceId
        });

    },

    _onBroadcastApplicationExit: function (e) {
        window.location = PD.Path.resolve("~/hub/home");
    },

    _showMaintenanceNotificationDialog: function (message, maintenanceId) {

        var that = this;

        this._acknowledgeMaintenanceNotification(maintenanceId);

        PD.Web.UI.MessageDialog.warn({
            title: "Scheduled System Maintenance Notification",
            message: "<span style='font-weight:bold;color:darkred;'>WARNING: Upcoming System Maintenance</span><br/><br/>Maintenance will start in " + message + ".<br/><br/>Complete your work and exit the application before maintenance begins to ensure no work is lost.",
            ok: function (e) {
                e.close();
            }
        });

    },

    _showMaintenanceNotificationToaster: function (message) {
        PD.messageStack.action("<div style='height:40px;padding:10px;text-align:center;'><b>WARNING</b><br/> Maintenance will start in " + message + ".</div>");
    },

    _acknowledgeMaintenanceNotification: function (maintenanceId) {
        localStorage.setItem("maintenanceId", maintenanceId);
    },

    _maintenanceNotificationIsAcknowledged: function (maintenanceId) {
        return localStorage.getItem("maintenanceId") === maintenanceId;
    }

});

PD.onBoot(function () {

    if (PD.Monitors == null) {
        PD.Monitors = {};
    }

    PD.Monitors["Maintenance"] = new PD.Shared.Maintenance.MaintenanceMonitor();

});