 
 # The controller helper methods.
 ctlrs.factory 'ControllerHelper', ['$location', ($location) ->
-  # Fetches a parent object's detail and apply the given
-  # action function.
-  getDetail: (obj, resource, action) ->
-    resource.detail(id: obj.detail).$promise
-      .then (detail) ->
-        if action
-          action(detail, obj)
-        obj
-
-  # Copies the given source object properties into the
-  # destination object.
-  copyContent: (source, dest) ->
-    # Copy the detail properties into the parent object.
-    srcProps = Object.getOwnPropertyNames(source)
-    destProps = Object.getOwnPropertyNames(dest)
-    fields = _.difference(srcProps, destProps)
-    for field in fields
-      dest[field] = source[field]
-
   # Replaces the browser URL search parameters to include at most
   # a non-default project, since all other search parameters are
   # redundant.

]


ctlrs.controller 'SubjectListCtrl', ['$rootScope', '$scope', '$state', '$stateParams', 'Subject',
  ($rootScope, $scope, $state, $stateParams, Subject) ->
    $rootScope.project = $stateParams.project
    project = $rootScope.project or 'QIN'
    params = project: project

    # Export the deferred subjects REST promise to the scope.
    $scope.subjects = Subject.query(params)

    # When the subjects are loaded. then export the collections
    # to the scope.
    $scope.subjects.$promise
      .then (subjects) ->
        # The unique subject collections.
        $scope.collections = _.uniq _.map(
          $scope.subjects,
          (subject) -> subject.collection
        )
]


#
# TODO - get subject from state resolve.
#
ctlrs.controller 'SubjectDetailCtrl', ['$rootScope', '$scope', '$stateParams',
  'ControllerHelper', 'Subject', 'Helpers',
  ($rootScope, $scope, $stateParams, ControllerHelper, Subject, Helpers) ->
    $rootScope.project = $stateParams.project
    # Compose a subject from the route parameters.
    subject =
      project: $stateParams.project or 'QIN'
      collection: _.str.capitalize($stateParams.collection)
      number: parseInt($stateParams.subject)
      detail: $stateParams.detail

    $scope.toggleModelingFormat = ->
      if $scope.modelingFormat == 'Chart'
        $scope.modelingFormat = 'Table'
      else if $scope.modelingFormat == 'Table'
        $scope.modelingFormat ='Chart'
      else
        throw "Modeling format is not recognized: " + $scope.modelingFormat
    
    # Adds the session links above the line.
    $scope.addSessionDetailLinks = (chart) ->
      # @returns the link to detail page for the given session
      sessionDetailLink = (session) ->

      # TODO - draw the x axis line.
      line = svg.append('line')
    
    # Adds the subject content as follows:
    # * Converts the subject detail session and encounter
    #   dates into moments, if necessary 
    # * copies the detail properties into the subject 
    # * sets the subject multiSession flag
    # * sets the modelingFormat to 'Chart' if there is more
    #   than one session, 'List' otherwise
    #
    # @param session the session object
    # @param detail the detail object
    addDetail = (subject, detail) ->
      # The default modeling format is 'Chart' for more than
      # one session, 'List' otherwise.
      defaultModelingFormat = (subject) ->
        if subject.sessions.length > 1 then 'Chart' else 'List'

      # Fix the session and encounter dates.
      Helpers.fixDate(sess, 'acquisition_date') for sess in detail.sessions
      Helpers.fixDate(enc, 'date') for enc in detail.encounters
      
      # Copy the detail content into the subject.
      Helpers.copyContent(detail, subject)
      
      # Flag indicating whether there is more than one session.
      subject.multiSession = subject.sessions.length > 1
      
      # The modeling display format, Chart or List.
      $scope.modelingFormat = defaultModelingFormat(subject)

    # Add the detail to the subject.
    addDetail(subject, detail)
    
    # Place the subject in the scope.
    $scope.subject = subject

    ControllerHelper.cleanBrowserUrl(subject.project)
]
