import Ember from 'ember'
import {create_date_sort} from 'ember-cli-dunlop/utils/date-sort'
{get} = Ember
target_date_sort = create_date_sort('target_date')
export default Ember.Controller.extend
  #filter: Ember.Object.create
  #  project_scenario_id_in: []
  #  state_in: []
  #  reference_date_gteq: null
  #  reference_date_lteq: null
  #filter_project_scenarios: Ember.computed 'filter.project_scenario_id_in.[]', ->
  #   @get('model.sorted_project_scenarios').filter((r) => r.get('id') in @get('filter.project_scenario_id_in'))
  #reference_date_filter_active: Ember.computed.or 'filter.{reference_date_gteq,reference_date_lteq}'
  #reference_date_filter_text: Ember.computed 'filter.reference_date_gteq', 'filter.reference_date_lteq', ->
  #  if gteq = @get('filter.reference_date_gteq')
  #    gteq = gteq.format('ll')
  #  else
  #    gteq = '-'
  #  if lteq = @get('filter.reference_date_lteq')
  #    lteq = lteq.format('ll')
  #  else
  #    lteq = '-'
  #  "#{gteq} / #{lteq}"
  #filtered_project_instances: Ember.computed 'model.project_instances.@each.id', 'filter.{project_scenario_id_in.length,state_in.length,reference_date_gteq,reference_date_lteq}', ->
  #  base = @get('model.project_instances').toArray()
  #  if @get('filter.project_scenario_id_in.length')
  #    base = base.filter (record) => record.get('project_scenario.id') in @get('filter.project_scenario_id_in')
  #  if reference_date_gteq = @get('filter.reference_date_gteq')
  #    base = base.filter (record) -> if reference_date = record.get('reference_date') then reference_date.toISOString().substring(0, 10) >= reference_date_gteq.toISOString().substring(0, 10) else false
  #  if reference_date_lteq = @get('filter.reference_date_lteq')
  #    base = base.filter (record) -> if reference_date = record.get('reference_date') then reference_date.toISOString().substring(0, 10) <= reference_date_lteq.toISOString().substring(0, 10) else false
  #  if @get('filter.state_in.length')
  #    base = base.filter (record) => record.get('state') in @get('filter.state_in')
  #  base.sort date_sort
  apply_filters: -> # empty since handled inline
  filters: Ember.Object.create
    state_in: []
    workflow_group_name_cont: ''
    notes_cont: ''
    workflow_action_definition_title_cont: ''
    target_date_lteq: null
    target_date_gteq: null

  filtered_project_instance_actions: Ember.computed 'model.project_instance_actions.[]', 'filters.filter_calls', ->
    return [] unless actions = @get('model.project_instance_actions')?.toArray()
    if @get('filters.state_in.length')
      actions = actions.filter (record) => record.get('state') in @get('filters.state_in')
    if group_name_filter = @get('filters.workflow_group_name_cont')
      matcher = new RegExp(group_name_filter, 'i')
      actions = actions.filter (record) -> matcher.test record.get('project_instance_group.workflow_group.name')
    if title_filter = @get('filters.workflow_action_definition_title_cont')
      matcher = new RegExp(title_filter, 'i')
      actions = actions.filter (record) -> matcher.test record.get('workflow_action_definition.title')
    if notes_filter = @get('filters.notes_cont')
      matcher = new RegExp(notes_filter, 'i')
      actions = actions.filter (record) -> matcher.test record.get('notes')
    actions.sort target_date_sort

  actions:
    edit_project_instance_action: (project_instance_action) ->
      @modal 'project/edit-instance-action', model: project_instance_action
