import Ember from 'ember'
import {create_date_sort} from 'ember-cli-dunlop/utils/date-sort'
{get} = Ember
date_sort = create_date_sort('reference_date')
export default Ember.Controller.extend
  queryParams: ['state']
  #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
  filtered_project_instances: Ember.computed.alias 'model'
  actions:
    toggle_filter: (key, value, checked) ->
      if checked
        @get("filter.#{key}").addObject value
      else
        @get("filter.#{key}").removeObject value
    clear_filter: (key) ->
      if Ember.isArray(@get("filter.#{key}"))
        @set "filter.#{key}", []
      else
        @set "filter.#{key}", null
