import ModalBase from 'ember-cli-dunlop/mixins/modal-base'
export default ModalBase.extend
  actions:
    position_workflow_action_definition_at_top_of: (workflow_action_definition, workflow_group) ->
      Ember.run @, ->
        workflow_action_definition.setProperties
          position: 0
          workflow_group: workflow_group
        workflow_action_definition.save()
        working_position = 1
        workflow_group.get('sorted_workflow_action_definitions').forEach (group_workflow_action_definition) ->
          return if group_workflow_action_definition.get('id') is workflow_action_definition.get('id')
          if group_workflow_action_definition.get('position') isnt working_position
            group_workflow_action_definition.set 'position', working_position
            group_workflow_action_definition.save()
          working_position += 1
        @send 'close'

    position_workflow_action_definition_below: (workflow_action_definition, workflow_action_definition_above) ->
      workflow_group = workflow_action_definition_above.get('workflow_group')
      working_position = 0
      Ember.run @, ->
        workflow_group.get('sorted_workflow_action_definitions').forEach (group_workflow_action_definition) ->
          return if group_workflow_action_definition.get('id') is workflow_action_definition.get('id')
          if group_workflow_action_definition.get('position') isnt working_position
            group_workflow_action_definition.set 'position', working_position
            group_workflow_action_definition.save()
          if group_workflow_action_definition.get('id') is workflow_action_definition_above.get('id')
            working_position += 1
            workflow_action_definition.setProperties
              position: working_position
              workflow_group: workflow_group
            workflow_action_definition.save()
          working_position += 1
        @send 'close'

    unassign_workflow_action_definition: ->
      workflow_action_definition = @get('model')
      workflow_action_definition.set 'workflow_group', null
      workflow_action_definition.save()
      @send 'close'

