import Ember from 'ember'
{get} = Ember
export default Ember.Route.extend
  session: Ember.inject.service('session')
  model: ->
    project = @modelFor('project.show')
    project.reload() unless @controllerFor('application').get('currentRouteName') in ['loading', 'project.show.dunlop', 'project.show.analytics']
    #TODO: implement with ember-custom-actions if v2 is released
    session = @get('session')
    model_name = 'panda/project'
    base_url = @store.adapterFor(model_name).buildURL(model_name)
    url = base_url + "/#{project.id}/analytics"
    request =
      url: url
      type: 'GET'
      contentType: "application/json"
      dataType: "json"
    if get(session, 'isAuthenticated')
      session.authorize 'authorizer:devise', (key, authorization) ->
        request.crossDomain = true
        request.headers ||= {}
        request.headers[key] = authorization
    Ember.RSVP.hash
      project: project
      analytics: Ember.$.ajax(request)

  setupController: (controller, model) ->
    controller.set 'project', @modelFor('project.show')
    @_super arguments...
