/// 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 that = this;
        var selectionListItems = this.state("selectionListItems");
        var selectionListItemIds = _.map(selectionListItems, "id");

        PD.Web.UI.MessageDialog.warn({
            title: "Delete List Items",
            message: "Delete the selected list item(s)?",
            ok: function (e) {
                e.close();
                that._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) {

        var that = this;

        PD.Web.UI.BoxedDialog.warn({
            title: "List Item In Use",
            message: "The following list items are currently in use:{{items}}Continuing with the deletion will result in the loss of data. Continue with the deletion?",
            items: args.names,
            ok: function (e) {
                e.close();
                that._deleteListItems(ids, args.confirmation);
            }
        });

    }

});