/**
 * 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',

        initialize: function () {
            this.$csrfToken = '';
        },

        setCsrfToken: function (csrfToken) {
            this.$csrfToken = csrfToken;
        },

        /**
         * Switches the current session user to $userId (requires permission!)
         *
         * @param {Number} userId
         * @return {Promise}
         */
        logInAs: function (userId) {
            const csrfToken = this.$csrfToken;

            return new Promise(function (resolve, reject) {
                QUIAjax.post('package_quiqqer_masterlogin_ajax_frontend_logInAs', resolve, {
                    'package': pkg,
                    userId   : userId,
                    csrfToken: csrfToken,
                    onError  : reject
                });
            });
        },

        /**
         * Switches the current session user to the original user
         *
         * @return {Promise}
         */
        logBack: function () {
            const csrfToken = this.$csrfToken;

            return new Promise(function (resolve, reject) {
                QUIAjax.post('package_quiqqer_masterlogin_ajax_frontend_logBack', resolve, {
                    'package': pkg,
                    csrfToken: csrfToken,
                    onError  : reject
                });
            });
        },

        /**
         * Search all possible MasterLogin slave users
         *
         * @return {Promise}
         */
        searchUsers: function (SearchParams) {
            const csrfToken = this.$csrfToken;

            return new Promise(function (resolve, reject) {
                QUIAjax.post('package_quiqqer_masterlogin_ajax_frontend_searchUsers', resolve, {
                    'package'   : pkg,
                    searchParams: JSON.encode(SearchParams),
                    csrfToken   : csrfToken,
                    onError     : reject
                });
            });
        }
    });
});
