// Generated by CoffeeScript 1.12.5
import Ember from 'ember';

import DS from 'ember-data';

export default Ember.Component.extend({
  record: null,
  project_specific: false,
  session: Ember.inject.service('session'),
  classNames: ['project-aggregate-statistics', 'project-aggregate-organization-statistics'],
  statistics: Ember.computed('statistics_data.isFulfilled', 'project_specific', function() {
    var count, d, project, project_id, project_specific, ref, state, states, stats, target;
    d = this.get('statistics_data');
    if (!d.isFulfilled) {
      return {};
    }
    target = {};
    states = ['pending', 'planned', 'active', 'overdue', 'inquiry', 'rejected', 'completed'];
    project_specific = this.get('project_specific');
    if (!project_specific) {
      states.forEach(function(state) {
        return target[state] = 0;
      });
    }
    ref = d.content.data;
    for (project_id in ref) {
      stats = ref[project_id];
      if (project_specific) {
        project = d.content.projects.findBy('id', project_id);
        target[project_id] = {
          project: project,
          states: []
        };
        states.forEach(function(state) {
          return target[project_id].states.push({
            state: state,
            count: stats[state] || 0
          });
        });
      } else {
        for (state in stats) {
          count = stats[state];
          target[state] += count;
        }
      }
    }
    return target;
  }),
  statistics_data: Ember.computed(function() {
    var base_url, model_name, record_id, request, session, store, url;
    if (!(record_id = this.get('record.id'))) {
      return {};
    }
    model_name = 'panda/organization';
    base_url = this.store.adapterFor(model_name).buildURL(model_name);
    url = base_url + "/" + record_id + "/statistics1";
    session = this.get('session');
    store = this.store;
    request = {
      url: url,
      contentType: "application/json",
      dataType: "json"
    };
    if (Ember.get(session, 'isAuthenticated')) {
      session.authorize('authorizer:devise', function(key, authorization) {
        var obj;
        request.crossDomain = true;
        return request.headers = (
          obj = {},
          obj["" + key] = authorization,
          obj
        );
      });
    }
    return DS.PromiseObject.create({
      promise: Ember.RSVP.hash({
        data: Ember.$.ajax(request),
        projects: store.findAll('panda/project', {
          backgroundReload: false
        })
      })
    });
  })
});
