import DS from 'ember-data'
export default DS.Model.extend
  update_revision: 0

  manager_ids: DS.attr(defaultValue: -> [])
  name:       DS.attr('string')
  identifier: DS.attr('string')
  start_date: DS.attr('moment')
  end_date:   DS.attr('moment')
  percent_done: DS.attr('number', defaultValue: 0)
  expected_number_of_workflow_instances: DS.attr('number')
  instance_scope: DS.attr('string', defaultValue: 'instance')
  batch_size_lower_value: DS.attr('number', defaultValue: 0)
  batch_size_upper_value: DS.attr('number', defaultValue: 200)
  reference_date_day_of_week: DS.attr('string')

  #organizations:               DS.hasMany('organization', inverse: 'projects', serialize: 'ids')
  workflow_groups:   DS.hasMany('panda/workflow-group')
  users:                       DS.hasMany('user',                     serialize: 'ids')
  workflow_instance_type:      DS.belongsTo('panda/workflow-instance-type', inverse: 'projects')
  tracker_subjects:            DS.hasMany('panda/tracker-subject')
  workflow_action_definitions: DS.hasMany('panda/workflow-action-definition', inverse: 'project')
  project_scenarios:           DS.hasMany('panda/project-scenario', inverse: 'project')

  workflowGroupSort: ['position:asc']
  sorted_workflow_groups: Ember.computed.sort('workflow_groups', 'workflowGroupSort')
  sorted_workflow_action_definitions: Ember.computed 'workflow_groups.@each.position', 'workflow_action_definitions.@each.position', ->
    @get('workflow_action_definitions').map((r) ->
      base = r.get('workflow_group.position')
      base = 100 unless Number.isFinite(base) # more than 100 groups is stupid, more than 200 actions in a group as well
      r.set('absolute_sorter', (200 * base) + r.get('position'))
      r
    ).sortBy 'absolute_sorter'
  unassigned_workflow_action_definitions: Ember.computed 'workflow_action_definitions.@each.workflow_group.id', ->
    @get('workflow_action_definitions').rejectBy('workflow_group.id').sortBy 'title'

  instance_scope_batch: Ember.computed.equal 'instance_scope', 'batch'

  project_instances: DS.hasMany('panda/project-instance', inverse: 'project')
  project_instance_groups: DS.hasMany('panda/project-instance-group', inverse: 'project')
  project_instance_actions: DS.hasMany('panda/project-instance-action', inverse: 'project')

  project_scenarios_sort: ['name:asc']
  sorted_project_scenarios: Ember.computed.sort 'project_scenarios', 'project_scenarios_sort'

  #unassigned_workflow_action_definitions: Ember.computed ->
  #  promise = new Ember.RSVP.Promise (resolve) =>
  #    @get('workflow_action_definitions').then (workflow_action_definitions) ->
  #      Ember.RSVP.all(workflow_action_definitions.mapBy 'workflow_group').then ->
  #        resolve workflow_action_definitions.sortBy 'title'
  #  DS.PromiseArray.create
  #    promise: promise
  #unassigned_workflow_action_definitions: ( ->
  #  [@store.createRecord('panda/workflow-action-definition', id: 'asdf', title: 'asdf2')]
  #).property()
  #unassigned_workflow_action_definitions: Ember.computed ->
  #  promise = @get('workflow_action_definitions')
  #  promise.then (workflow_action_definitions) ->
  #    workflow_action_definitions.rejectBy('workflow_group').sortBy 'title'
  #  promise
  #unassigned_workflow_action_definitions: Ember.computed ->
  #  DS.PromiseArray.create
  #    promise: Ember.RSVP.filter @get('workflow_action_definitions'), (workflow_action_definitions) ->
  #      debugger
  #      workflow_action_definitions.sortBy 'title'

  #@get('workflow_action_definitions').rejectBy('workflow_group.id').sortBy 'title'

