'use strict';

module.exports = function(app) {
  app.controller('GamesController', ['$rootScope', '$http', '$location', '$route', 'AuthService', 'UserService', function($rs, $http, $location, $route, AuthService, UserService) {

    AuthService.checkSessionExists();
    this.user = $rs.user;

    this.game = {};
    this.game.players = [];
    this.game.players[0] = $rs.user;

    this.friendsList = [];
    this.allGames = $rs.user.gameIds;

    this.createGame = function(gameData) {
      new Promise((resolve, reject) => {
        $http.post('/games/create', gameData)
          .then((game) => {
            let playersArray = game.data.players;
            playersArray.forEach((player) => {
              this.updatePlayer(player)
                .then((playerData) => {
                  if (playerData.data.email === $rs.user.email) {
                    $rs.user = playerData.data;
                    window.sessionStorage.setItem('currentUser', JSON.stringify($rs.user));
                  }
                  $route.reload();
                  resolve();
                })
                .catch((err) => {
                  alert('error creating game');
                  reject();
                });
            });
            resolve();
          })
          .catch((err) => {
            alert('error creating game');
            reject();
          });
      })
    };

    this.getGames = function(emailOrUsername) {
      new Promise((resolve, reject) => {
        $http.get('/games/all')
          .then((games) => {
            resolve(games);
          })
          .catch((err) => {
            alert('error getting games');
            reject();
          });
      })
    };

    this.getFriendsList = function() {
      $http.get('/friends/list')
        .then((friendsList) => {
          friendsList.data.forEach((friend) => {
            delete friend.password;
            delete friend.$$hashKey;
          });
          this.friendsList = friendsList.data;
        })
        .catch((err) => {
          alert('error getting friends list');
        });
    };

    this.addPlayer = function(user) {
      if (user === undefined || user === null) return;
      user = JSON.parse(user);
      // delete user.password;
      this.game.players.push(user);
      this.friendsList = this.friendsList.filter((friend) => {
        return friend._id !== user._id;
      });
    };

    this.removePlayer = function(user) {
      let playersArray = this.game.players;
      let userIndex = playersArray.indexOf(user);
      this.friendsList.push(playersArray[userIndex]);
      playersArray.splice(userIndex, 1);
    };

    this.updatePlayer = function(player) {
      return new Promise((resolve, reject) => {
        let playerData = {
          emailOrUsername: player.email,
        };
        $http.post('/users', playerData)
          .then((user) => {
            UserService.calcHandicap(user.data)
              .then((handicap) => {
                let handicapData = {
                  handicap: handicap,
                };
                UserService.updateUser(playerData, handicapData)
                  .then((user) => {
                    resolve(user);
                  })
                  .catch(reject);
              })
              .catch(reject);
          })
          .catch(reject);
      });
    };

  }]);
};

@@ -257,24 +257,25 @@
 onExists();%0A
+%0A
     this.use
@@ -280,33 +280,32 @@
 ser = $rs.user;%0A
-%0A
     this.game = 
@@ -301,129 +301,132 @@
 his.
-g
+allG
 ame
+s
  = 
-%7B%7D;%0A    this.game.players = %5B%5D;%0A    this.game.players%5B0%5D = $rs.user;%0A%0A    this.friendsList = %5B%5D;%0A    this.allGames
+$rs.user.gameIds;%0A    this.friendsList = %5B%5D;%0A    this.game = %7B%0A      players: %5B%5D,%0A    %7D;%0A    this.game.players%5B0%5D
  = $
@@ -432,24 +432,16 @@
 $rs.user
-.gameIds
 ;%0A%0A    t
@@ -482,51 +482,8 @@
 ) %7B%0A
-      new Promise((resolve, reject) =%3E %7B%0A  
     
@@ -522,34 +522,32 @@
 meData)%0A        
-  
 .then((game) =%3E 
@@ -544,26 +544,24 @@
 (game) =%3E %7B%0A
-  
           le
@@ -602,26 +602,24 @@
 ;%0A          
- 
- 
 playersArray
@@ -649,26 +649,24 @@
             
-  
 this.updateP
@@ -679,18 +679,16 @@
 player)%0A
-  
         
@@ -729,26 +729,24 @@
             
-  
 if (playerDa
@@ -799,18 +799,16 @@
         
-  
 $rs.user
@@ -845,18 +845,16 @@
         
- 
- 
 window.s
@@ -937,14 +937,10 @@
     
-  %7D%0A  
+%7D%0A
     
@@ -972,39 +972,8 @@
 ();%0A
-                  resolve();%0A  
     
@@ -977,34 +977,32 @@
              %7D)%0A
-  
               .c
@@ -1025,34 +1025,32 @@
                 
-  
 alert('error cre
@@ -1067,38 +1067,8 @@
 ');%0A
-                  reject();%0A  
     
@@ -1073,34 +1073,32 @@
             %7D);%0A
-  
           %7D);%0A  
@@ -1099,33 +1099,8 @@
 %7D);%0A
-            resolve();%0A  
     
@@ -1098,34 +1098,32 @@
  %7D);%0A        %7D)%0A
-  
         .catch((
@@ -1124,34 +1124,32 @@
 atch((err) =%3E %7B%0A
-  
           alert(
@@ -1184,44 +1184,11 @@
     
-    reject();%0A          %7D);%0A      
 %7D)
+;
 %0A   
@@ -2026,39 +2026,8 @@
 r);%0A
-      // delete user.password;%0A
     

refactor variable declarations. remove promise from this.createGame function