jscape.domain = jscape.domain || {};

/** @class */
jscape.DomainRestrictions = Class.extend(
    /** @lends {jscape.DomainRestrictions.prototype} */
{
    /** @constructor */
    initialize: function(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o) {
        this.maxConcurrentConnections = parseInt(a) || null;
        this.maxIpConnections = parseInt(b) || null;
        this.maxUserConnections = parseInt(c) || null;
        this.maxSessionDownloads = parseInt(d) || null;
        this.maxSessionUploads = parseInt(e) || null;
        this.maxFileDownloadSize = parseInt(f) || null;
        this.maxFileUploadSize = parseInt(g) || null;
        this.maxDownloads = h || null;
        this.maxUploads = i || null;
        this.maxTransfers = j || null;
        this.maxTransferRate = parseInt(k) || null;
        this.maxAccountAuthorizationAttempts = l || null;
        this.maxIpAuthorizationAttempts = m || null;
        this.maxIpFlaggingAttempts = n || null;
        this.maxInvalidAuthentications = parseInt(o) || null;
    }
});
jscape.DomainRestrictions.fromJSON = function(data) {
    return data
            ? new jscape.DomainRestrictions(data.maxConcurrentConnections,
                                            data.maxIpConnections,
                                            data.maxUserConnections,
                                            data.maxSessionDownloads,
                                            data.maxSessionUploads,
                                            data.maxFileDownloadSize,
                                            data.maxFileUploadSize,
                                            jscape.DomainRestrictions.Quota.fromJSON(data.maxDownloads),
                                            jscape.DomainRestrictions.Quota.fromJSON(data.maxUploads),
                                            jscape.DomainRestrictions.Quota.fromJSON(data.maxTransfers),
                                            data.maxTransferRate,
                                            jscape.DomainRestrictions.AttemptLimit.fromJSON(data.maxAccountAuthorizationAttempts),
                                            jscape.DomainRestrictions.AttemptLimit.fromJSON(data.maxIpAuthorizationAttempts),
                                            jscape.DomainRestrictions.AttemptLimit.fromJSON(data.maxIpFlaggingAttempts),
                                            data.maxInvalidAuthentications)
            : null;
};
/** @class */
jscape.DomainRestrictions.Quota = Class.extend(
    /** @lends {jscape.DomainRestrictions.Quota.prototype} */
{
    /** @constructor */
    initialize: function(a, b) {
        this.bytes = parseInt(a) || 0;
        this.resetPeriod = parseInt(b) || 0;
    }
});
jscape.DomainRestrictions.Quota.fromJSON = function(data) {
    return data ? new jscape.DomainRestrictions.Quota(data.bytes, data.resetPeriod) : null;
};

/** @class */
jscape.DomainRestrictions.AttemptLimit = Class.extend(
    /** @lends {jscape.DomainRestrictions.AttemptLimit.prototype} */
{
    /** @constructor */
    initialize: function(a, b, c) {
        this.maxAttempts = parseInt(a) || 0;
        this.attemptsPeriodMillis = parseInt(b) || 0;
        this.resetPeriodMillis = parseInt(c) || 0;
    }
});
jscape.DomainRestrictions.AttemptLimit.fromJSON = function(data) {
    return data ? new jscape.DomainRestrictions.AttemptLimit(data.maxAttempts, data.attemptsPeriodMillis, data.resetPeriodMillis) : null;
};

/** @class */
jscape.domain.ConnectionsModule = jscape.domain.DomainModule.extend(
    /** @lends {jscape.domain.ConnectionsModule.prototype} */
{
    onCreate: function () {
        this._super.apply(this, arguments);

        this.controller = new jscape.domain.ConnectionsController();
        this.controller.onCreate(this.managementService, this.domainName);
    },

    onStart: function () {
        this.controller.onStart();
    },

    onPause: function() {
        if (this.controller.hasChanged()) {
            this._confirmAndApply([this.controller]);
        }
    },

    onResume: function() {
        this.controller.onResume();
    }
});

