'use strict';

/* Services */


// Demonstrate how to register services
// In this case it is a simple value service.
angular.module('myApp.services', [])
  .value('FIREBASE_URL', 'https://waitandeat-jonos.firebaseio.com/')
  .factory('partyService', function($firebase, FIREBASE_URL) {
    var partiesRef = new Firebase(FIREBASE_URL + 'parties')
    var parties = $firebase(partiesRef);

    var partyServiceObject = {
      parties: parties,
      saveParty: function(party) {
      	parties.$add(party);  	
      }
    };

    return partyServiceObject
  })
  .factory('textMessageService', function($firebase, FIREBASE_URL, partyService) {
  	var textMessageRef = new Firebase(FIREBASE_URL + 'textMessages');
    var textMessages = $firebase(textMessageRef);

    var textMessageServiceObject = {
      sendTextMessage: function(party) {
        var newTextMessage = {
		      phoneNumber: party.phone,
		      size: party.size,
		      name: party.name
		    };
		    textMessages.$add(newTextMessage);
		    party.notified = "Yes";
		    partyService.parties.$save(party.$id);
      }
    };
    
    return textMessageServiceObject;
    
    
  })
  .factory('authService', function($firebaseSimpleLogin, $location, $rootScope, FIREBASE_URL) {
  	var authRef = new Firebase(FIREBASE_URL);
    var auth = $firebaseSimpleLogin(authRef);

    var authServiceObject = {
    	register: function(user) {
        auth.$createUser(user.email, user.password).then(function(data) {
      	  console.log(data);
      	  authServiceObject.login(user);
        });
    	},
    	login: function(user) {
    		auth.$login('password', user).then(function(data) {
          console.log(data);
          // Redirect users to /waitlist.
          $location.path('/waitlist');
        });
    	},
    	logout: function () {
        auth.$logout();
        // Redirect users to /.
        $location.path('/');
    	}
    };

    $rootScope.$on("$firebaseSimpleLogin:login", function(e, user) {
		  // Save currentUser on our rootScope.
		  $rootScope.currentUser = user;
		});

		$rootScope.$on("$firebaseSimpleLogin:logout", function() {
		  // Save currentUser on our rootScope.
		  $rootScope.currentUser = null;
		});

    return authServiceObject
  });

@@ -229,21 +229,20 @@
 actory('
-party
+data
 Service'
@@ -281,29 +281,25 @@
 RL) %7B%0A  
-  
+%09
 var 
-parties
+data
 Ref = ne
@@ -325,62 +325,174 @@
 _URL
- + 'parties')%0A    var parties = $firebase(
+);%0A  %09var fireData = $firebase(dataRef);%0A%0A  %09return fireData;%0A  %7D)%0A  .factory('partyService', function(dataService) %7B%0A    var parties = dataService.$child('
 parties
-Ref);
+') 
 %0A%0A  
@@ -703,39 +703,27 @@
 unction(
-$firebase, FIREBASE_URL
+dataService
 , partyS
@@ -730,25 +730,26 @@
 ervice) %7B%0A  
+ 
-%09
+ 
 var textMess
@@ -755,92 +755,32 @@
 sage
-Ref = new Firebase(FIREBASE_URL + 'textMessages');%0A    var textMessages = $firebase(
+s = dataService.$child('
 text
@@ -786,19 +786,18 @@
 tMessage
-Ref
+s'
 );%0A%0A    

put firebase repetitive code into dataService