'use strict';

module.exports = function(app) {
  app.service('GameService', ['$rootScope', '$http', function($rs, $http) {

    this.getById = function(gameId) {
      $http.get($rs.baseUrl + '/games/' + gameId)
        .then((game) => {
          $rs.gameData = game.data;
        })
        .catch((err) => {
          alert('error getting game by id');
        });
    };

    this.getAllByPublicId = function(publicIdArray) {
      return new Promise((resolve, reject) => {
        let publicIdData = {
          publicIdArray: publicIdArray,
        };
        $http.post($rs.baseUrl + '/games/all', publicIdData)
          .then((games) => {
            resolve(games.data);
          })
          .catch(() => {
            alert('error getting games');
            reject();
          });
      });
    };

    this.getAllFriends = function() {
      return new Promise((resolve, reject) => {
        //TODO: change list to all
        $http.get('/friends/list')
          .then((allFriends) => {
            resolve(allFriends.data);
          })
          .catch((err) => {
            alert('error getting friends list');
            reject();
          });
      });
    };

    this.rankFriends = function() {
      return new Promise((resolve, reject) => {
        this.getAllFriends()
          .then((allFriends) => {
            allFriends.push($rs.user);
            this.sortByLowest(allFriends)
              .then((sortedArray) => {
                resolve(sortedArray);
              })
              .catch(reject);
          })
          .catch(reject);
      });
    };

    this.sortByLowest = function(array) {
      return new Promise((resolve, reject) => {
        let dummyUser = {
          fullName: 'N/A',
          handicap: 'N/A',
        };
        if (array.length < 3) {
          if (array.length < 1) reject();
          let emptyPlaces = 3 - array.length;
          for (let i = 0; i < emptyPlaces; i++) {
            array.push(dummyUser);
          }
        }
        for (let i = 1; i < array.length; i++) {
          if (array[i - 1].handicap > array[i].handicap) {
            let temp = array[i - 1];
            array[i - 1] = array[i];
            array[i] = temp;
            i--;
          }
        }
        resolve(array);
      });
    };
  }]);
};

@@ -1763,32 +1763,33 @@
 /A',%0A        %7D;%0A
+%0A
         if (arra
@@ -1991,32 +1991,83 @@
     %7D%0A        %7D%0A
+%0A        let done = true;%0A        while(!done) %7B%0A  
         for (let
@@ -2101,32 +2101,34 @@
 ++) %7B%0A          
+  
 if (array%5Bi - 1%5D
@@ -2168,24 +2168,26 @@
             
+  
 let temp = a
@@ -2199,16 +2199,18 @@
 i - 1%5D;%0A
+  
         
@@ -2246,24 +2246,26 @@
             
+  
 array%5Bi%5D = t
@@ -2285,12 +2285,37 @@
     
-i--;
+  done = false;%0A            %7D
 %0A   
@@ -2315,32 +2315,32 @@
   %7D%0A          %7D%0A
-
         %7D%0A      
@@ -2325,32 +2325,33 @@
     %7D%0A        %7D%0A
+%0A
         resolve(

add logic to sortByLowest function. introduced 'done' variable to check if any swaps have occurred. use of bubble sort algorithm