/** @class */
jscape.domain.ConnectionsController = Class.extend(
    /** @lends {jscape.domain.ConnectionsController.prototype} */
{
    DEFAULT_DESCRIPTOR: new jscape.DomainRestrictions(),

    view: null,
    descriptor: null,

    onCreate: function (managementService, domainName) {
        this.managementService = managementService;
        this.domainName = domainName;
    },

    onStart: function() {
        if (this.view == null) {
            this.view = new jscape.domain.ConnectionsView();
        }
        this.view.show(this);
    },

    hasChanged: function() {
        return this.descriptor && !Object._equals(this.descriptor, this._createDescriptor());
    },

    onResume: function() {
        this.managementService.getDomainRestrictions(this.domainName)
                .done($.proxy(function(descriptor) { this.descriptor = descriptor; }, this))
                .fail($.proxy(function() { this.descriptor = this._copyDescriptor(this.DEFAULT_DESCRIPTOR) }, this))
                .always($.proxy(function() { this._setupView(this.descriptor); }, this));
    },

    onApply: function () {
        return this.managementService.setDomainRestrictions(this.domainName, this._createDescriptor())
                .done($.proxy(this._fireApplySuccess, this))
                .done($.proxy(function (descriptor) {
                    this.descriptor = descriptor;
                    this._setupView(this.descriptor);
                }, this));
    },

    onDiscard: function() {
        var descriptor = this.descriptor;
        this.descriptor = this._copyDescriptor(descriptor);
        this._setupView(this.descriptor);
    },

    _createDescriptor: function () {
        return new jscape.DomainRestrictions(this.view.getMaxConcurrentConnections(),
                                             this.view.getMaxIpConnections(),
                                             this.view.getMaxUserConnections(),
                                             this.view.getMaxSessionDownloads(),
                                             this.view.getMaxSessionUploads(),
                                             this.view.getMaxFileDownloadSize(),
                                             this.view.getMaxFileUploadSize(),
                                             this.view.getMaxDownloads(),
                                             this.view.getMaxUploads(),
                                             this.view.getMaxTransfers(),
                                             this.view.getMaxTransferRate(),
                                             this.view.getAccountAuthorization(),
                                             this.view.getIpAuthorization(),
                                             this.view.getIpFlagging(),
                                             this.view.getMaxInvalidAuthentications());
    },

    _copyDescriptor: function(descriptor) {
        return jscape.DomainRestrictions.fromJSON(descriptor);
    },

    _setupView: function (descriptor) {
        if (descriptor && this.view) {
            this.view.setMaxConcurrentConnections(descriptor.maxConcurrentConnections);
            this.view.setMaxIpConnections(descriptor.maxIpConnections);
            this.view.setMaxUserConnections(descriptor.maxUserConnections);
            this.view.setMaxSessionDownloads(descriptor.maxSessionDownloads);
            this.view.setMaxSessionUploads(descriptor.maxSessionUploads);
            this.view.setMaxFileDownloadSize(descriptor.maxFileDownloadSize);
            this.view.setMaxFileUploadSize(descriptor.maxFileUploadSize);
            this.view.setMaxDownloads(descriptor.maxDownloads);
            this.view.setMaxUploads(descriptor.maxUploads);
            this.view.setMaxTransfers(descriptor.maxTransfers);
            this.view.setMaxTransferRate(descriptor.maxTransferRate);
            this.view.setAccountAuthorization(descriptor.maxAccountAuthorizationAttempts);
            this.view.setIpAuthorization(descriptor.maxIpAuthorizationAttempts);
            this.view.setIpFlagging(descriptor.maxIpFlaggingAttempts);
            this.view.setMaxInvalidAuthentications(descriptor.maxInvalidAuthentications);
        }
    },

    _fireApplySuccess: function () {
        $(document).triggerHandler("broadcast", [window.R.string.APPLICATION_SUCCESS_APPLY]);
    }
});

