import ModalBase from 'ember-cli-dunlop/mixins/modal-base'
import Changeset from 'ember-changeset'
export default ModalBase.extend
  organization_tracker_subjects: Ember.A()
  constraint_workflow_groups: Ember.A()
  offset_selectable_workflow_groups: []

  offset_type_none: Ember.computed.equal 'model.offset_type', 'none'
  offset_type_connected_finish: Ember.computed.equal 'model.offset_type', 'connected_finish'
  offset_type_connected_start: Ember.computed.equal 'model.offset_type', 'connected_start'

  willOpenModal: ->
    @set "constraint_workflow_groups", @get('model.project.sorted_workflow_groups').rejectBy('id', @get('model.id'))
    @set_offset_selectable_workflow_groups()
    changeset = new Changeset(@get('model'))
    @set 'model', changeset
    @store.findAll('panda/tracker-subject', backgroundReload: false).then (tracker_subjects)=>
      organization_tracker_subjects = Ember.A()
      current_tracker_groups = @get('model.tracker_groups')
      tracker_subjects.filterBy("edit_from_workflow_group").forEach (tracker_subject)->
        current_tracker_subject_item = current_tracker_groups.findBy("tracker_subject.id", tracker_subject.id)
        organization_tracker_subjects.push Ember.Object.create
          model: tracker_subject
          current_tracker_group: current_tracker_subject_item
      @set "organization_tracker_subjects", organization_tracker_subjects

  set_offset_selectable_workflow_groups: (offset_type) ->
    offset_type ||= @get('model.offset_type')
    return if offset_type is 'none'
    selectable_workflow_groups = @get('model.project.sorted_workflow_groups') #.rejectBy('offset_type', 'none') allow all groups, more user friendly
    @set 'offset_selectable_workflow_groups', selectable_workflow_groups
  actions:
    saveRecord: ->
      #if offset_value = @get('model.offset_value')
      #  @set 'model.offset_value', parseInt(offset_value)
      #if duration_value = @get('model.duration_value')
      #  @set 'model.duration_value', parseInt(duration_value)
      #if @get("model.isNew")
      #  return @get("model").save() # add extra functions, do not close modal
      @set("model.tracker_groups", @get("organization_tracker_subjects").mapBy("current_tracker_group").compact())
      @get('model.constraints').forEach (constraint)->
        constraint_workflow_group_id = constraint.get('constraint_workflow_group.id')
        constraint.get('workflow_action_definitions').rejectBy('workflow_group.id', constraint_workflow_group_id).forEach (illigal_workflow_group_action)->
          constraint.get('workflow_action_definitions').removeObject illigal_workflow_group_action

          #workflow_action_definitions.forEach (workflow_action_definition)->
          #  debugger
          #  workflow_action_definitions.removeObject(workflow_action_definition) unless workflow_action_definition.get("workflow_group.id") is constraint_workflow_group_id
          #  # remove non constraint_workflow_action_definition actions, switching the constraint_workflow_group does not remove these
          #  #constraint.set "workflow_action_definitions", workflow_action_definitions.filterBy("id", constraint_workflow_group_id)
          #debugger
        constraint.save()
      @incrementProperty('model.project.update_revision')
      @_super()
    rollback_and_close: ->
      constraints = @get('model.constraints')
      constraints.filterBy('isNew').invoke 'unloadRecord'
      constraints.rejectBy('isNew').invoke 'rollbackAttributes' # not rolling back belongsTo, known issue
      @_super()

    set_offset_type: (offset_type) ->
      offset_type ||= 'none'
      @set 'model.offset_base', null
      #TODO: CLEANUP DEPENDENCY TREE FARTHER AWAY FROM REFERENCE DATE DEPENDENT ON MODEL
      @set 'model.offset_type', offset_type
      @set_offset_selectable_workflow_groups offset_type

    newConstraint: ->
      constraint = @store.createRecord('panda/workflow-group-constraint', workflow_group: @get('model'))
    set_batch_element_threshold_unit: (unit) -> @set 'model.batch_element_threshold_unit', unit

