/**
 * Created by Téo on 11/04/2016.
 */
'use strict';

controllers.controller('ModifyProfilCtrl',['$rootScope','$scope', '$location','ModifyProfilServ','ProfilServ','ConnexionServ','$cookies','$http',
    function($rootScope,$scope, $location,ModifyProfilServ,ProfilServ/*,ConnexionServ*/,$cookies,$http) {

    $scope.return = function() {
        $location.path('/profil');
    };

    ProfilServ.getProfil(
        function success(response) {
            $scope.id = response.id;
            $scope.name  = response.name;
            $scope.firstName = response.firstName;
            $scope.pseudo = response.pseudo;
        },
        function error(errorResponse) {
            if(errorResponse.status==401){
                alert("Session interrompue")
                console.log("Utilisateur non authentifié.");
            }

            console.log("Error:" + JSON.stringify(errorResponse));
            $location.path('/');
        }
    );

    $scope.submit = function () {

        if ($scope.password == null) {
            var userModification = {
                id: $scope.id,
                name: $scope.name,
                firstName: $scope.firstName,
                avatar: ""
            };
        }
        else {
            var userModification = {
                id: $scope.id,
                name: $scope.name,
                firstName: $scope.firstName,
                avatar: "",
                password : $scope.password
            };
        }

        var pseudo = $scope.pseudo;
        var password = $scope.password;

        ModifyProfilServ.modifyProfil(userModification,
        function success(response) {
            //alert($scope.challenge.question);
            console.log("Success:" + JSON.stringify(response));
            if ($scope.password != null) {
                $cookies.put("username", pseudo);
                $http.defaults.headers.common['Authorization'] = "Basic " + btoa(pseudo + ":" + password);
            }
            $location.path('/profil');
        },
        function error() {

            if(errorResponse.status==401){
                alert("Session interrompue")
                console.log("Utilisateur non authentifié.");
                $location.path('/');
            }
            else {
                $scope.errorMessage = "Erreur côté serveur.";
                console.log("Error:" + JSON.stringify(errorResponse));
                $location.path('/');
            }
        }
    );

    };

}])
@@ -2052,19 +2052,72 @@
 n error(
-) %7B
+errorResponse) %7B%0A            console.log(errorResponse);
 %0A%0A      

add rerror in parameter