/** @class */
jscape.domain.ConnectionsView = Class.extend(
    /** @lends {jscape.domain.ConnectionsView.prototype} */
{
    /** @const */
    TIMEOUT_FACTOR: 60 * 1000,

    /** @constructor */
    initialize: function() {
        this.fieldset = $("fieldset[name=connectionsfields]");

        this.maxConcurrentConnections = new jscape.domain.ConnectionsView.Restriction(
                $("input[name=maxconcurconenabled]:checkbox", this.fieldset),
                $("input[name=maxconcurcon]", this.fieldset).numberspinner({min: 1, max: 1000, value: 100, required: true, disabled: true, width: 60})
        );
        this.maxIpConnections = new jscape.domain.ConnectionsView.Restriction(
                $("input[name=maxipconenabled]:checkbox", this.fieldset),
                $("input[name=maxipcon]", this.fieldset).numberspinner({min: 1, max: 1000, value: 100, required: true, disabled: true, width: 60})
        );
        this.maxUserConnections = new jscape.domain.ConnectionsView.Restriction(
                $("input[name=maxuserconenabled]:checkbox", this.fieldset),
                $("input[name=maxusercon]", this.fieldset).numberspinner({min: 1, max: 1000, value: 100, required: true, disabled: true, width: 60})
        );
        this.maxSessionDownloads = new jscape.domain.ConnectionsView.Restriction(
                $("input[name=maxsessiondloadsenabled]:checkbox", this.fieldset),
                $("input[name=maxsessiondloads]", this.fieldset).numberspinner({min: 1, max: 1000, increment: 10, value: 100, required: true, disabled: true, width: 60})
        );
        this.maxSessionUploads = new jscape.domain.ConnectionsView.Restriction(
                $("input[name=maxsessionuploadsenabled]:checkbox", this.fieldset),
                $("input[name=maxsessionuploads]", this.fieldset).numberspinner({min: 1, max: 1000, increment: 10, value: 100, required: true, disabled: true, width: 60})
        );
        this.maxFileDownloadSize = new jscape.domain.ConnectionsView.Restriction(
                $("input[name=maxfiledloadsizeenabled]:checkbox", this.fieldset),
                $("input[name=maxfiledloadsize]", this.fieldset).numberspinner({
                    min: 1, max: 9999, increment: 10, value: 100, required: true, disabled: true, width: 60,
                    unitbox: $("select[name=maxfiledloadsizeunit]", this.fieldset).combobox({
                        required: true, editable: false, disabled: true, width: 60, panelHeight: "auto"
                    })
                })
        );
        this.maxFileUploadSize = new jscape.domain.ConnectionsView.Restriction(
                $("input[name=maxfileuploadsizeenabled]:checkbox", this.fieldset),
                $("input[name=maxfileuploadsize]", this.fieldset).numberspinner({
                    min: 1, max: 9999, increment: 10, value: 100, required: true, disabled: true, width: 60,
                    unitbox: $("select[name=maxfileuploadsizeunit]", this.fieldset).combobox({
                        required: true, editable: false, disabled: true, width: 60, panelHeight: "auto"
                    })
                })
        );
        this.maxDownloads = new jscape.domain.ConnectionsView.RestrictionResettable(
                $("input[name=maxdloadsenabled]:checkbox", this.fieldset),
                $("input[name=maxdloads]", this.fieldset).numberspinner({
                    min: 1, max: 9999, increment: 10, value: 1000, required: true, disabled: true, width: 60,
                    unitbox: $("select[name=maxdloadsunit]", this.fieldset).combobox({
                        required: true, editable: false, disabled: true, width: 60, panelHeight: "auto"
                    })
                }),
                $("input[name=maxdloadsperiod]", this.fieldset).numberspinner({min: 1, max: 1000, value: 1, width: 60})
        );
        this.maxUploads = new jscape.domain.ConnectionsView.RestrictionResettable(
                $("input[name=maxuploadsenabled]:checkbox", this.fieldset),
                $("input[name=maxuploads]", this.fieldset).numberspinner({
                    min: 1, max: 9999, increment: 10, value: 1000, required: true, disabled: true, width: 60,
                    unitbox: $("select[name=maxuploadsunit]", this.fieldset).combobox({
                        required: true, editable: false, disabled: true, width: 60, panelHeight: "auto"
                    })
                }),
                $("input[name=maxuploadsperiod]", this.fieldset).numberspinner({min: 1, max: 1000, value: 1, width: 60})
        );
        this.maxTransfers = new jscape.domain.ConnectionsView.RestrictionResettable(
                $("input[name=maxtransfersenabled]:checkbox", this.fieldset),
                $("input[name=maxtransfers]", this.fieldset).numberspinner({
                    min: 1, max: 9999, increment: 10, value: 1000, required: true, disabled: true, width: 60,
                    unitbox: $("select[name=maxtransfersunit]", this.fieldset).combobox({
                        required: true, editable: false, disabled: true, width: 60, panelHeight: "auto"
                    })
                }),
                $("input[name=maxtransfersperiod]", this.fieldset).numberspinner({min: 1, max: 1000, value: 1, width: 60})
        );

        this.maxTransferRate = new jscape.domain.ConnectionsView.Restriction(
                $("input[name=maxtransferrateenabled]:checkbox", this.fieldset),
                $("input[name=maxtransferrate]", this.fieldset).numberspinner({
                    min: 1, max: 9999, increment: 10, value: 100, required: true, disabled: true, width: 60,
                    unitbox: $("select[name=maxtransferrateunit]", this.fieldset).combobox({
                        required: true, editable: false, disabled: true, width: 60, panelHeight: "auto"
                    })
                })
        );

        this.accountAuthorizationEnabled = $("input[name=disableaccountenabled]:checkbox", this.fieldset);
        this.accountAuthorization = $("input[name=disableaccount]", this.fieldset).numberspinner({min: 1, max: 100, value: 3, required: true, disabled: true, width: 60});
        this.accountAuthorizationTimeout = $("input[name=disableaccounttimeout]", this.fieldset).numberspinner({min: 1, max: 1000, value: 3, required: true, disabled: true, width: 60});
        this.accountAuthorizationRestore = $("input[name=disableaccountrestore]", this.fieldset).numberspinner({min: 0, max: 1000, value: 60, required: true, disabled: true, width: 60});
        this.accountAuthorizationEnabled.change($.proxy(this._adjustSpinners, this, this.accountAuthorizationEnabled, [this.accountAuthorization, this.accountAuthorizationTimeout, this.accountAuthorizationRestore]));

        this.ipAuthorizationEnabled = $("input[name=disableipenabled]:checkbox", this.fieldset);
        this.ipAuthorization = $("input[name=disableip]", this.fieldset).numberspinner({min: 1, max: 100, value: 3, required: true, disabled: true, width: 60});
        this.ipAuthorizationTimeout = $("input[name=disableiptimeout]", this.fieldset).numberspinner({min: 1, max: 1000, value: 3, required: true, disabled: true, width: 60});
        this.ipAuthorizationRestore = $("input[name=disableiprestore]", this.fieldset).numberspinner({min: 0, max: 1000, value: 60, required: true, disabled: true, width: 60});
        this.ipAuthorizationEnabled.change($.proxy(this._adjustSpinners, this, this.ipAuthorizationEnabled, [this.ipAuthorization, this.ipAuthorizationTimeout, this.ipAuthorizationRestore]));

        this.ipFlaggingEnabled = $("input[name=flagipenabled]:checkbox", this.fieldset);
        this.ipFlagging = $("input[name=flagip]", this.fieldset).numberspinner({min: 1, max: 100, value: 3, required: true, disabled: true, width: 60});
        this.ipFlaggingTimeout = $("input[name=flagiptimeout]", this.fieldset).numberspinner({min: 1, max: 1000, value: 3, required: true, disabled: true, width: 60});
        this.ipFlaggingRestore = $("input[name=flagiprestore]", this.fieldset).numberspinner({min: 0, max: 1000, value: 60, required: true, disabled: true, width: 60});
        this.ipFlaggingEnabled.change($.proxy(this._adjustSpinners, this, this.ipFlaggingEnabled, [this.ipFlagging, this.ipFlaggingTimeout, this.ipFlaggingRestore]));

        this.maxInvalidAuthEnabled = $("input[name=maxinvalidauthenabled]:checkbox", this.fieldset);
        this.maxInvalidAuth = $("input[name=maxinvalidauth]", this.fieldset).numberspinner({min: 1, max: 99, value: 3, required: true, disabled: true, width: 60});
        this.maxInvalidAuthEnabled.change($.proxy(this._adjustSpinners, this, this.maxInvalidAuthEnabled, [this.maxInvalidAuth]));

        this.applyButton = $("a[role=button][name=apply]", this.fieldset).linkbutton({onClick: $.proxy(this._onClick$ApplyButton, this)});
        this.discardButton = $("a[role=button][name=discard]", this.fieldset).linkbutton({onClick: $.proxy(this._onClick$DiscardButton, this)});
    },

    getMaxConcurrentConnections: function() {
        return this.maxConcurrentConnections.val();
    },

    setMaxConcurrentConnections: function(value) {
        this.maxConcurrentConnections.val(value);
    },

    getMaxIpConnections: function() {
        return this.maxIpConnections.val();
    },

    setMaxIpConnections: function(value) {
        this.maxIpConnections.val(value);
    },

    getMaxUserConnections: function() {
        return this.maxUserConnections.val();
    },

    setMaxUserConnections: function(value) {
        this.maxUserConnections.val(value);
    },

    getMaxSessionDownloads: function() {
        return this.maxSessionDownloads.val();
    },

    setMaxSessionDownloads: function(value) {
        this.maxSessionDownloads.val(value);
    },

    getMaxSessionUploads: function() {
        return this.maxSessionUploads.val();
    },

    setMaxSessionUploads: function(value) {
        this.maxSessionUploads.val(value);
    },

    getMaxFileDownloadSize: function() {
        return this.maxFileDownloadSize.val();
    },

    setMaxFileDownloadSize: function(value) {
        this.maxFileDownloadSize.val(value);
    },

    getMaxFileUploadSize: function() {
        return this.maxFileUploadSize.val();
    },

    setMaxFileUploadSize: function(value) {
        this.maxFileUploadSize.val(value);
    },

    getMaxDownloads: function() {
        return this._getQuota(this.maxDownloads);
    },

    setMaxDownloads: function(value) {
        this._setQuota(this.maxDownloads, value);
    },

    getMaxUploads: function() {
        return this._getQuota(this.maxUploads);
    },

    setMaxUploads: function(value) {
        this._setQuota(this.maxUploads, value);
    },

    getMaxTransfers: function() {
        return this._getQuota(this.maxTransfers);
    },

    setMaxTransfers: function(value) {
        this._setQuota(this.maxTransfers, value);
    },

    getMaxTransferRate: function() {
        return this.maxTransferRate.val();
    },

    setMaxTransferRate: function(value) {
        this.maxTransferRate.val(value);
    },

    getAccountAuthorization: function() {
        return this.accountAuthorizationEnabled.is(":checked")
                ? new jscape.DomainRestrictions.AttemptLimit(parseInt(this.accountAuthorization.numberspinner("getValue")),
                                                             parseInt(this.accountAuthorizationTimeout.numberspinner("getValue")) *  this.TIMEOUT_FACTOR,
                                                             parseInt(this.accountAuthorizationRestore.numberspinner("getValue")) * this.TIMEOUT_FACTOR)
                : null
    },

    setAccountAuthorization: function(value) {
        if (value != null) {
            this.accountAuthorization.numberspinner("setValue", value.maxAttempts);
            this.accountAuthorizationTimeout.numberspinner("setValue", value.attemptsPeriodMillis / this.TIMEOUT_FACTOR);
            this.accountAuthorizationRestore.numberspinner("setValue", value.resetPeriodMillis / this.TIMEOUT_FACTOR);
        }
        this.accountAuthorizationEnabled.prop("checked", value != null).change();
    },

    getIpAuthorization: function() {
        return this.ipAuthorizationEnabled.is(":checked")
                ? new jscape.DomainRestrictions.AttemptLimit(parseInt(this.ipAuthorization.numberspinner("getValue")),
                                                             parseInt(this.ipAuthorizationTimeout.numberspinner("getValue")) *  this.TIMEOUT_FACTOR,
                                                             parseInt(this.ipAuthorizationRestore.numberspinner("getValue")) * this.TIMEOUT_FACTOR)
                : null
    },

    setIpAuthorization: function(data) {
        if (data != null) {
            this.ipAuthorization.numberspinner("setValue", data.maxAttempts);
            this.ipAuthorizationTimeout.numberspinner("setValue", data.attemptsPeriodMillis / this.TIMEOUT_FACTOR);
            this.ipAuthorizationRestore.numberspinner("setValue", data.resetPeriodMillis / this.TIMEOUT_FACTOR);
        }
        this.ipAuthorizationEnabled.prop("checked", data != null).change();
    },

    getIpFlagging: function () {
        return this.ipFlaggingEnabled.is(":checked")
               ? new jscape.DomainRestrictions.AttemptLimit(parseInt(this.ipFlagging.numberspinner("getValue")),
                                                            parseInt(this.ipFlaggingTimeout.numberspinner("getValue")) *  this.TIMEOUT_FACTOR,
                                                            parseInt(this.ipFlaggingRestore.numberspinner("getValue")) * this.TIMEOUT_FACTOR)
                : null;
    },

    setIpFlagging: function(data) {
        if (data != null) {
            this.ipFlagging.numberspinner("setValue", data.maxAttempts);
            this.ipFlaggingTimeout.numberspinner("setValue", data.attemptsPeriodMillis / this.TIMEOUT_FACTOR);
            this.ipFlaggingRestore.numberspinner("setValue", data.resetPeriodMillis / this.TIMEOUT_FACTOR);
        }
        this.ipFlaggingEnabled.prop("checked", data != null).change();
    },

    getMaxInvalidAuthentications: function() {
        return this.maxInvalidAuthEnabled.is(":checked")
                ? parseInt(this.maxInvalidAuth.numberspinner("getValue"))
                : null;
    },

    setMaxInvalidAuthentications: function(value) {
        if (value != null) {
            this.maxInvalidAuth.numberspinner("setValue", parseInt(value));
        }
        this.maxInvalidAuthEnabled.prop("checked", value != null).change();
    },

    show: function(listener) {
        this.listener = listener || jscape.domain.ConnectionsView.LISTENER;
    },

    _getQuota: function(input) {
        var size = input.val();
        var period = input.resetPeriod();
        return size && period ? new jscape.DomainRestrictions.Quota(size, period) : null;
    },

    _setQuota: function(input, quota) {
        input.val(quota ? quota.bytes : null);
        input.resetPeriod(quota ? quota.resetPeriod : null);
    },

    _onClick$ApplyButton: function() {
        if (this.fieldset.form("validate")) {
            this.applyButton.linkbutton("disable");
            this.listener.onApply(this)
                    .always($.proxy(function () { this.applyButton.linkbutton("enable"); }, this));
        }
    },

    _onClick$DiscardButton: function() {
        this.listener.onDiscard(this);
    },

    _adjustSpinners: function (checkbox, spinners) {
        var enabled = checkbox.is(":checked");
        $.each(spinners, function(_, spinner) {
            spinner.numberspinner(enabled ? "enable" : "disable").numberspinner(enabled ? "enableValidation" : "disableValidation");
        });
    }
});
jscape.domain.ConnectionsView.LISTENER = {
    onApply: $.noop,
    onDiscard: $.noop
};

