/// class PD.Manage.Modules.SyncGroupCommand

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

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

    setDefaults: function () {
        this.props("id", "sync/group");
        this.props("enabled", true);
        this.props("icon", PD.Path.to16x16("arrow_circle2.png"));
        this.props("label", "Sync Group");
        this.props("tooltip", "Update the selected Group with data<br/>retrieved from Active Directory.");
        this.state("groups", null);
    },

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

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

        var group = _.first(this.state("groups"));

        this._showSyncDialog(group.id);

    },

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

    _isEnabled: function () {
        return _.isArray(this.state("groups"))
            && this.state("groups") != null;
    },

    _showSyncDialog: function (id) {

        PDJS.Shared.TaskStatusDialog.open({
            title: "Sync | Group",
            taskStart: (e) => {

                PD.ajax({
                    type: "POST",
                    url: PD.Path.resolve(`~/Manage/ActiveDirectory/SyncGroup/${id}`),
                    success: (e2) => {
                        e.startPolling(e2);
                    },
                    error: (e2) => {
                        e.abort(e2.responseJSON);
                        e2.errorHandled = true;
                    }
                });

            },
            taskSuccess: (e) => {

                PD.context
                    .get_default()
                    .get_events()
                    .publish("sync/refresh");

            }
        });

    }

});