/// class PD.Manage.Modules.SyncUserCommand

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

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

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

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

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

        var user = _.first(this.state("users"));

        this._showSyncDialog(user.id);

    },

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

    _isEnabled: function () {
        return _.isArray(this.state("users"))
            && this.state("users").length == 1;;
    },

    _showSyncDialog: function (id) {

        PD.Shared.Dialogs.TaskStatus.open({
            config: {
                title: "Sync User"
            },
            showDetail: true,
            taskStart: function (args) {

                PD.ajax({
                    type: "POST",
                    url: PD.Path.resolve("~/Manage/ActiveDirectory/SyncUser/" + id),
                    success: function (e) {
                        args.startPolling(e);
                    },
                    error: function (e) {
                        args.abort(e.responseJSON);
                        e.errorHandled = true;
                    }
                });

            },
            close: function (e) {

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

            }
        });

    }

});