auth.$inject = [ '$rootScope', 'userService', 'ngDialog', '$state' ];

export default function auth($rootScope, userService, ngDialog, $state) {
    
    $rootScope.$on('$stateChangeStart', (event, toState, toParams) => {

        if (!(toState.data && toState.data.public) && !userService.isAuthenticated()) {       
            event.preventDefault();

            const dialog = ngDialog.open({ 
                template: '<user-auth success="success"></user-auth>',
                plain: true,
                className: 'ngdialog-theme-default custom-width-250 custom-width-350',
                controller: ['$scope', function($scope){
                    $scope.success = function(){
                        dialog.close();
                        return $state.go(toState.name, toParams);
                    };
                }]
            });
            
        }

    });
}
