(function() {
  'use strict';
 
  module.exports = RegisterController;

  RegisterController.$inject = [
    '$rootScope',
    'AuthService',
    'User',
    'Session',
    '$log',
    '$timeout',
    '$state',
    'Validation'
  ];

  function RegisterController(
    $rootScope,
    AuthService,
    User,
    Session,
    $log,
    $timeout,
    $state,
    Validation) {

    var vm = this;
    vm.newuser = User;
    vm.passwordPrefix = 'registration';
    vm.register = register;
    vm.registerShow = false;
    vm.showRegisterButton = true;
    vm.validation = Validation;

    function register() {
      vm.showRegisterButton = false;

      if (!vm.registerForm.$valid) {
        $rootScope.$emit('notify', $rootScope.MESSAGES.generalError);
        vm.showRegisterButton = true;
        return;
      }

      vm.credentials = {};
      vm.credentials.username = vm.newuser.email;
      vm.credentials.password = vm.newuser.password;

      User.$save().then(successfulRegistration).catch(failedRegistration);
    }

    function failedRegistration(error) {
      vm.processing = false;
      vm.showRegisterButton = true;
      if (error.data && error.data.message === 'Duplicate User') {
        $rootScope.$emit('notify', $rootScope.MESSAGES.emailInUse);
      } else if (error.status === 409) { 
        var message = $rootScope.MESSAGES.registrationContactError;
        message.content = message.content.replace('[contactId]', error.data.message); 
        $rootScope.$emit('notify', message);
      } else {
        $rootScope.$emit('notify', $rootScope.MESSAGES.failedResponse);
      }
    }

    function failedLogin() {
      vm.pending = false;
      vm.loginFailed = true;
      vm.showRegisterButton = true;
    }

    function successfulRegistration() {
      AuthService.login(vm.credentials).then(successfulLogin,failedLogin ).then(function() {
        vm.processing = false;
        vm.showRegisterButton = true;
      });
    }

    function successfulLogin(user) {
      // TODO Refactor this to a shared location for use here and in login_controller
      vm.registerShow = !vm.registerShow;
      $rootScope.showLoginButton = false; //TODO use a service here, avoid using rootscope
      $rootScope.$emit('notify', $rootScope.MESSAGES.successfullRegistration);
      if (vm.registerForm) {
        vm.registerForm.$setPristine();
      }

      vm.newuser = {};
      $timeout(function() {
        if (vm.registerCallback) {
          vm.registerCallback();
        } else if (Session.hasRedirectionInfo()) {
          var url = Session.exists('redirectUrl');
          var params = Session.exists('params');
          Session.removeRedirectRoute();
          if (params === undefined) {
            $state.go(url);
          } else {
            $state.go(url, JSON.parse(params));
          }
        } else {
          $state.go('content', {link:'/getstarted'});
        }
      }, 500);
    }
  }
})();
