/**
 * Created by emileharmel on 18/07/2014.
 */

(function (define ) {
	"use strict";

	define( [
		'utils/createGuid'
	], function (createGuid )
	{

		var UNABLE_TO_CONNECT      = 'Unable to connect',

		 CockpitApi = function ($rootScope, $resource, $http, $q, $log )
			{

				$log = $log.getInstance( "CockpitApi" );
				$log.debug( "constructor() ");


				var usersCollection     = $resource("/api/v1/users/"),
					usersResource       = $resource("/api/v1/users/:id", {id: '@id'}),

					/**
					 * Util function to build a resolved promise
					 * @returns {promise|*|promise}
					 */
					makeResolved = function( response )
					{
						var dfd = $q.defer();
						dfd.resolve( response );

						return dfd.promise;
					},
					makeRejected = function( fault )
					{
						var dfd = $q.defer();
						dfd.reject( fault );

						return dfd.promise;
					},


					getCurrentUser = function()
					{
						return $http.get("/api/v1/currentUser")
								.then(
								function (data)
								{
									return makeResolved({ data : data.data });

								},
								function(error)
								{
									return makeRejected( '401 (Unauthorized)' )
								},
								function(progress)
								{
									console.log(progress+'progress');
								});
					},

					saveUser = function (data, user)
					{
						angular.extend(user, data);
						return usersResource.save(user);
					},


					checkLogin = function(username, password){


						return $http(
							{
								method: 'POST',
								url: '/login',
								data: $.param({
									'username': username,
									'password': password
								}),
								headers: {'Content-Type': 'application/x-www-form-urlencoded'}
							}).then(
							function (answer)
							{
								return makeResolved({ session : createGuid(), username : username });
							},
							function(error)
							{
								return makeRejected( '401 (Unauthorized)' )
							},
							function(progress)
							{
								console.log(progress+'progress');
							});

					};

				return {

					getUser         : getCurrentUser,
					saveUser        : saveUser,
					checkLogin      : checkLogin,
					makeResolved    : makeResolved,
					makeRejected    : makeRejected

				};



			};



		return ["$rootScope", "$resource", "$http", "$q", "$log", CockpitApi];
	});


}( define  ));

@@ -386,32 +386,43 @@
   = $resource(%22/
+musterroll/
 api/v1/users/%22),
@@ -461,16 +461,27 @@
 ource(%22/
+musterroll/
 api/v1/u
@@ -955,16 +955,27 @@
 p.get(%22/
+musterroll/
 api/v1/c
@@ -1530,16 +1530,16 @@
 'POST',%0A
-
 %09%09%09%09%09%09%09%09
@@ -1545,16 +1545,27 @@
 %09url: '/
+musterroll/
 login',%0A

add musterroll prefix to path