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

/** @class */
jscape.domain.DescriptionModule = jscape.domain.DomainModule.extend(
    /** @lends {jscape.domain.DescriptionModule.prototype} */
{
    view: null,
    description: null,

    onCreate: function() {
        this._super.apply(this, arguments);

        this._initValidators();
    },

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

    onResume: function() {
        this.view.refresh();
    },

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

    onRefresh: function() {
        return this.managementService.getDomainDescription(this.domainName)
                .done($.proxy(function(value) { this.description = value; }, this));
    },

    onApply: function() {
        if (!this.view.valid()) {
            return $.Deferred().reject().promise();
        }

        var description = this.view.getDescription();
        return this.managementService.setDomainDescription(this.domainName, description)
                .done($.proxy(function () {
                          this.description = description;
                          this._fireApplySuccess();
                      }, this));
    },

    onDiscard: function () {
        this.view.refresh();
    },

    hasChanged: function () {
        return this.description != null && this.description != this.view.getDescription();
    },

    _initValidators: function() {
        var v = new jscape.Validator();
        v.rule("domain_description", $.proxy(this._validateDescription, this), window.R.string.DOMAIN_ERROR_DESCRIPTION_TOO_LONG);
    },

    _validateDescription: function(value) {
        return typeof value !== "string" || value.length < 1024;
    }
});

/** @class */
jscape.domain.DescriptionView = Class.extend(
    /** @lends {jscape.domain.DescriptionView.prototype} */
{
    /** @constructor */
    initialize: function() {
        this.fieldset = $("fieldset[name=descriptionfields]").form({onChange: $.proxy(this._adjustButtonsState, this)});

        this.description = $("textarea[name=description]", this.fieldset).textbox({required: false, validType: "domain_description", multiline: true, height: 270});
        this.description.textbox("textbox").on("input", $.proxy(this._adjustButtonsState, this));

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

    getDescription: function() {
        return this.description.textbox("getValue");
    },

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

    refresh: function() {
        this.listener.onRefresh()
                .done($.proxy(function(value) {
                                 this.description.textbox("setValue", value).textbox("validate");
                                 this.description.data("originalValue", value);
                                 this._adjustButtonsState();
                              }, this));
    },

    valid: function() {
        return this.fieldset.form("validate");
    },

    _onClick$ApplyButton: function() {
        this.applyButton.linkbutton("disable");
        this.listener.onApply()
                .done($.proxy(function() { this.description.data("originalValue", this.getDescription()); }, this))
                .always($.proxy(this._adjustButtonsState, this));
    },

    _onClick$DiscardButton: function() {
        this.applyButton.linkbutton("disable");
        this.listener.onDiscard();
    },

    _adjustButtonsState: function() {
        var modified = this.description.textbox("textbox").val() != this.description.data("originalValue");
        this.applyButton.linkbutton(modified ? "enable" : "disable");
        this.discardButton.linkbutton(modified ? "enable" : "disable");
    }
});
jscape.domain.DescriptionView.LISTENER = {
    onApply: $.noop,
    onDiscard: $.noop
};