(function() {
    'use strict';

    angular
        .module('app')
        .service('userService', userService);

    userService.$inject = ['$http', '$cookies', 'auth2Service'];

    function userService($http, $cookies, auth2Service) {
        this.getMe = getMe;

        function getMe() {
            if (this.me != undefined)
                return this.me;
            if ($cookies.get('User') != "" || $cookies.get('User') != undefined) {
                this.me = JSON.parse($cookies.get('User'));
                return this.me;
            }

            if (this.me === undefined)
                $http.get('/api/user').then(
                    function(response) {
                        this.me = response.data;
                    },
                    function(response) {
                        this.me = {};
                        auth2Service.checkAuthByResponse(response);
                    }
                );
        }
    }
})();