(function() {
  'use strict';

  angular
    .module('app.auth')
    .controller('AuthController', AuthController);

  AuthController.$inject = ['$location', 'authService'];

  function AuthController($location, authService) {
    var vm = this;

    vm.register = register;
    vm.login = login;


    function register(user) {
      return authService.register(user)
        .then(function() {
          return vm.login(user);
        })
        .then(function(resp) {
          return authService.saveUserInfo(resp.uid, user);
        })
        .catch(function(error) {
          vm.error = error;
        });
    }

    function login(user) {
      return authService.login(user)
        .then(function(response) {
          $location.path('/waitlist');
          return response;
        })
        .catch(function(error) {
          vm.error = error;
        });
    }

  }

})();