"use strict";

(function () {
    angular
        .module("piadinamia")
        .factory("sharedCartService", sharedCartService);

    sharedCartService.$inject = ["$timeout", "sessionService"];

    function sharedCartService($timeout, sessionService) {
        var service = {
                getCartByItem: getCartByItem,
                getCartByUser: getCartByUser

            },
            cartByUser = {},
            cartByItem = {};

        sessionService.isLogged().then(activate);

        return service;

        function activate(userId) {
            var catalogNameRef = firebase.database().ref("/users/" +
                    userId + "/catalog");

            catalogNameRef.on("value", function (catalogNameSnapshot) {
                var catalogName = catalogNameSnapshot.val();

                var subscribersRef = firebase.database().ref("/users/" +
                        userId + "/catalogs/" + catalogName + "/subscribers");

                subscribersRef.on("value", function (snapshot) {
                    var subscribers = snapshot.val();

                    subscribeCarts(catalogName, subscribers);
                });
            });
        }

        function subscribeCarts(catalogName, subscribers) {
            if (!subscribers) {
                return;
            }

            subscribers.forEach(function (user) {
                var cartRef = firebase.database().ref("/users/" + user.id +
                            "/catalogs/" + catalogName + "/cart");

                cartRef.on("value", function (snapshot) {
                    var cart = snapshot.val();

                    $timeout(function () {
                        calcCartByUser(user, cart);
                        calcCartByItem();
                    });
                });
            });
        }

        function calcCartByUser(user, cart) {
            var myCart = [],
                total = 0;

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

            cartByUser[user.id] = {
                name: user.name || "Myself",
                total: total,
                cart: myCart
            };
        }

        function calcCartByItem() {
            cartByItem = {};

            angular.forEach(cartByUser, function (user) {
                user.cart.forEach(function (item) {
                    var counter = 0;

                    if (cartByItem[item.item]) {
                        counter = cartByItem[item.item].total + item.qty;
                    } else {
                        counter = item.qty;
                    }

                    cartByItem[item.item] = {
                        total: counter
                    };
                });
            });
        }

        function getCartByItem() {
            return cartByItem;
        }

        function getCartByUser() {
            return cartByUser;
        }

    }

}());

@@ -1301,32 +1301,95 @@
 %0A            %7D%0A%0A
+            reset(cartByUser);%0A            reset(cartByItem);%0A%0A
             subs
@@ -1414,32 +1414,32 @@
 nction (user) %7B%0A
-
                 
@@ -1711,24 +1711,64 @@
 nction () %7B%0A
+                        if (cart) %7B%0A    
             
@@ -1823,32 +1823,36 @@
                 
+    
 calcCartByItem()
@@ -1853,16 +1853,42 @@
 Item();%0A
+                        %7D%0A
         
@@ -2411,32 +2411,38 @@
 ) %7B%0A            
+reset(
 cartByItem = %7B%7D;
@@ -2427,37 +2427,33 @@
 reset(cartByItem
- = %7B%7D
+)
 ;%0A%0A            a
@@ -3069,24 +3069,24 @@
 tByUser() %7B%0A
-
             
@@ -3111,24 +3111,234 @@
 %0A        %7D%0A%0A
+        function reset(obj) %7B%0A            var key;%0A%0A            for (key in obj) %7B%0A                if (obj.hasOwnProperty(key)) %7B%0A                    delete obj%5Bkey%5D;%0A                %7D%0A            %7D%0A        %7D%0A%0A
     %7D%0A%0A%7D());

Fix reset shared cart on catalog change.