/**
 * Master Login Frontend Manager
 *
 * @module package/quiqqer/masterlogin/bin/js/frontend/classes/Manager
 * @author www.pcsg.de (Patrick Müller)
 */
define('package/quiqqer/masterlogin/bin/js/frontend/classes/Manager', [

    'Ajax'

], function (QUIAjax) {
    "use strict";

    var pkg = 'quiqqer/masterlogin';

    return new Class({

        Type: 'package/quiqqer/masterlogin/bin/js/frontend/classes/Manager',

        /**
         * Switches the current session user to $userId (requires permission!)
         *
         * @param {Number} userId
         * @return {Promise}
         */
        logInAs: function (userId) {
            return new Promise(function (resolve, reject) {
                QUIAjax.post('package_quiqqer_masterlogin_ajax_frontend_logInAs', resolve, {
                    'package': pkg,
                    userId   : userId,
                    onError  : reject
                });
            });
        },

        /**
         * Switches the current session user to the original user
         *
         * @return {Promise}
         */
        logBack: function (userId) {
            return new Promise(function (resolve, reject) {
                QUIAjax.post('package_quiqqer_masterlogin_ajax_frontend_logBack', resolve, {
                    'package': pkg,
                    onError  : reject
                });
            });
        },

        /**
         * Search all possible MasterLogin slave users
         *
         * @return {Promise}
         */
        searchUsers: function (SearchParams) {
            return new Promise(function (resolve, reject) {
                QUIAjax.get('package_quiqqer_masterlogin_ajax_frontend_searchUsers', resolve, {
                    'package'   : pkg,
                    searchParams: JSON.encode(SearchParams),
                    onError     : reject
                });
            });
        }
    });
});