myApp.service('DealRetrievalService', ['$http', function($http) {
  var _this = this;
  return {
    removeInactiveDeals: function (deals) {
      var newDeals = [];
      _.forEach(deals, function (deal) {
        if(deal.status === 'Active'){
          newDeals.push(deal);
        }
      });
      return newDeals;
    },

    removeExpiredDeals: function (deals) {
      var newDeals = [];
      _.forEach(deals.data, function (deal) {
        var validTo = Date.parse(deal.valid_to);
        var today = new Date();
        if(validTo > today){
          newDeals.push(deal);
        }
      });
      return _this.removeInactiveDeals(newDeals);
    },

    getAllDeals: function() {
      return $http.get('/dealRecord')
        .success(function(deals) {
          var temp = _this.removeExpiredDeals(deals.data);
          console.log('deals were:', temp);
          return _this.removeExpiredDeals(deals.data);
        })
        .error(function(err) {
        });
    },

    getRelatedDeals: function (category, retailer) {
      var q = '/dealRecord?status=Active';

      if(category && category !== 'All'){
        q += '&category=' + category;
      }

      if(retailer && retailer !== 'All'){
        q += '&retailer=' + retailer;
      }

      return $http.get(q)
        .success(function(deals) {
          return _this.removeExpiredDeals(deals.data);
        })
        .error(function(err) {
        });
    }
  };
}]);