/**
 * 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/home`, 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.home', [
  'ui.router',
  'plusOne',
  'ngBoilerplate.account',
  'ngBoilerplate.users',
  'ngBoilerplate.admin',
  'ngBoilerplate.activeIngredient',
  'ui.bootstrap',
  'ngBoilerplate.message'
])

/**
 * 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( 'home', {
    url: '/home',
    views: {
       "header": {
        controller: 'HomeCtrl',
        templateUrl: 'header/header.tpl.html'
      },
      "admin_header": {
        controller: 'HomeCtrl',
        templateUrl: ''
      },
      "main": {
        controller: 'HomeCtrl',
        templateUrl: 'home/home.tpl.html'
      },
      "kidney_data@home": {
        controller: 'HomeCtrl',
        templateUrl: 'home/kidney_data.tpl.html'
      },
      "footer": {
        controller: 'HomeCtrl',
        templateUrl: 'footer/footer.tpl.html'
      }
    },
    data:{ pageTitle: 'Consultas' },
    resolve:{
        activeIngredients: function(activeIngredientService, sessionService) {
            console.log("activeIngredients");
            if(sessionService.isLoggedIn()){
                return activeIngredientService.getAllActiveIngredientsWithRecomendations();
            }else{
                return "";
            }
        }
    }
  });
})

.controller( 'HomeCtrl', function HomeController( $scope, sessionService, activeIngredients) {
    $scope.isLoggedIn = sessionService.isLoggedIn;
    $scope.activeIngredients = activeIngredients;
    $scope.logout = function() {
        sessionService.logout();
        $state.go("home");
    };
    $scope.isAdmin = sessionService.isAdmin;
    $scope.isUser = sessionService.isUser;
    $scope.isTempUser = sessionService.isTempUser;
})

.controller( 'AccordionDemoCtrl', function AccordionDemoCtrl( $scope) {
    $scope.oneAtATime = true;
})

.filter('rangeFilter', function() {
    return function( items , value) {
        var filtered = [];
        // If time is with the range
        angular.forEach(items, function(item) {
            if((value == null)||(value === 0)){
                filtered.push(item);
            }

            if( (parseInt(item.maxGlomerularFiltrationRate,10) >= parseInt(value,10)) && 
                (parseInt(item.minGlomerularFiltrationRate,10) <= parseInt(value,10)) ) {
                filtered.push(item);
            }
        });
        return filtered;
    };
})

.controller("MessageCtrl", function($scope, messageService) {
    $scope.sendMessage = function() {
        var account = localStorage.getItem("session");
        var name = localStorage.getItem("name");
        if(account !== null && name !== null){
            console.log("MessageCtrl");
            messageService.sendMessage($scope.message, function() {
                $scope.message.messageText = "";
                alert("Mensaje mandado correctamente, gracias por su sugerencia");
        },
        function() {
            alert("Error mandando el mensaje, pruebe a salir y volver a entrar a su cuenta");
        });
        }
    };
});