/** @class */
jscape.domain.ConnectionsView.Restriction = Class.extend(
    /** @lends {jscape.domain.ConnectionsView.Restriction.prototype} */
{
    /** @constructor */
    initialize: function (checkbox, spinner) {
        this.enabled = checkbox.change($.proxy(this._onChange$Enable, this));

        this.input = spinner;
        $.extend(this.input.numberspinner("options"), {required: true, disabled: true});
    },

    val: function (value) {
        if (!arguments.length) {
            return this.enabled.is(":checked") ? parseInt(this.input.numberspinner("getValue")) : null;
        } else {
            value = parseInt(value);
            if (isNaN(value)) {
                this.enabled.prop("checked", false).change();
            } else {
                this.enabled.prop("checked", true).change();
                this.input.numberspinner("setValue", value);
            }
        }
    },

    _onChange$Enable: function () {
        var enable = this.enabled.is(":checked");
        if (enable) {
            this.input.numberspinner("enable").numberspinner("enableValidation");
        } else {
            this.input.numberspinner("disable").numberspinner("disableValidation");
        }
        var unitbox = this.input.numberspinner("options").unitbox;
        if (unitbox && unitbox.length) {
            if (enable) {
                unitbox.combobox("enable").combobox("enableValidation");
            } else {
                unitbox.combobox("disable").combobox("disableValidation");
            }
        }
    }
});
/** @class */
jscape.domain.ConnectionsView.RestrictionResettable = jscape.domain.ConnectionsView.Restriction.extend(
    /** @lends {jscape.domain.ConnectionsView.RestrictionResettable.prototype} */
{
    RESET_PERIOD_FACTOR: 24 * 60 * 60 * 1000,

    /** @constructor */
    initialize: function(checkbox, spinner, resetSpinner) {
        this._super(checkbox, spinner);
        this.resetSpinner = resetSpinner;
    },

    resetPeriod: function(value) {
        if (!arguments.length) {
            return this.enabled.is(":checked") ? parseInt(this.resetSpinner.numberspinner("getValue")) * this.RESET_PERIOD_FACTOR : null;
        } else {
            value = parseInt(value);
            if (!isNaN(value)) {
                this.resetSpinner.numberspinner("setValue", value / this.RESET_PERIOD_FACTOR);
                this._adjustResetSpinner();
            }
        }
    },

    /** @override */
    _onChange$Enable: function() {
        this._super();
        this._adjustResetSpinner();
    },

    _adjustResetSpinner: function() {
        if (this.enabled.is(":checked")) {
            this.resetSpinner.numberspinner("enable").numberspinner("enableValidation");
        } else {
            this.resetSpinner.numberspinner("disable").numberspinner("disableValidation");
        }
    }
});
