/*jshint -W100*/
/**
 * Each section of the site has its own module. It probably also has
 * submodules, though this boilerplate is too simple to demonstrate it. Within
 * `src/app/users`, however, could exist several additional folders representing
 * additional modules that would then be listed as dependencies of this one.
 * For example, a `note` section could have the submodules `note.create`,
 * `note.delete`, `note.edit`, etc.
 *
 * Regardless, so long as dependencies are managed correctly, the build process
 * will automatically take take of the rest.
 *
 * The dependencies block here is also where component dependencies should be
 * specified, as shown below.
 */
angular.module( 'ngBoilerplate.users', [
  'ui.router',
  'plusOne',
  'ngBoilerplate.account',
  'ngBoilerplate.home',
  'ngBoilerplate.admin',
  'angular-md5'
])

/**
 * Each section or module of the site can also have its own routes. AngularJS
 * will handle ensuring they are all available at run-time, but splitting it
 * this way makes each module more "self-contained".
 */
.config(function config( $stateProvider ) {
  $stateProvider.state( 'users', {
    url: '/users',
    views: {
       "header": {
        controller: 'UsersCtrl',
        templateUrl: 'header/header.tpl.html'
      },
      "main": {
        controller: 'UsersCtrl',
        templateUrl: 'users/users.tpl.html'
      },
      "footer": {
        controller: 'UsersCtrl',
        templateUrl: 'footer/footer.tpl.html'
      }
    },
    data:{ pageTitle: 'Administrar cuenta' },
    resolve:{
        account: function(accountService, sessionService) {
            if(sessionService.isLoggedIn()){
                console.log("account");
                return accountService.getMyAccount();
            }
        }
    }
  });
})

/**
 * And of course we define a controller for our route.
 */
.controller( 'UsersCtrl', function UsersController( $scope, sessionService, account, $state, $http, md5) {
    $scope.account = account;
    $scope.isLoggedIn = sessionService.isLoggedIn;
    $scope.logout = function() {
        sessionService.logout();
        $state.go("home");
    };
    $scope.isAdmin = sessionService.isAdmin;
    
    //TODO move this to account.js/accountService
    $scope.modifyPassword = function() {
        console.log("modifyPassword");
        if($scope.user.new_password === $scope.user.password_new_verify){
            return $http.post("/basic-web-app/rest/accounts/modifyPassword", "name=" + localStorage.getItem("name") + "&password=" +md5.createHash($scope.user.old_password || '') + "&new_password=" +md5.createHash($scope.user.new_password || ''), {
            headers:{'Content-Type':'application/x-www-form-urlencoded'}
        }
        ).then(function(data){
            alert('Contraseņa modificada correctamente');
            window.location.reload();
        }, function(data){
            alert("Contraseņa incorrecta");
        });
        }
        
    };
})


.directive("passwordVerify", function() {
    return {
        require: "ngModel",
        scope: {
            passwordVerify: '='
        },
        link: function(scope, element, attrs, ctrl) {
            scope.$watch(function() {
                var combined;
                
                if (scope.passwordVerify || ctrl.$viewValue) {
                   combined = scope.passwordVerify + '_' + ctrl.$viewValue; 
                }                    
                return combined;
            }, function(value) {
                if (value) {
                    ctrl.$parsers.unshift(function(viewValue) {
                        var origin = scope.passwordVerify;
                        if (origin !== viewValue) {
                            ctrl.$setValidity("passwordVerify", false);
                            return undefined;
                        } else {
                            ctrl.$setValidity("passwordVerify", true);
                            return viewValue;
                        }
                    });
                }
            });
        }
    };
});
