'use strict';

/*
 * Pretty simple controller for the login screen.
 * Not much else to say about this one!
 */

angular.module('login', ['peerhandler'])

.controller('LoginCtrl', function($scope, $state, $localStorage, $ionicHistory, $translate, apiFactory, modalFactory,  peerFactory, socketFactory, contactsFactory, configFactory) {

        $scope.isLoginButtonDisabled = false;
        var loginCompleted = function (number) {
            socketFactory.connectToServer($localStorage.token)
            .then(function () {
                apiFactory.setApiToken($localStorage.token);
                return peerFactory.connectToPeerJS(number);
            })
            .then(function () {
                // Disable back button so we can't back to login!
                $ionicHistory.nextViewOptions({
                    disableBack: true
                });
                $state.go('tabs.contacts');
            })
            .catch(function (error) {
                $scope.isLoginButtonDisabled = false;
                socketFactory.disconnectFromServer();
                peerFactory.disconnectFromPeerJS();
                console.log(error);
            })
        }
        // Hack so we're disconnected for sure!
        peerFactory.disconnectFromPeerJS();

        // Already got a valid token, we can just log in
        if ($localStorage.user && $localStorage.token) {
            loginCompleted($localStorage.user.phoneNumber);
        } else if ($localStorage.user) {
            $scope.user = $localStorage.user;
        }

        $scope.goToSignUp = function () {
            $state.go('verify', { state: 'register' });
        }

        $scope.goToResetPassword = function () {
            $state.go('verify', { state: 'reset_password' });
        }

        $scope.login = function(user) {
            if (typeof user !== 'undefined' && user.phoneNumber && user.password) {

                $scope.isLoginButtonDisabled = true;

                var number = user.phoneNumber.replace(/[ +]/g, '');
                apiFactory.auth.login(number, user.password)
                    .then(function success(user) {
                        $localStorage.user  = user;
                        $localStorage.token = user.token;
                        loginCompleted(user.phoneNumber);
                    })
                    .catch(function (error) {
                        $scope.isLoginButtonDisabled = false;
                        var name = error.name;

                        if (name.toLowerCase() === apiFactory.errorTypes.GENERIC.UNSPECIFIED_ERROR) {
                            name = 'TIMEOUT_ERROR';
                        }

                        modalFactory.alert($translate.instant('ERROR'), $translate.instant(name));
                    })
            }
        };
});


@@ -437,30 +437,27 @@
         
-socket
+api
 Factory.
 connectT
@@ -452,23 +452,19 @@
 ory.
-connectToServer
+setApiToken
 ($lo
@@ -480,16 +480,18 @@
 e.token)
+;%0A
 %0A       
@@ -499,66 +499,83 @@
     
-.then(function () %7B%0A                apiFactory.setApiToken
+var promises = %5B%5D;%0A%0A            promises.push(socketFactory.connectToServer
 ($lo
@@ -591,16 +591,17 @@
 e.token)
+)
 ;%0A      
@@ -610,19 +610,22 @@
     
- 
-   return 
+promises.push(
 peer
@@ -651,25 +651,27 @@
 erJS(number)
-;
+);%0A
 %0A           
@@ -663,35 +663,58 @@
 );%0A%0A            
-%7D)%0A
+Promise.all(promises)%0A    
             .the
@@ -717,36 +717,47 @@
 .then(function (
-) %7B%0A
+results) %7B%0A    
                 
@@ -822,16 +822,20 @@
         
+    
 $ionicHi
@@ -878,16 +878,20 @@
         
+   
+ 
 disableB
@@ -916,16 +916,20 @@
         
+    
 %7D);%0A    
@@ -932,32 +932,36 @@
                 
+ 
+   
 $state.go('tabs.
@@ -976,35 +976,43 @@
 ');%0A            
+   
+ 
 %7D)%0A
+    
             .cat
@@ -1025,32 +1025,36 @@
 ction (error) %7B%0A
+    
                 
@@ -1083,32 +1083,36 @@
 sabled = false;%0A
+    
                 
@@ -1157,32 +1157,36 @@
                 
+    
 peerFactory.disc
@@ -1221,16 +1221,20 @@
         
+    
 console.
@@ -1241,24 +1241,28 @@
 log(error);%0A
+    
             

Move logging in to use promise.all