export function RoutesRun( $rootScope,
                           $translate,
                           $mdComponentRegistry,
                           $mdSidenav,
                           ToastService,
                           AnalyticService,
                           DocumentService,
                           $location,
                           $auth,
                           $state )
{
	'ngInject';

	//
	let onStateChangeStart = $rootScope.$on( "$stateChangeStart",
		( event, toState, toParams, fromState, fromParams ) =>
		{
			DocumentService.invalidateTitle();
			$rootScope.isCMS = false;

			//
			if( toState.data && toState.data.auth )
			{
				DocumentService.changeTitle("cms");
				$rootScope.isCMS = true;

				//
				if( !$auth.isAuthenticated() )
				{
					event.preventDefault();

					ToastService.error( 'Session expired!' );

					return $state.go( 'main.login' );
				}
			}

			if( toState.data && toState.data.title )
				DocumentService.changeTitle( toState.data.title, true );

			// ------------------------------- //
			// ------------------------------- //

			$rootScope.$state = toState;
			$rootScope.$stateParams = toParams;

			if( !$rootScope.language )
				$rootScope.language = $translate.preferredLanguage();

			if( $rootScope.isTextAlignmentLeft == null ) // == undefined/null
				$rootScope.isTextAlignmentLeft = true;

			if( !$rootScope.isLoading )
				$rootScope.isLoading = true;

			if( $mdComponentRegistry.get( 'side-menu' ) )
				$mdSidenav( 'side-menu' ).close();

			//
			$rootScope.showCookiePolicy = false;

			let page = toState && toState.name && toState.name === 'main.landing';
			let control = window.mycc === undefined;

			$rootScope.showCookiePolicy = page || control;
		} );

	//
	$rootScope.$on( '$destroy', () =>
	{
		onStateChangeStart();
	} );
}