'use strict';

/**
 * A banner that represents a pending reply to a review.
 *
 * The banner offers actions for publishing and discarding the review.
 */
RB.ReviewRequestPage.ReviewReplyDraftBannerView = RB.FloatingBannerView.extend({
    className: 'banner',

    template: _.template('<h1><%- draftText %></h1>\n<p><%- reminderText %></p>\n<span class="banner-actions">\n <input type="button" value="<%- publishText %>"\n        class="publish-button" />\n <input type="button" value="<%- discardText %>"\n        class="discard-button" />\n</span>\n<% if (showSendEmail) { %>\n <label>\n  <input type="checkbox" class="send-email" checked />\n  <%- sendEmailText %>\n</label>\n<% } %>'),

    events: {
        'click .publish-button': '_onPublishClicked',
        'click .discard-button': '_onDiscardClicked'
    },

    /**
     * Render the banner.
     *
     * Returns:
     *     RB.ReviewRequestPage.ReviewReplyDraftBannerView:
     *     This object, for chaining.
     */
    render: function render() {
        var _this = this;

        RB.FloatingBannerView.prototype.render.call(this);

        var reviewRequestEditor = this.options.reviewRequestEditor;

        this.$el.html(this.template({
            draftText: gettext('This reply is a draft.'),
            reminderText: gettext('Be sure to publish when finished.'),
            publishText: gettext('Publish'),
            discardText: gettext('Discard'),
            sendEmailText: gettext('Send E-Mail'),
            showSendEmail: reviewRequestEditor.get('showSendEmail')
        }));

        this.listenTo(this.model, 'saving destroying', function () {
            return _this.$('input').prop('disabled', true);
        });
        this.listenTo(this.model, 'saved', function () {
            return _this.$('input').prop('disabled', false);
        });
        this.listenTo(this.model, 'publishError', function (errorText) {
            return alert(errorText);
        });

        return this;
    },


    /**
     * Handler for when Publish is clicked.
     *
     * Publishes the reply.
     */
    _onPublishClicked: function _onPublishClicked() {
        var $sendEmail = this.$('.send-email');

        this.model.publish({
            trivial: $sendEmail.length === 1 && !$sendEmail.is(':checked')
        }, this);
    },


    /**
     * Handler for when Discard is clicked.
     *
     * Discards the reply.
     */
    _onDiscardClicked: function _onDiscardClicked() {
        this.model.destroy();
    }
});

//# sourceMappingURL=reviewReplyDraftBannerView.js.map