/// class PD.Manage.Modules.SelectionListItemDeleteCommand

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

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

    setDefaults: function () {

        this.props("id", "selection-list-item/delete");
        this.props("enabled", false);
        this.props("icon", PD.Path.to16x16("delete.png"));
        this.props("label", "Delete");
        this.props("tooltip", "Delete the selected List Item.");

        this.state("selectionListItems", null);

    },

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

    isEnabled: function () {

        var selectionListItems = this.state("selectionListItems");

        return _.isArray(selectionListItems)
            && selectionListItems.length > 0;

    },

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

        var selectionListItems = this.state("selectionListItems");
        var selectionListItemIds = _.map(selectionListItems, "id");

        Jade.UI.MessageDialog.open({
            dialogStyle: Jade.UI.MessageDialogStyle.Warning,
            dialogButtons: Jade.UI.MessageDialogButtonStyle.OKCancel,
            allowClose: true,
            title: "Delete List Items",
            message: "Delete the selected list item(s)?",
            select: (e) => {
                e.close();
                if (e.button === Jade.UI.MessageDialogButton.ok) {
                    this._deleteListItems(selectionListItemIds, null);
                }
            }
        });

    },

    _deleteListItems: function (ids, confirmation) {

        var that = this;
        var service = new PD.Manage.Services.SelectionListItemService();

        service.deleteListItems(ids, confirmation, function (e) {
            if (e.requiresConfirmation) {
                that._showDeleteConfirmationDialog(ids, e);
            }
            else {
                PD.context
                    .get_default()
                    .get_events()
                    .publish("selection-list-item/delete", ids);
            }
        });

    },

    _showDeleteConfirmationDialog: function (ids, args) {

        let message = `
            The following list items are currently in use:
            ${args.names.join("<br/>")}
            Continuing with the deletion will result in the loss of data.
            Continue with the deletion?`;
        
        Jade.UI.MessageDialog.open({
            dialogStyle: Jade.UI.MessageDialogStyle.Warning,
            dialogButtons: Jade.UI.MessageDialogButtonStyle.OKCancel,
            allowClose: true,
            title: "List Item In Use",
            message: message,
            select: (e) => {
                e.close();
                if (e.button === Jade.UI.MessageDialogButton.ok) {
                    that._deleteListItems(ids, args.confirmation);
                }
            }
        });

    }

});