import Ember from 'ember'
import PagedRemoteArray from 'ember-cli-dunlop/utils/paged-remote-array'
{get} = Ember
export default_filters = Ember.Object.create
  filter_calls: 0
  workflow_action_definition_identifier_in: []
  project_instance_group_workflow_group_id: ''
  project_id: ''
  project_instance_project_scenario_id: ''
  workflow_action_definition_organization_id: ''
  # untill ransack chronological pre joins issue has been fixed force order to first lookup the pre-joined relation since that magically works
  workflow_action_definition_title_cont: ''
  project_instance_client_identifier_cont: ''
  #workflow_action_definition_offset_type_not_eq: 'attached'
  target_date_not_null: 1 # only target date actions
  state_in: []
  target_date_gteq: null
  target_date_lteq: null
  s: 'target_date asc'

export default Ember.Route.extend
  queryParams:
    state:           {refreshModel: true}
    project:         {refreshModel: true}
    action:          {refreshModel: true}
    page:            {refreshModel: true}
    perPage:         {refreshModel: true}
    organization_id: {refreshModel: true}
  modelName: 'panda/project-instance-action'
  setupController: (controller, model) ->
    controller.set('filters', model.project_instance_actions.get('filters'))
    @_super(arguments...)

  model: (params) ->
    scope_key = 'panda/action-list'
    if user_default = @store.peekAll('dunlop/user-filter').find((r) -> r.get('scope_key') is scope_key and r.get('is_default'))
      filters = Ember.Object.create(user_default.get('filters'))
    else
      filters = default_filters
    #@store.findAll('epots/migration-window-planning')
    #includes = [
    #  'workflow_action_definition.~workflow_action_connections'
    #  'workflow_action_definition.~pointing_workflow_action_connections'
    #  'workflow_action_definition.~organization'
    #  'workflow_action_definition.~workflow_action_offsets'
    #  'project_instance_group.~project'
    #  'project_instance_group.~project_instance'
    #  'project_instance_group.~project_instance_actions'
    #  'project_instance_group.workflow_group'
    #  'project_instance.~project'
    #  'project_instance.project_scenario.~project'
    #  'project_instance.project_scenario.~workflow_groups'
    #  'project_instance.project_scenario.~workflow_action_definitions'
    #  'pointing_project_instance_action_connections.project_instance_action.~project'
    #  'pointing_project_instance_action_connections.project_instance_action.~project_instance'
    #  'pointing_project_instance_action_connections.project_instance_action.~project_instance_group'
    #  'pointing_project_instance_action_connections.project_instance_action.~pointing_project_instance_action_connections'
    #  'pointing_project_instance_action_connections.project_instance_action.workflow_action_definition.~workflow_action_connections'
    #  'pointing_project_instance_action_connections.project_instance_action.workflow_action_definition.~pointing_workflow_action_connections'
    #  'pointing_project_instance_action_connections.project_instance_action.workflow_action_definition.~organization'
    #  'pointing_project_instance_action_connections.project_instance_action.workflow_action_definition.~workflow_action_offsets'
    #]
    filters.set('state_in',                                      params.state.split(','))  if params.state
    filters.set('workflow_action_definition_identifier_in',      params.action.split(',')) if params.action
    filters.set('workflow_action_definition_organization_id', params.organization_id)   if params.organization_id
    Ember.RSVP.hash
      projects: @store.findAll('panda/project')
      organizations: @store.findAll('panda/organization')
      project_instance_actions:
        PagedRemoteArray.create
          modelName:   @modelName
          store:       @store
          filters:     filters
          page:        params.page || 1
          perPage:     params.perPage || 30
          otherParams:
            include: 'action-list'
