/// class PD.Manage.Modules.Maintenance.ScheduleMaintenanceDialog
// using PD.Web.UI.Dialog

PD.Manage.Modules.Maintenance.ScheduleMaintenanceDialog =
PD.Web.UI.Dialog.extend({

    //===================================
    // CONSTRUCTOR
    //===================================

    init: function (options) {

        this._super($.extend(true, {
            config: {
                resizable: false,
                height: "210px",
                width: "500px",
                title: "Schedule Maintenance"
            }
        }, options));

    },

    //===================================
    // PROPERTIES
    //===================================

    get_scheduleDate: function () {
        return this.get_node().find("INPUT[data-id=schedule-date]");
    },

    get_scheduleHour: function () {
        return this.get_node().find("SELECT[data-id=schedule-hour]");
    },
    
    get_scheduleMinute: function () {
        return this.get_node().find("SELECT[data-id=schedule-minute]");
    },
    
    get_schedulePeriod: function () {
        return this.get_node().find("SELECT[data-id=schedule-period]");
    },

    get_okButton: function () {
        return this.get_node().find("BUTTON[data-id=ok]");
    },

    get_cancelButton: function () {
        return this.get_node().find("BUTTON[data-id=cancel]");
    },

    get_notificationMinutes: function () {
        return this.get_node().find("SELECT[data-id=notification-minutes]");
    },

    //===================================
    // EVENT HANDLERS
    //===================================

    onActivate: function () {

        this.get_okButton().on("click", _.bind(this.okButton_onClick, this));
        this.get_cancelButton().on("click", _.bind(this.cancelButton_onClick, this));

        this.populateScheduleHours();
        this.populateScheduleMinutes();
        this.populateValues();

    },

    okButton_onClick: function () {

        var that = this,
            fieldHighlighter = new PD.Web.InvalidFieldHighlighter();

        fieldHighlighter.clear(this.get_node());

        PD.ajax({
            type: "POST",
            url: PD.Path.resolve("~/Manage/Maintenance/Schedule"),
            data: {
                startAt: this.getScheduledDateTime(),
                warningMinutes: this.get_notificationMinutes().val()
            },
            contextData: {
                container: this.get_node()
            },
            success: function (e) {
                that.close({
                    command: "ok",
                    data: e
                });
            }
        });

    },

    cancelButton_onClick: function () {
        this.close();
    },

    //===================================
    // PROTECTED METHODS
    //===================================

    template: function (e) {

        return [
            "<div class='-pd-maintenance-mode-scheduler-dialog'>",
                "<div style='font-size:12px;'>",
                    "<div style='margin-bottom:10px;'>",
                        "Use this dialog to schedule a future date and time for the system to enter maintenance mode. When a future date and time for maintenance has been scheduled the application will continue to function normally until the scheduled time is reached, at which point the application will automatically enter maintenance mode.",
                    "</div>",
                    "<div style='background-color:#FFFFFF;border:1px solid #DDDDDD;padding:10px;margin-bottom:10px;'>",
                        "<label for='StartAt' style='display:inline-block;font-weight:bold;margin-bottom:10px;'>Start Maintenance</label><br/>",
                        "Date:&nbsp;<input type='text' style='width:80px;padding-left:5px;' data-id='schedule-date' /> ",
                        "@ <select data-id='schedule-hour'></select>",
                        "&nbsp;:&nbsp;",
                        "<select data-id='schedule-minute'></select>",
                        "&nbsp;",
                        "<select data-id='schedule-period'><option value='AM'>AM</option><option value='PM'>PM</option></select>",
                    "</div>",
                "</div>",
                "<div data-id='button-deck'>",
                    "<button class='-pd-button' data-id='ok'>OK</button>",
                    "<button class='-pd-button' data-id='cancel'>Cancel</button>",
                "</div>",
            "</div>"
        ];

    },

    //===================================
    // PRIVATE METHODS
    //===================================

    getScheduledDateTime: function (convertToDate) {
        
        var result = null,
            date = this.get_scheduleDate().val(),
            hour = this.get_scheduleHour().val(),
            minute = this.get_scheduleMinute().val(),
            period = this.get_schedulePeriod().val();

        if (date != "") {
            result = date + " " + hour + ":" + minute + ":00 " + period;
        }

        if (result != null && convertToDate) {
            result = new Date(result);
        }

        return result;

    },

    populateScheduleHours: function () {

        var that = this;

        _.times(12, function (index) {
            index++;
            that.get_scheduleHour().append("<option value='" + index + "'>" + index + "</option>");
        });

    },

    populateScheduleMinutes: function () {

        var that = this;

        _.times(60, function (index) {
            index = index < 10 ? "0" + index : index;
            that.get_scheduleMinute().append("<option value='" + index + "'>" + index + "</option>");
        });

    },

    populateValues: function () {

        var startAt = this.get_options().startAt,
            warningMins = this.get_options().warningMins,
            year = null,
            month = null,
            day = null,
            hour = null,
            minute = null,
            period = null;

        if (_.isDate(startAt)) {

            year = startAt.getFullYear();
            month = startAt.getMonth() + 1;
            day = startAt.getDate();
            hour = startAt.getHours();
            period = hour < 12 ? "AM" : "PM";
            hour = hour < 13 ? hour : hour - 12;
            minute = startAt.getMinutes();

            this.get_scheduleDate().val(month + "/" + day + "/" + year);
            this.get_scheduleHour().val(hour);
            this.get_scheduleMinute().val(minute < 10 ? "0" + minute : minute);
            this.get_schedulePeriod().val(period);

        }
        else {
            this.get_scheduleDate().val(this.getCurrentDate());
        }

        this.get_scheduleDate().select();
        this.get_scheduleDate().focus();

        if (_.isNumber(warningMins)) {
            this.get_notificationMinutes().val(warningMins);
        }

    },

    getCurrentDate: function () {

        var current = new Date();
        var dd = current.getDate();
        var mm = current.getMonth() + 1;
        var yy = current.getFullYear();

        return mm + "/" + dd + "/" + yy;

    }

});

PD.Manage.Modules.Maintenance.ScheduleMaintenanceDialog.open =
function (options) {
    return new PD.Manage.Modules.Maintenance.ScheduleMaintenanceDialog(options);
};