"use strict";

angular.module("piadinamia").factory("cartService",
    ["$firebase", "Firebase", "FBURL",
    function ($firebase, Firebase, FBURL) {
        var myCart = [],
            myCatalogName = "",
            myUserId;

        return {
            init: function (cart, catalogName, userId) {
                myCart = cart;
                myCatalogName = catalogName;
                myUserId = userId;
            },

            getItems: function () {
                return myCart;
            },

            saveItems: function () {
                var fburl = FBURL + "/users/" + myUserId +
                        "/catalogs/" + myCatalogName + "/cart",
                    cartRef = new Firebase(fburl);

                $firebase(cartRef).$set(myCart);

                if (myCart.length === 0) {
                    $firebase(cartRef).$set("");
                }
            },

            getTotalCount: function () {
                var total = 0;

                myCart.forEach(function (item) {
                    total += item.qty;
                });

                return total;
            },

            getTotalPrice: function () {
                var total = 0;

                myCart.forEach(function (item) {
                    total += item.price * item.qty;
                });

                return total;
            },

            addItem: function (item, price, qty) {
                var self = this,
                    found = false;

                qty = qty || 1;

                myCart.every(function (el, index) {
                    if (el.item === item) {
                        found = true;

                        myCart[index].qty += qty;
                        myCart[index].total = myCart[index].price *
                            myCart[index].qty;

                        if (myCart[index].qty === 0) {
                            self.clearItem(index);
                        }

                        return false;
                    }

                    return true;
                });

                if (!found) {
                    myCart.push({
                        item: item,
                        price: price,
                        qty: 1,
                        total: price
                    });
                }

                this.saveItems();
            },

            clearItem: function (index) {
                myCart.splice(index, 1);
                this.saveItems();
            }
        };

    }]);

@@ -184,48 +184,13 @@
     
-myCatalogName = %22%22,%0A            myUserId
+fburl
 ;%0A%0A 
@@ -258,24 +258,54 @@
 , userId) %7B%0A
+                var cartRef;%0A%0A
             
@@ -343,109 +343,266 @@
     
-myCatalogName = catalogName;%0A                myUserId = userId;%0A            %7D,%0A%0A            getItems:
+fburl = FBURL + %22/users/%22 + userId +%0A                    %22/catalogs/%22 + catalogName + %22/cart%22;%0A%0A                cartRef = new Firebase(fburl);%0A%0A                $firebase(cartRef).$on(%22change%22, function() %7B%0A                    $firebase(cartRef).$on(%22loaded%22,
  fun
@@ -600,32 +600,36 @@
 ded%22, function (
+cart
 ) %7B%0A            
@@ -636,21 +636,210 @@
     
+  
-return myCart
+      myCart = %5B%5D;%0A%0A                        angular.forEach(cart, function(item) %7B%0A                            myCart.push(item);%0A                        %7D);%0A                    %7D);%0A                %7D)
 ;%0A  
@@ -864,20 +864,19 @@
         
-save
+get
 Items: f
@@ -908,50 +908,22 @@
     
-var fburl = FBURL + %22/users/%22 + myUserId +
+return myCart;
 %0A   
@@ -923,32 +923,36 @@
 rt;%0A            
+%7D,%0A%0A
             %22/ca
@@ -951,47 +951,32 @@
     
-%22/catalogs/%22 + myCatalogName + %22/cart%22,
+saveItems: function () %7B
 %0A   
@@ -980,35 +980,35 @@
                 
-   
+var
  cartRef = new F

Add cart update.