'use strict';

var _      = require('lodash');
var Reflux = require('reflux');
var moment = require('moment');

var api = require('data/api');

var DataActions = require('actions/DataActions');

function melt (d) {
  var base = _.omit(d, 'indicators');

  return _.map(d.indicators, function (i) {
    return _.assign({
        indicator : i.indicator,
        value     : i.value
      }, base);
  });
}

var DataStore = Reflux.createStore({
  listenables : [DataActions],

  init : function () {
    this.loading = false;
    this.data    = [];
  },

  getInitialState : function () {
    return {
      loading : this.loading,
      data    : this.data
    };
  },

  onClear : function () {
    this.loading = false;
    this.data    = [];

    this.trigger({
      loading : false,
      data    : []
    });
  },

  onFetch : function (campaign, region, charts) {
    var m     = moment(campaign.start_date, 'YYYY-MM-DD');
    var end   = campaign.end_date;

    var promises = _.map(charts, function (def) {
      var q = {
        indicator__in  : def.indicators,
        campaign_end   : end
      };

      // If no timeRange or startOf property is provided, the chart should fetch
      // data for all time.
      if (!_.isNull(_.get(def, 'timeRange', null)) || def.hasOwnProperty('startOf')) {
        q.campaign_start = m.clone()
          .startOf(def.startOf)
          .subtract(def.timeRange)
          .format('YYYY-MM-DD');
      }

      switch (def.regions) {
        case 'subregions':
          q.parent_region__in = region.id;
          break;

        case 'type':
          var parent = _.get(region, 'parent.id');
          if (!_.isUndefined(parent)) {
            q.parent_region__in = parent;
          }

          q.region_type = region.region_type;
          break;
        default:
          q.region__in = region.id;
          break;
      }

      if (def.level) {
        q.level = def.level;
      }

      return api.datapoints(q);
    });

    Promise.all(promises).then(function (responses) {
      this.data = _(responses)
        .pluck('objects')
        .flatten()
        .map(melt)
        .flatten()
        .value();

      this.loading = false;

      this.trigger({
        loading : false,
        data    : this.data
      });
    }.bind(this));

    this.loading = true;
    this.data    = [];

    this.trigger({
      loading : true,
      data    : []
    });
  }

});

module.exports = DataStore;

@@ -13,21 +13,16 @@
 ;%0A%0Avar _
-     
  = requi
@@ -196,17 +196,16 @@
 ion melt
- 
 (d) %7B%0A  
@@ -278,17 +278,16 @@
 function
- 
 (i) %7B%0A  
@@ -304,34 +304,32 @@
 .assign(%7B%0A      
-  
 indicator : i.in
@@ -321,17 +321,16 @@
 ndicator
- 
 : i.indi
@@ -346,20 +346,13 @@
     
-  
 value
-     
 : i.
@@ -357,18 +357,16 @@
 i.value%0A
-  
     %7D, b
@@ -430,17 +430,16 @@
 tenables
- 
 : %5BDataA
@@ -454,17 +454,16 @@
 %0A%0A  init
- 
 : functi
@@ -456,33 +456,32 @@
   init: function
- 
 () %7B%0A    this.lo
@@ -505,27 +505,24 @@
   this.data 
-   
 = %5B%5D;%0A  %7D,%0A%0A
@@ -538,17 +538,16 @@
 ialState
- 
 : functi
@@ -540,33 +540,32 @@
 lState: function
- 
 () %7B%0A    return 
@@ -579,17 +579,16 @@
  loading
- 
 : this.l
@@ -597,36 +597,32 @@
 ding,%0A      data
-    
 : this.data%0A    
@@ -639,17 +639,16 @@
  onClear
- 
 : functi
@@ -649,17 +649,16 @@
 function
- 
 () %7B%0A   
@@ -686,35 +686,32 @@
 ;%0A    this.data 
-   
 = %5B%5D;%0A%0A    this.
@@ -725,33 +725,32 @@
 (%7B%0A      loading
- 
 : false,%0A      d
@@ -744,36 +744,32 @@
 alse,%0A      data
-    
 : %5B%5D%0A    %7D);%0A  %7D
@@ -780,17 +780,16 @@
  onFetch
- 
 : functi
@@ -790,17 +790,16 @@
 function
- 
 (campaig
@@ -829,20 +829,16 @@
   var m 
-    
 = moment
@@ -886,18 +886,16 @@
 var end 
-  
 = campai
@@ -949,17 +949,16 @@
 function
- 
 (def) %7B%0A
@@ -994,18 +994,16 @@
 ator__in
-  
 : def.in
@@ -1032,19 +1032,16 @@
 aign_end
-   
 : end%0A  
@@ -1957,17 +1957,16 @@
 function
- 
 (respons
@@ -2169,17 +2169,16 @@
  loading
- 
 : false,
@@ -2190,20 +2190,16 @@
     data
-    
 : this.d
@@ -2271,19 +2271,16 @@
 is.data 
-   
 = %5B%5D;%0A%0A 
@@ -2310,17 +2310,16 @@
  loading
- 
 : true,%0A
@@ -2328,20 +2328,16 @@
     data
-    
 : %5B%5D%0A   

Format dataStore