/// class PD.Manage.Modules.MailQueueResendCommand

PD.Manage.Modules.MailQueueResendCommand =
PD.Commands.Command.extend({

    //===================================
    // PUBLIC METHODS
    //===================================

    setDefaults: function () {

        this.props("id", "mail-queue-item/resend");
        this.props("enabled", false);
        this.props("icon", PD.Path.to16x16("mail_forward.png"));
        this.props("label", "Resend");
        this.props("tooltip", "Resend the selected messages.");

        this.state("mailQueueItems", null);

    },

    setState: function (state) {
        this._super(state);
        this.props("enabled", this.isEnabled());
    },

    isEnabled: function () {
        return _.isArray(this.state("mailQueueItems"))
            && this.state("mailQueueItems").length === 1;
    },

    execute: function (e) {
        this._super(e);

        let mailQueueItem = _.first(this.state("mailQueueItems"));

        Jade.UI.MessageDialog.open({
            dialogStyle: Jade.UI.MessageDialogStyle.Warning,
            dialogButtons: Jade.UI.MessageDialogButtonStyle.OKCancel,
            allowClose: true,
            title: "Resend Queued Message",
            message: "Resend the selected mail message?",
            select: (e) => {
                e.close();
                if (e.button === Jade.UI.MessageDialogButton.ok) {
                    this._executeResend(mailQueueItem);
                }
            }
        });

    },

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

    _executeResend: function (mailQueueItem) {

        let service = new PD.Manage.Services.MailQueueService();

        service.retryItem(mailQueueItem.id, () => {
            PD.context
                .get_default()
                .get_events()
                .publish("mail-queue-item/delete", {
                    id: mailQueueItem.id
                });
        });

    }

});