'use strict';

app.directive('userPanel', function() {
  return {
    restrict: 'EA',
    replace: true,
    scope: {},
                    //'<a class="btn btn-nav pull-right" ui-sref="login" ng-if="!currentUser"><span>Login</span></a>'+
    template: '<div class="white pull-right" ng-if="currentUser">'+
                '<i class="fa fa-user fa-lg"></i>&nbsp;&nbsp; {{currentUser.username}} (<a class="link-unstyled link-default" ng-click="logoutUser()">Logout</a>)'+
               '</div>',
    controller: function($scope, $state, $window, Auth, User) {

      $scope.currentUser = null;

      $scope.logoutUser = function () {
        User.logout();
        $window.location.href = '/installer';
      };

      $scope.$on('user:set', function(event, currentUser) {
        console.log('caught user:set event');
        $scope.currentUser = currentUser;
      });

      $scope.$on('user:unset', function(event) {
        console.log('caught user:unset event');
        $scope.currentUser = null;
      });



    }
  };
});
