import Ember from 'ember'
import {create_date_sort} from 'ember-cli-dunlop/utils/date-sort'
date_sort = create_date_sort('date')
export default Ember.Controller.extend
  organizations: ( -> @store.peekAll('panda/organization')).property()
  queryParams: ['state', 'organization_id']
  #filter: Ember.Object.create
  #  workflow_action_definition_id_in: []
  #  project_instance_group_workflow_group_id_in: []
  #  state_in: []
  #  date_gteq: null
  #  date_lteq: null
  filtered_project_instance_actions_inline: Ember.computed 'project.project_instance_actions.@each.id', 'filter.{workflow_action_definition_id_in.length,state_in.length,project_instance_group_workflow_group_id_in.length,date_gteq,date_lteq}', ->
    base = @get('project.project_instance_actions').toArray()
    if @get('filter.workflow_action_definition_id_in.length')
      base = base.filter (record) => record.get('workflow_action_definition.id') in @get('filter.workflow_action_definition_id_in')
    if @get('filter.project_instance_group_workflow_group_id_in.length')
      base = base.filter (record) => record.get('project_instance_group.workflow_group.id') in @get('filter.project_instance_group_workflow_group_id_in')
    if date_gteq = @get('filter.date_gteq')
      base = base.filter (record) -> if date = record.get('date') then date.toISOString().substring(0, 10) >= date_gteq.toISOString().substring(0, 10) else false
    if date_lteq = @get('filter.date_lteq')
      base = base.filter (record) -> if date = record.get('date') then date.toISOString().substring(0, 10) <= 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_instance_actions: Ember.computed.alias 'model'

  filter_query: Ember.computed 'filters.filter_calls', ->
    @get('filters')?.toProperties() || {}
  export_human_url: Ember.computed 'project.id', 'filter_query', ->
    model_name = 'panda/project-instance-action'
    base_url = @store.adapterFor(model_name).buildURL(model_name)
    filter_addition = Ember.$.param q: @get('filter_query')
    "#{base_url}/export_human.csv?#{filter_addition}"
  export_technical_url: Ember.computed 'project.id', 'filter_query', ->
    model_name = 'panda/project-instance-action'
    base_url = @store.adapterFor(model_name).buildURL(model_name)
    filter_addition = Ember.$.param q: @get('filter_query')
    "#{base_url}/export_technical.csv?#{filter_addition}"

  #date_filter_active: Ember.computed.or 'filter.{date_gteq,date_lteq}'
  #date_filter_text: Ember.computed 'filter.date_gteq', 'filter.date_lteq', ->
  #  if gteq = @get('filter.date_gteq')
  #    gteq = gteq.format('ll')
  #  else
  #    gteq = '-'
  #  if lteq = @get('filter.date_lteq')
  #    lteq = lteq.format('ll')
  #  else
  #    lteq = '-'
  #  "#{gteq} / #{lteq}"

  filter_workflow_action_definitions: Ember.computed 'filters.workflow_action_definition_id_in.[]', ->
     @get('project.workflow_action_definitions').filter((r) => r.get('id') in @get('filters.workflow_action_definition_id_in'))
  workflow_action_definitions_filter_html: Ember.computed 'filters.workflow_action_definition_id_in.[]', ->
    length = @get('filters.workflow_action_definition_id_in.length')
    return '<i class="filter icon"></i>' unless length
    tooltip = @get('project.workflow_action_definitions').filter((r) => r.get('id') in @get('filters.workflow_action_definition_id_in')).mapBy('title').join("&#60;br&#62;")
    model_name = if length is 1 then t('models.workflow_action_definition') else t('models.plural.workflow_action_definition')
    "<span data-tooltip='#{tooltip}' data-position='bottom center' data-html='true'>#{length} #{model_name}</span>"

  actions:
    toggle_filter: (key, value, checked) ->
      new_value = []
      if checked
        new_value = new_value.concat(@get("filters.#{key}")).concat(value)
      else
        @get("filters.#{key}").forEach (present_in_filter) ->
          new_value.push present_in_filter unless present_in_filter is value
      @set "filters.#{key}", new_value
      query = @get('filters')?.toProperties() || {}
      @set('model.page', 1)
      @set('model.lastPage', null)
      @get('model').setOtherParam('q', query)
      false

    #toggle_filter: (key, value, checked) ->
    #  if checked
    #    @get("filters.#{key}").addObject value
    #  else
    #    @get("filters.#{key}").removeObject value
    #  query = @get('filters')?.toProperties() || {}
    #  @get('model').setOtherParam('q', query)
    clear_filter: (keys...) ->
      keys.forEach (key) =>
        if Ember.isArray(@get("filters.#{key}"))
          @set "filters.#{key}", []
        else
          @set "filters.#{key}", null
      query = @get('filters')?.toProperties() || {}
      @set('model.page', 1)
      @set('model.lastPage', null)
      @get('model').setOtherParam('q', query)
      false
    #set_filter_date_today: ->
    #  today = moment()
    #  @setProperties
    #    'filters.target_date_gteq': today
    #    'filters.target_date_lteq': today
    #set_filter_date_this_week: ->
    #  today = moment()
    #  @setProperties
    #    'filters.target_date_gteq': today.clone().startOf('isoWeek')
    #    'filters.target_date_lteq': today.endOf('isoWeek')
