/// class PD.Manage.Modules.MailQueueDeleteCommand

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

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

    setDefaults: function () {

        this.props("id", "mail-queue-item/delete");
        this.props("enabled", false);
        this.props("icon", PD.Path.to16x16("delete.png"));
        this.props("label", "Delete");
        this.props("tooltip", "Delete 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);

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

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

    },

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

    _executeDelete: function (mailQueueItem) {

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

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

    }

});