import Ember from 'ember'
#import config from 'ember-get-config'

export default Ember.Controller.extend
  session: Ember.inject.service('session')
  #filters: {filter_calls: 0} # will be overwritten in route
  #filters: Ember.computed.alias 'model.project_instance_actions.filters'
  scope_project: null
  scope_scenario: null
  scope_organization: null
  projects: ( -> @store.findAll('panda/project')).property()
  sorted_projects: Ember.computed 'projects.@each.name', -> @get('projects').sortBy('name')
  show_completed_at: Ember.computed 'filters.state_in.[]', 'filters.completed_at_gteq', 'filters.completed_at_lteq', 'filters.s', ->
    return true if @get('filters.completed_at_lteq') or @get('filters.completed_at_gteq')
    return true if /^completed_at/.test @get('filters.s')
    JSON.stringify(@get('filters.state_in')) is '["completed"]'
  show_inquiry_at: Ember.computed 'filters.state_in.[]', 'filters.inquiry_at_gteq', 'filters.inquiry_at_lteq', 'filters.s', ->
    return true if @get('filters.inquiry_at_lteq') or @get('filters.inquiry_at_gteq')
    return true if /^inquiry_at/.test @get('filters.s')
    JSON.stringify(@get('filters.state_in')) is '["inquiry"]'
  filter_query: Ember.computed 'filters.filter_calls', ->
    @get('filters')?.toProperties() || {}
  export_human_url: Ember.computed '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'), include_pointing: true
    "#{base_url}/export_human.csv?#{filter_addition}"
  export_technical_url: Ember.computed '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'), include_pointing: true
    "#{base_url}/export_technical.csv?#{filter_addition}"
  actions:
    apply_filters: ->
      @incrementProperty 'filters.filter_calls'
      current_scope_project_id = @get('scope_project.id')
      if project_ids = @get('filters.project_id_in')
        if project_ids.length is 1
          if current_scope_project_id isnt project_ids[0] # only reload if changed
            project = @store.peekRecord('panda/project', project_ids[0]) # projects all pre-loaded
            @set 'filters.workflow_action_definition_title_cont', '' if @get('filters.workflow_action_definition_title_cont') # this field disappears since actual action matching is assumed
            project.reload().then =>
              @set 'scope_project', project
        else if @get('scope_project')
          @set 'scope_project', null
          @set 'filters.project_instance_project_scenario_id_in', []
          @set 'filters.project_instance_group_workflow_group_id_in', []
          @set 'filters.workflow_action_definition_identifier_in', []
      else if @get('scope_project')
        @set 'scope_project', null
        @set 'filters.project_instance_project_scenario_id_in', []
        @set 'filters.project_instance_group_workflow_group_id_in', []
        @set 'filters.workflow_action_definition_identifier_in', []
      @get('model.project_instance_actions')?.reload()
      false
    edit_project_instance_action: (project_instance_action) ->
      @modal 'project/edit-instance-action', model: project_instance_action

    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
      @apply_filters()
    clear_filter: (keys...) ->
      keys.forEach (key) =>
        if Ember.isArray(@get("filters.#{key}"))
          @set "filters.#{key}", []
        else
          @set "filters.#{key}", null
      @apply_filters()
