app.service('CurrentUser', CurrentUser);

function CurrentUser(UserState, Game, FirebaseRef, $firebaseAuth, Error, $rootScope){
    var self = this,
        colors = ['pink', 'green', 'blue', 'red', 'orange'],
        avatars = ['fish', 'cat', 'bird', 'snail'],
        color = colors[Math.floor(Math.random() * colors.length)],
        avatar = avatars[Math.floor(Math.random() * avatars.length)];


    function setDetails(name, id){
        var user = {};
        self.name = user.name = name;
        self.score = user.score = 0;
        self.ready = user.ready = false;
        self.color = user.color = color;
        self.id = user.id = id;
        self.avatar = user.avatar = avatar;
        return user;
    }

    this.toggleReady = function(status){
        this.ready = status;
        this.ref.update({ready: status});
    }

    this.setUser = function(name){
        $firebaseAuth(FirebaseRef).$authAnonymously().then(function(authData) {
            var user = setDetails(name, authData.uid);

            console.log("Logged in as:", authData.uid);
            self.ref = Game.ref.child('players').child(authData.uid);
            self.ref.set(user);
            self.ref.onDisconnect().remove();

            UserState.state = 'registered';
        })
         .catch(function(error) {
          console.error("Authentication failed:", error);
          Error.message = "Could not register user. Please try again.";
          if(!$rootScope.$$phase) $rootScope.$apply();
        });
    }

    this.chooseAnswer = function(answer){
        self.answer = answer;
        self.ref.update({answer: answer});
    };

    this.addScore = function(points){
        self.score += points;
        self.ref.update({score: self.score});
    }

    this.ref = null;
}

@@ -705,32 +705,35 @@
 rn user;%0A    %7D%0A%0A
+%0A%0A%0A
     this.toggleR
@@ -1002,24 +1002,393 @@
 Data.uid);%0A%0A
+            // F%C3%B6rs%C3%B6ka g%C3%B6ra s%C3%A5 att man inte kan f%C3%A5 samma avatar + f%C3%A4rg som n%C3%A5n annan%0A            // var index = colors.indexOf(user.color);%0A            // colors.splice(index, 1);%0A            // console.log(colors);%0A            //%0A            // var index2 = avatars.indexOf(user.avatar);%0A            // avatars.splice(index2, 1);%0A            // console.log(avatars);%0A%0A
             

Test code for not getting same avatar twice