/* @ngInject */
module.exports = function ($scope, $state, AuthService, AUTH_EVENTS, $http) {
  $scope.currentUser = null;

  $scope.updateCurrentUser = function () {
    $scope.currentUser = AuthService.getCurrentUser();
    $scope.updateCurrentUserHeaders();
  };

  $scope.updateCurrentUserHeaders = function () {
    var token, email;
    if ($scope.currentUser) {
      token = $scope.currentUser.access_token;
      email = $scope.currentUser.email;
    }
    var defaultHeaders = {
      "Accept": "application/json",
      "X-User-Token": token,
      "X-User-Email": email,
    };
    $http.defaults.headers.common = defaultHeaders;
  };

  $scope.$on(AUTH_EVENTS.loginSuccess, function () {
    $scope.updateCurrentUser();
    if ($scope.currentUser.force_password_reset === true) {
      $state.go('layout.user_settings');
    } else {
      $state.go('layout.group.rounds', {groupId: 1});
    }
  });

  $scope.$on(AUTH_EVENTS.logoutSuccess, function () {
    $scope.updateCurrentUser();
  });

  $scope.updateCurrentUser();
};

/* @ngInject */
module.exports = function ($scope, $state, AuthService, AUTH_EVENTS, $http) {
  $scope.currentUser = null;

  $scope.updateCurrentUser = function () {
    $scope.currentUser = AuthService.getCurrentUser();
    $scope.updateCurrentUserHeaders();
  };

  $scope.updateCurrentUserHeaders = function () {
    var token, email;
    if ($scope.currentUser) {
      token = $scope.currentUser.access_token;
      email = $scope.currentUser.email;
    }
    var defaultHeaders = {
      "Accept": "application/json",
      "X-User-Token": token,
      "X-User-Email": email,
    };
    $http.defaults.headers.common = defaultHeaders;
  };

  $scope.$on(AUTH_EVENTS.loginSuccess, function () {
    $scope.updateCurrentUser();
    if ($scope.currentUser.force_password_reset === true) {
      $state.go('layout.person.profile', { personId: $scope.currentUser.id });
    } else {
      $state.go('layout.group.rounds', {groupId: 1});
    }
  });

  $scope.$on(AUTH_EVENTS.logoutSuccess, function () {
    $scope.updateCurrentUser();
  });

  $scope.updateCurrentUser();
};

update router navigation on force_password_reset