(function () {
  'use strict';

  angular
    .module('ddayApp')
    .directive('navbar', navbar);

  /** @ngInject */
  function navbar () {
    return {
      restrict: 'E',
      replace: true,
      transclude: true,
      templateUrl: 'app/components/navbar/navbar.html',
      controller: NavbarController,
      controllerAs: 'navbar'
    };

    /** @ngInject */
    function NavbarController (Auth) {
      var vm = this,
        isAuthenticated = Auth.isAuthenticated(),
        userProfile = null,
        links = [
          {
            route: 'project',
            label: 'Project',
            shouldShow: function () {
              return isAuthenticated;
            }
          },
          {
            route: 'voting',
            label: 'Voting',
            shouldShow: function () {
              return isAuthenticated && userProfile && userProfile.projectId;
            }
          },
          {
            route: 'account',
            label: 'Account',
            shouldShow: function () {
              return isAuthenticated;
            }
          },
          {
            route: 'settings',
            label: 'Settings',
            shouldShow: function () {
              return isAuthenticated && userProfile && userProfile.role && ('ADMIN' === userProfile.role);
            }
          },
          {
            route: 'auth',
            label: 'Login',
            shouldShow: function () {
              return !isAuthenticated;
            }
          },
          {
            route: null,
            label: 'Logout',
            shouldShow: function () {
              return isAuthenticated;
            },
            onClick: Auth.logout
          }
        ];

      Auth.getLoggedIn().then(function (loggedIn) {
        Auth.getLoggedInProfile(loggedIn.uid).then(function (user) {
          userProfile = user;
          filterLinks();
        }, handleError);
      });

      vm.links = [
        {
          route: 'dashboard',
          label: 'Dashboard'
        }
      ];

      function filterLinks () {
        _.each(links, function (link) {
          if (link.shouldShow()) {
            vm.links.push(link);
          }
        });
      }

      function handleError (error) {
        toastr.clear();
        toastr.error(error.toString());
      }
    }
  }

})();

@@ -1710,24 +1710,163 @@
         %5D;%0A%0A
+      vm.links = %5B%0A        %7B%0A          route: 'dashboard',%0A          label: 'Dashboard'%0A        %7D%0A      %5D;%0A%0A      if (isAuthenticated) %7B%0A  
       Auth.g
@@ -1901,24 +1901,26 @@
 loggedIn) %7B%0A
+  
         Auth
@@ -1972,24 +1972,26 @@
 on (user) %7B%0A
+  
           us
@@ -2018,16 +2018,18 @@
         
+  
 filterLi
@@ -2031,24 +2031,26 @@
 terLinks();%0A
+  
         %7D, h
@@ -2072,119 +2072,59 @@
     
+  
 %7D);%0A
-%0A
       
-vm.links = %5B%0A        %7B%0A          route: 'dashboard',%0A          label: 'Dashboard'%0A        %7D%0A      %5D;
+%7D else %7B%0A        filterLinks();%0A      %7D
 %0A%0A  

Fix for navbar links when user is not logged in