import Ember from 'ember'
import ActiveWorkflowActionDefinitions from 'ember-panda/mixins/project/active-workflow-action-definitions'
import get_active_workflow_groups from 'ember-panda/utils/project/active-workflow-groups'
import get_active_workflow_action_definitions from 'ember-panda/utils/project/active-workflow-action-definitions'
export default Ember.Controller.extend
  newWorkflowStepName: ''
  batch_size_simulation_range: Ember.computed 'model.{batch_size_lower_value,batch_size_upper_value}',->
    {min: @get('model.batch_size_lower_value'), max: @get('model.batch_size_upper_value')}
  batch_size_simulation_value: 0
  chosen_absolute_date: null
  reference_date_day_of_week: Ember.computed.oneWay 'model.reference_date_day_of_week'
  project: Ember.computed.alias 'model'

  # Multiple project scenarios (currently inactive)
  selected_project_scenarios: []
  has_multiple_selected_project_scenarios: Ember.computed.gt 'selected_project_scenarios.length', 1
  has_one_selected_project_scenario: Ember.computed.equal 'selected_project_scenarios.length', 1
  selected_project_scenarios_set_operation: 'union'

  active_project_scenario: null

  init: ->
    @_super arguments...
    #@get 'active_workflow_action_definitions_identity'
    #@set_active_workflow_action_definitions()

  #_active_workflow_action_definitions_obsever: Ember.observer 'selected_project_scenarios.@each.id', 'selected_project_scenarios_set_operation', 'project.workflow_action_definitions.@each.id', 'project.workflow_groups.@each.id', 'project.update_revision', ->
  _active_workflow_action_definitions_obsever: Ember.observer 'active_project_scenario', 'project.workflow_action_definitions.@each.id', 'project.workflow_groups.@each.id', 'project.update_revision', ->
    Ember.run.debounce @, (->
      #[project_scenarios, set_operation] = @getPropertyValues('selected_project_scenarios', 'selected_project_scenarios_set_operation')
      set_operation = 'union'
      if active_project_scenario = @get('active_project_scenario')
        @set 'active_workflow_action_definitions', get_active_workflow_action_definitions(@get('model.workflow_action_definitions'), [active_project_scenario], set_operation)
        @set 'active_workflow_groups', get_active_workflow_groups(@get('model.workflow_groups'), [active_project_scenario], set_operation)
      else
        @set 'active_workflow_action_definitions', @get('model.workflow_action_definitions')
        @set 'active_workflow_groups', @get('model.workflow_groups')
    ), 350

  actions:
    newWorkflowStep: ->
      new_name = @get('newWorkflowStepName')
      workflow_group = @get("model.workflow_groups").createRecord(name: new_name, position: @get('model.workflow_groups.length'))
      @set 'newWorkflowStepName', ''
      @modal "project/edit-workflow-group",
        model: workflow_group
    destroyWorkflowStep: (workflow_group) -> workflow_group.destroyRecord()
    toggle_sidebar: (id) ->
      $("##{id}").sidebar('toggle')
    positionWorkflowActionDefinition: (dragged_workflow_action_definition, options = {})->
      workflow_group = options.target.content
      items = options.target.$('.draggableItem')
      top_offsets = items.map (i, item) -> $(item).offset().top
      dragged_offset = options.event.originalEvent.clientY
      my_position = 0
      compare_offset = top_offsets[0]
      while compare_offset and compare_offset < dragged_offset
        my_position += 1
        compare_offset = top_offsets[my_position]
      position_counter = 0
      workflow_group.get('sorted_workflow_action_definitions').forEach (workflow_action_definition)->
        if position_counter is my_position
          position_counter += 1
        workflow_action_definition.set 'position', position_counter
        workflow_action_definition.save()
        position_counter += 1
        # Just before the last one, so the 

      dragged_workflow_action_definition.set 'workflow_group', workflow_group
      dragged_workflow_action_definition.set 'position', my_position
      dragged_workflow_action_definition.save()
    displayWorkflowActionDefinitions: ->
      #workflow_action_definitions = Ember.A()
      #@get('model.sorted_workflow_groups').forEach (workflow_group, i)->
      #  workflow_action_definitions.addObjects(workflow_group.get('workflow_action_definitions'))
      @modal 'display-workflow-action-definitions',
        #model: workflow_action_definitions
        model: @get('model.sorted_workflow_groups')
        title: @get('model.name')
    show_project_info: ->
      @modal 'project/show/dunlop-project-info', model: @get('model')
    sortWorkflowSteps: ->
      @modal 'sort-records',
        model: @get('model.workflow_groups')
        title: "Sort workflow steps for project #{@get('model.name')}"

    positionWorkflowActionDefinitionInProject: (workflow_action_definition) ->
      @modal 'project/position-workflow-action-definition-in-project',
        model: workflow_action_definition

    select_project_scenario: (project_scenario) ->
      @set 'selected_project_scenarios', project_scenario


