app.controller('menuCtrl',
	
function ($scope,$http,$cookies, $location, $route, $timeout, $mdSidenav, $mdMedia, $window, $cookies, utilsSvc, userService, UserManager, menuService) {

	$scope.appVersion = '1.7.9.2';
	$scope.currentMenu = '';
	
	$scope.isUserLoggedIn = function(){
		return userService.getCurrentUser() != null;
	}
	
	$scope.isAmbienteTest = function(){
		return ($location.$$host.toLowerCase().indexOf('tamtest')>=0) || ($location.$$host.toLowerCase().indexOf('localhost')>=0)
	}
	
	$scope.forcedClose = false;
	
	$window.addEventListener('resize', onResize);
	function onResize() {
	  $scope.$digest();
	}
	$scope.$on('$destroy', function() {
	  $window.removeEventListener('resize', onResize);
	});
	
	$scope.shouldLockOpen = function() {
		if ($scope.forcedClose)
			return false;
		else
			return $mdMedia('gt-sm');
	}
	
	$scope.isCurrentMenu=function(url) {
		return $scope.currentMenu==url;//($location.path().indexOf(url)>=0);
	}
	
	$scope.toggleMenu = function() {
		$scope.forcedClose = !$scope.forcedClose;
		if ($scope.forcedClose)
			$mdSidenav('menuSidenav').close();
		else
			$mdSidenav('menuSidenav').open();
	}	

	$scope.showMenu = function(){
		$mdSidenav('menuSidenav').open();
	}

	$scope.largeScreen =function(){
		return utilsSvc.largeScreen();
	}
	
	$scope.notSmallScreen =function(){
		console.log($mdMedia('gt-sm'));
		return $mdMedia('gt-sm');
	}
	
	$scope.closeSideNav = function(){
		if ($mdMedia('sm')||$mdMedia('xs')||$scope.forcedClose)
			$mdSidenav('menuSidenav').close();
	}
	    	
	$scope.forceSidenavOpen = function() {
		return $mdMedia('gt-sm') || ($scope.requestMenuOpen &&  $mdMedia('gt-sm'));
	}
	
	$scope.appVariables = {};
	
	$scope.checkUserPermission = function() {
		if ($scope.isUserLoggedIn()) {
			UserManager.AppVariables().$promise.then(function(data){
				
				$scope.appVariables = data;
				
				
				if ($scope.appVariables.profiloLocale=='UO' || $scope.appVariables.profiloLocale=='UR') {
					if (!$scope.appVariables.abilitaBonifici) {
						$('#BONIFICI').css('display','none');
						$('#SBBONIFICI').css('display','none');
					}
					if (!$scope.appVariables.abilitaAEA) {
						$('#AEA').css('display','none');
						$('#SBAEA').css('display','none');
					}
					if (!$scope.appVariables.abilitaRID) {
						$('#RID').css('display','none');
						$('#SBRID').css('display','none');
					}
					$('#DistinteBanca').css('display','none');
					$('#SBDistinteBanca').css('display','none');
					$('#TabellaBanche').css('display','none');
					$('#SBTabellaBanche').css('display','none');
					$('#RemunerationSeda').css('display','none');
					$('#SBRemunerationSeda').css('display','none');
					
					if ($scope.appVariables.abilitaAEA ==false && $scope.appVariables.abilitaRID==false) {
						$('#AcquisizioneFlussiDispositivi').css('display','none');
						$('#SBAcquisizioneFlussiDispositivi').css('display','none');
					}
						
				}
				
				if ($scope.appVariables.profiloLocale=='UO' || $scope.appVariables.profiloLocale=='UR' || $scope.appVariables.profiloLocale=='VI' ) {
					if (!$scope.appVariables.abilitaAccessoAnagrafica){
						$('#AnagraficaOrdinanti').css('display','none');
						$('#SBAnagraficaOrdinanti').css('display','none');
					}
						
				}
				
				
				
			});
		}
	}
	
	$scope.visibleToProfile = function(menu){
		if (typeof menu!="undefined" && typeof menu.visible != "undefined" && $scope.getCurrentUser()!=null)
			return menu.visible.indexOf($scope.getCurrentUser().userProfile)>=0;
		return false;
	}
	
	$scope.getCurrentUser = function(){
		return userService.getCurrentUser();
	}
	
	$scope.logout = function() {
		userService.logout();
	}
	
	$scope.menu = function(){
		return menuService.getMenu().mainMenu;
	}

	$scope.go = function(url, tipoChiamata) {
		if (tipoChiamata!='') {
			$scope.currentMenu = url;
			var filtro = userService.getFiltroIncassiSDD();
			filtro.tipoChiamata = tipoChiamata;
			
			filtro.idDistintaBanca = 0;
			filtro.idDistintaFlusso = 0;
			filtro.codiceClienteDebitore='';
			
			userService.setFiltroIncassiSDD(filtro);
			
			if (url.indexOf("distinteBanca")==-1) {
				localStorage.removeItem("filterDistinte");
				localStorage.removeItem("filterDistinteAttivo");
				localStorage.removeItem("filterDistinteSelectedIndex");
			}
			
			$route.reload();
		}
			
		$location.path(url);
	}
	
    $scope.closeOnClick = function() {
    	if (!$mdMedia('gt-sm')){
    		$mdSidenav('menuSidenav').close();
    	}
    }
    
    $scope.isMenuSelected = function(menuId) {
        return menuService.isMenuSelected(menuId);
    }

   $scope.setSelectedMenu = function (menu) {
       return menuService.setSelectedMenu(menu)
   }

    $scope.isSubMenuSelected = function(subMenuId) {
        return menuService.isSubMenuSelected(subMenuId);
    }

    $scope.setSelectedSubMenu = function (subMenuId) {
        return menuService.setSelectedSubMenu(subMenuId)
    }

    $scope.hasSubMenu = function(menu) {
        return menuService.hasSubMenu(menu);
    }
    
});
