'use strict';

define(['angular', 'jquery'], function(angular, $) {

  var app = angular.module('portal.notifications.services', []);

  app.factory('notificationsService', ['$q','$http', 'miscService', 'keyValueService','SERVICE_LOC', function($q, $http, miscService, keyValueService, SERVICE_LOC) {
      var filteredNotificationPromise;
      var dismissedPromise;
      var getAllNotifications = function() {
        return $http.get(SERVICE_LOC.notificationsURL, {cache : true}).then(
                                              function(result) {
                                                  return  result.data.notifications;
                                              } ,
                                              function(reason){
                                                  miscService.redirectUser(reason.status, 'notifications json feed call');
                                              }
                                          );
      };
      
      var getDismissedNotificationIds = function() {
        dismissedPromise = dismissedPromise || keyValueService.getValue('notification:dismiss').then(function(data){
      	  if(data && !data.value) { //empty state is {value : ""}
      	    return data;
      	  } else {
            return [];
          }
      	});
      	return dismissedPromise;
      };
      
      var setDismissedNotifications = function(arrayOfIds) {
    		keyValueService.setValue('notification:dismiss',arrayOfIds);
        dismissedPromise = null;
      };

      var getNotificationsByGroups = function() {
        var successFn, errorFn;
        //if it already happened, just return it
        if(filteredNotificationPromise) {
          return filteredNotificationPromise;
        }

        successFn = function(result){
          //post processing
          var groups = result[1], allNotifications = result[0];
          var notificationsByGroup = [];
          $.each(allNotifications, function (index, notification){ //for each notification
            var added = false;
            $.each(notification.groups, function(index, group) { //for each group for that notification
              if(!added) {
                var inGroup = $.grep(groups, function(e) {return e.name === group}).length; //intersect, then get length
                if(inGroup > 0) {//are they in that group?
                  notificationsByGroup.push(notification); //they should see this notification
                  added = true;
                }
              }
            });
          });

          return notificationsByGroup;
        }

        errorFn = function(reason) {
          miscService.redirectUser(reason.status, 'q for filtered notifications');
        }
        
        var groupPromise = $http.get(SERVICE_LOC.groupURL, {cache : true}).then (
                                      function(result){
                                        return result.data.groups;
                                      },function(reason){
                                        miscService.redirectUser(reason.status, 'group json feed call');
                                      });
        

        //setup new q
        filteredNotificationPromise = $q.all([getAllNotifications(), groupPromise]).then(successFn, errorFn);

        return filteredNotificationPromise;
      };

      return {
        getAllNotifications: getAllNotifications,
        getNotificationsByGroups : getNotificationsByGroups,
        getDismissedNotificationIds : getDismissedNotificationIds,
        setDismissedNotifications : setDismissedNotifications
      };

  }]);

  return app;

});

@@ -1155,17 +1155,18 @@
 %7B%0A      
-%09
+  
   if(dat
@@ -1170,17 +1170,23 @@
 data && 
-!
+typeof 
 data.val
@@ -1191,41 +1191,318 @@
 alue
-) %7B //empty state is %7Bvalue : %22%22%7D
+ === 'string') %7B //data and has a value string%0A            if(data.value) %7B //value string contains things%0A              return JSON.parse(data.value);%0A            %7D else %7B //empty state%0A              return %5B%5D;%0A            %7D%0A          %7D else if (data) %7B //data exists, but data.value doesn't, so its just json
 %0A   
@@ -1539,16 +1539,54 @@
 %7D else %7B
+ // null returned, just empty state it
 %0A       

fix parsing