var AppConfig = {url:'rest/'};


var app = angular.module(
		'app',
		[
				'ngRoute', 'ngMaterial', 'ngAnimate', 'ngCookies', 'ngSanitize'
				,'ng-mfb',
				'ui.grid','ui.grid.grouping',
				,'disableAll'
				,'services'
				,'utilsSvc'
				,'formChangeLoggerSvc'
				,'userService'
				,'menuService'
				,'incassoSDDSvc',
				'ngFileUpload'
				//'userManager',
				
		]
)
.config(['$locationProvider', function($locationProvider) {
        $locationProvider.hashPrefix('');
    }])
    .config(['$rootScopeProvider', function($rootScopeProvider) {
    	$rootScopeProvider.digestTtl(100);
    }])
.filter('to_trusted', ['$sce', function($sce){
        return function(text) {
            return $sce.trustAsHtml(text);
        };
    }])
.run(function($rootScope, $location, userService, UserManager, utilsSvc) {
	
	$rootScope.$on("$routeChangeStart", function(event, next, current) {
		// Check for valid client session
		if (userService.getCurrentUser() == null) { // no user logged
													// in->redirect to login if
													// not already
			if (next.templateUrl != "partials/login.html")
				$location.path("/login");
		}
		
		if (next.templateUrl != "partials/login.html" && next.allowed && userService.getCurrentUser()!=null)
			if (next.allowed.indexOf(userService.getCurrentUser().userProfile)==-1) {
				utilsSvc.showMessage("Utente non autorizzato");
				$location.path("/");
			}
			
		
		//check for valid server session
		UserManager.ValidSession({},function(data){
			if (!data.value) {
				userService.logout();
			}
		});

	})

	$rootScope.url = '/sicura/';
})
.config(function($httpProvider){
  $httpProvider.defaults.headers.common['Cache-Control'] = 'no-cache';
  $httpProvider.defaults.cache = false;

  if (!$httpProvider.defaults.headers.get) {
      $httpProvider.defaults.headers.get = {};
  }
  $httpProvider.defaults.headers.get['If-Modified-Since'] = '0';
})
.config(['$compileProvider', function ($compileProvider) {
  $compileProvider.debugInfoEnabled(false);
}])
.config(function($mdDateLocaleProvider) {
	$mdDateLocaleProvider.months = ['gennaio', 'febbraio', 'marzo','aprile','maggio','giugno','luglio','agosto','settembre','ottobre','novembre','dicembre'];
    $mdDateLocaleProvider.shortMonths = ['gen', 'feb', 'mar','apr','mag','giu','lug','ago','set','ott','nov','dic'];
    $mdDateLocaleProvider.days = ['domenica', 'lunedì', 'martedì','mercoledì','giovedì','venerdì','sabato'];
    $mdDateLocaleProvider.shortDays = ['dom','lun','mar','mer','gio','ven','sab'];	
	$mdDateLocaleProvider.formatDate = function(date) {
		if (date==null)
			return '';
		else
	      return date.asDateString();
	    };
    $mdDateLocaleProvider.parseDate = function(str){
	    	var parts = str.split("/");
	    	var dt = new Date(parseInt(parts[2], 10),
	                  parseInt(parts[1], 10) - 1,
	                  parseInt(parts[0], 10));
	    	
	    	return dt;
	    }	   
	    
})
.config(function($mdThemingProvider) {

	$mdThemingProvider.definePalette('app-palette', $mdThemingProvider.extendPalette('green', {
	    'contrastDefaultColor': 'light',
	    'contrastDarkColors': '50 100 200 A100',
	    'contrastStrongLightColors': '300 400 A200 A400'
	  }));
	  $mdThemingProvider.definePalette('app-accent', $mdThemingProvider.extendPalette('brown', {
	    'A100': '#DE3641'
	  }));
	
	  $mdThemingProvider.theme('app-dark', 'default')
	    .primaryPalette('yellow')
	    .dark();
	
	  $mdThemingProvider.theme('default')
	      .primaryPalette('app-palette')
	      .accentPalette('app-accent');
	
	  $mdThemingProvider
	      .enableBrowserColor();
})
  .directive('capitalize', function() {
    return {
      require: 'ngModel',
      link: function(scope, element, attrs, modelCtrl) {
        var capitalize = function(inputValue) {
          if (inputValue == undefined) inputValue = '';
          var capitalized = inputValue.toUpperCase();
          if (capitalized !== inputValue) {
            // see where the cursor is before the update so that we can set it back
            var selection = element[0].selectionStart;
            modelCtrl.$setViewValue(capitalized);
            modelCtrl.$render();
            // set back the cursor after rendering
            element[0].selectionStart = selection;
            element[0].selectionEnd = selection;
          }
          return capitalized;
        }
        modelCtrl.$parsers.push(capitalize);
        capitalize(scope[attrs.ngModel]); // capitalize initial value
      }
    };
  });

;    
