/// class PD.Manage.Modules.TagDeleteCommand

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

    //===================================
    // PROPERTIES
    //===================================

    get_tags: function () {
        return this.state("tags") || [];
    },

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

    setDefaults: function () {
        this.props("id", "tag/delete");
        this.props("enabled", false);
        this.props("icon", PD.Path.to16x16("delete.png"));
        this.props("label", "Delete");
        this.props("tooltip", "Delete the selected tag.");
        this.state("tags", null);
    },

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

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

        var that = this;
        var ids = _.map(this.get_tags(), "id");
        
        this._showConfirmationDialog(ids, function () {
            that._startTask(ids);
        });

    },

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

    _isEnabled: function () {
        return this.get_tags().length > 0;
    },

    _showConfirmationDialog: function (tag, okFn) {

        ProspectDirector.Web.UI.MessageDialog.confirm({
            message: "Delete the selected tag(s)?",
            buttons: {
                ok: {
                    label: "OK",
                    callback: okFn
                },
                cancel: {
                    label: "Cancel"
                }
            }
        });

    },

    _showInUseConfirmationDialog: function (taskArgs, ids) {

        var that = this;

        ProspectDirector.Web.UI.MessageDialog.confirm({
            message: "Some of the selected tags are in use proceed with deletion anyway?",
            buttons: {
                ok: {
                    label: "Yes",
                    callback: function (e) {
                        e.close();
                        that._invokeServiceCall(args, ids, true);
                    }
                },
                cancel: {
                    label: "No",
                    callback: function () {
                        taskArgs.abort();
                    }
                }
            }
        });

    },

    _startTask: function (ids) {

        var that = this;

        PD.Shared.Dialogs.TaskStatus.open({
            config: {
                title: "Delete Tags"
            },
            showDetail: true,
            enableClose: false,
            taskStart: function (args) {
                that._invokeServiceCall(args, ids, false);
            },
            taskSuccess: function (e) {
                e.enableClose(true);
            },
            close: function (e) {

                PD.context
                    .get_default()
                    .get_events()
                    .publish("tag/delete");

            }
        });


    },

    _invokeServiceCall: function (taskArgs, ids, confirmed) {

        var that = this;
        var service = new PDJS.Services.Tags.TagService();

        service.delete({
            ids: ids,
            confirmed: confirmed,
            success: function (e) {

                if (e.data.requiresConfirmation) {
                    that._showInUseConfirmationDialog(taskArgs, ids);
                }
                else {
                    taskArgs.startPolling(e.data);
                }
            },
            error: function (e) {
                taskArgs.abort(e.error);
                e.errorHandled = true;
            }
        });

    }

});