import ModalBase from 'ember-cli-dunlop/mixins/modal-base'
export default ModalBase.extend
  selected_workflow_action_definition_ids: []
  selected_workflow_group_ids: []
  initial_selected_workflow_action_definition_ids: []
  initial_selected_workflow_group_ids: []
  willOpenModal: ->
    current_workflow_action_definition_ids = @get('model.workflow_action_definitions').mapBy('id').toArray()
    current_workflow_group_ids = @get('model.workflow_groups').mapBy('id').toArray()
    @set 'selected_workflow_action_definition_ids', current_workflow_action_definition_ids
    @set 'selected_workflow_group_ids', current_workflow_group_ids

    @set 'initial_workflow_action_definition_ids', current_workflow_action_definition_ids.copy()
    @set 'initial_workflow_group_ids', current_workflow_group_ids.copy()
  actions:
    check_all: ->
      @set 'selected_workflow_action_definition_ids', @get('model.project.workflow_action_definitions').mapBy('id').toArray()
      @set 'selected_workflow_group_ids', @get('model.project.workflow_groups').mapBy('id').toArray()
    check_none: ->
      @set 'selected_workflow_action_definition_ids', []
      @set 'selected_workflow_group_ids', []
    change_workflow_group_selection: (workflow_group, checked) ->
      if checked
        @get('selected_workflow_group_ids').addObject workflow_group.get('id')
        @get('selected_workflow_action_definition_ids').addObjects workflow_group.get('workflow_action_definitions').mapBy('id')
      else
        @get('selected_workflow_group_ids').removeObject workflow_group.get('id')
        @get('selected_workflow_action_definition_ids').removeObjects workflow_group.get('workflow_action_definitions').mapBy('id')
    change_workflow_action_definition_selection: (workflow_action_definition, checked) ->
      if checked
        @get('selected_workflow_action_definition_ids').addObject workflow_action_definition.get('id')
        #@get('model.workflow_action_definitions').addObject workflow_action_definition
      else
        @get('selected_workflow_action_definition_ids').removeObject workflow_action_definition.get('id')
        #@get('model.workflow_action_definitions').removeObject workflow_action_definition
    saveRecord: ->
      selected_workflow_action_definition_ids = @get('selected_workflow_action_definition_ids')
      selected_workflow_group_ids = @get('selected_workflow_group_ids')
      initial_workflow_action_definition_ids = @get('initial_workflow_action_definition_ids')
      initial_workflow_group_ids = @get('initial_workflow_group_ids')

      added_workflow_action_definition_ids = selected_workflow_action_definition_ids.filter  (id) -> id not in initial_workflow_action_definition_ids
      removed_workflow_action_definition_ids = initial_workflow_action_definition_ids.filter (id) -> id not in selected_workflow_action_definition_ids

      added_workflow_group_ids = selected_workflow_group_ids.filter  (id) -> id not in initial_workflow_group_ids
      removed_workflow_group_ids = initial_workflow_group_ids.filter (id) -> id not in selected_workflow_group_ids

      if added_workflow_action_definition_ids.length
        added_workflow_action_definitions = @get('model.project.workflow_action_definitions').filter (record) ->
          record.get('id') in added_workflow_action_definition_ids
        @get('model.workflow_action_definitions').addObjects added_workflow_action_definitions
      if removed_workflow_action_definition_ids.length
        removed_workflow_action_definitions = @get('model.project.workflow_action_definitions').filter (record) ->
          record.get('id') in removed_workflow_action_definition_ids
        @get('model.workflow_action_definitions').removeObjects removed_workflow_action_definitions

      if added_workflow_group_ids.length
        added_workflow_groups = @get('model.project.workflow_groups').filter (record) ->
          record.get('id') in added_workflow_group_ids
        @get('model.workflow_groups').addObjects added_workflow_groups
      if removed_workflow_group_ids.length
        removed_workflow_groups = @get('model.project.workflow_groups').filter (record) ->
          record.get('id') in removed_workflow_group_ids
        @get('model.workflow_groups').removeObjects removed_workflow_groups

      @_super arguments...
      @incrementProperty 'model.project.update_revision'
