app.service('$graph', function ($http, inform) {
  this.get = function (namespace, collection, query, obj, seriesKey, dataKey, labelsKey, callback) {
    var params = {
      query: query,
      obj: obj,
      seriesKey: seriesKey,
      dataKey: dataKey,
      labelsKey: labelsKey
    };

    $http.post('/api/' + namespace + '/graph/' + collection, params)
      .then(function (response) {
        response.data.options = {
          responsive: true,
          responsiveAnimationDuration: 1500,
          /*legend: {
            display: true
          },*/
          tooltip: {
            enabled: true
          },
          elements: {
            line: {
              tension: 0,
              stepped: true
            }
          }
        };
        callback(response.data);
      })
      .catch(function (response) {
        var error;
        if (response.data) {
          error = response.data.message || response.data;
        }
        else {
          error = 'O servidor excedeu o tempo limite de processamento, por favor tente novamente mais tarde';
        }
        inform.add(error, { ttl: 2000, type: "error" });
        callback();
      });
  };
});
