'use strict';

app.directive('userPanel', function() {
  return {
    restrict: 'EA',
    //replace: true,
    scope: {},
    template: '<a class="btn btn-nav pull-right" ui-sref="home" ng-if="!currentUser">' +
                '<i class="fa fa-user fa-lg"></i>&nbsp;&nbsp;<span>Login &#47; Register</span>'+
              '</a>'+
              '<button class="btn btn-nav pull-right" ng-if="currentUser" data-placement="bottom-right" data-html="1" data-trigger="click" bs-dropdown="dropdown">'+
                '<i class="fa fa-user fa-lg"></i>&nbsp;&nbsp; {{currentUser.username}} &nbsp;<i class="fa fa-caret-down"></i>'+
              '</button>',
    controller: function($scope, $state, $window, $modal, Auth, User) {

      $scope.currentUser = null;

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

      $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;
      });


      $scope.dropdown = [
        {
          "text": "<i class=\"fa fa-list\"></i>&nbsp;&nbsp;New Job",
          "href": "/urcassistant#/home",
          "target": "_self"
        },
        {
          "text": "<i class=\"fa fa-users\"></i>&nbsp;&nbsp;Account Settings",
          "href": "/urcassistant#/account-settings",
          "target": "_self"
        },
        {
          "text": "<i class=\"fa fa-sign-out\"></i>&nbsp;&nbsp;Logout",
          "click": "logoutUser()"
        }
      ];


    }
  };
});
