/// 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) {

        Jade.UI.MessageDialog.open({
            dialogStyle: Jade.UI.MessageDialogStyle.Warning,
            dialogButtons: Jade.UI.MessageDialogButtonStyle.OKCancel,
            title: "Delete Tag",
            message: "Delete the selected tag(s)?",
            select: (e) => {
                e.close();
                if (e.button == Jade.UI.MessageDialogButton.ok) {
                    okFn();
                }
            }
        });

    },

    _showInUseConfirmationDialog: function (taskArgs, ids) {

        Jade.UI.MessageDialog.open({
            dialogStyle: Jade.UI.MessageDialogStyle.Warning,
            dialogButtons: Jade.UI.MessageDialogButtonStyle.YesNo,
            title: "Tags In Use",
            message: "Some of the selected tags are in use proceed with deletion anyway?",
            select: (e) => {
                e.close();
                switch (e.button) {
                    case Jade.UI.MessageDialogButton.yes:
                        that._invokeServiceCall(args, ids, true);
                        break;
                    case Jade.UI.MessageDialogButton.no:
                        taskArgs.abort();
                        break;
                }
            }
        });

    },

    _startTask: function (ids) {

        PDJS.Shared.TaskStatusDialog.open({
            title: "Delete | Tag",
            taskStart: (e) => {
                this._invokeServiceCall(e, ids, false);
            },
            taskSuccess: (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;
            }
        });

    }

});