/// class PD.Manage.Modules.Resources.ResourceDeleteCommand
/// using PD.Manage.Modules.Resources.ResourceCommand

PD.Manage.Modules.Resources.ResourceDeleteCommand =
PD.Manage.Modules.Resources.ResourceCommand.extend({

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

    setDefaults: function () {
        this._super();
        this.props("id", "resource/delete");
        this.props("enabled", false);
        this.props("label", "Delete");
        this.props("icon", PD.Path.to16x16("delete.png"));
        this.props("tooltip", "Delete the selected files and/or folders.");
    },

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

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

        PD.Web.UI.MessageDialog.warn({
            title: "Delete Selections",
            message: "Delete the selected " + (this.getResources().length) + " items?",
            ok: $.proxy(this._executeDeletion, this)
        });

    },

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

    _executeDeletion: function (e) {

        var resources = this.getResources(),
            folderIds = _.map(_.filter(resources, { type: "folder" }), "id"),
            resourceIds = _.map(resources, "id"),
            service = new PD.Manage.Services.ResourcesService();

        e.close();

        service.deleteResources(resourceIds, function () {
            PD.context.get_default().get_events().publish("resources/delete", {
                resourceIds: resourceIds,
                folderIds: folderIds
            });
        });

    },

    _isEnabled: function () {

        var result = false,
            resource = _.first(this.getResources());

        if (resource != null) {
            result = !PD.Type.isEmptyGuid(resource.id);
        }

        return result && this.hasSelections();

    }

});