import ModalBase from 'ember-cli-dunlop/mixins/modal-base'
import DS from 'ember-data'
add_offset_to = (target, offsets, record, project) ->
  Ember.makeArray(offsets).forEach (offset_name) ->
    offset_object =
      value: record.get("#{offset_name}_value") || 0
      unit: record.get("#{offset_name}_unit")
    if offset_object.unit is 'businessday'
      offset_object.business_days = record.get("#{offset_name}_business_days") || [1,2,3,4,5]
    target[offset_name] = offset_object
  target

export default ModalBase.extend
  contract: null
  download_file_name: 'PANDA-contract.json'
  download_href: Ember.computed 'contract.content', ->
    return 'javascript:void(0)' unless content = @get('contract.content')
    hrefContent = "data:text/json;charset=utf-8,#{encodeURIComponent(JSON.stringify(content))}"
    hrefContent

  willOpenModal: ->
    project = @get('model')
    component = @
    project_name = String(project.get('name')).replace(/[\W_]/g, '-')
    is_batch_project = project.get('instance_scope') is 'batch'
    @set 'download_file_name', "PANDA-contract-#{project_name}.json"
    @set 'contract', DS.PromiseObject.create
      promise: new Ember.RSVP.Promise (resolve) ->
        contract =
          generating_project: project.get('id')
          identifier: project.get('identifier')
          contract_version: 'v1'
          instance_scope: project.get('instance_scope')
          workflow_groups:  []
          workflow_actions: []
          scenarios:        []
        if instance_type = project.get('workflow_instance_type.name')
          contract.instance_type = instance_type
        if is_batch_project
          contract.batch_size_lower_value = project.get('batch_size_lower_value')
          contract.batch_size_upper_value = project.get('batch_size_upper_value')
        if reference_date_day_of_week = project.get('reference_date_day_of_week')
          contract.reference_date_day_of_week = reference_date_day_of_week
        project.get('sorted_workflow_groups').forEach (workflow_group) ->
          workflow_group_object =
            name: workflow_group.get('name')
            identifier: workflow_group.get('identifier')
            offset_type: workflow_group.get('offset_type')
            offset_base: workflow_group.get('offset_base.identifier') || null # no undefined
            scenarios: workflow_group.get('project_scenarios').mapBy('identifier')
          add_offset_to workflow_group_object, ['offset', 'duration', 'batch_element_threshold'], workflow_group
          if is_batch_project
            add_offset_to workflow_group_object, 'batch_element_threshold', workflow_group
          contract.workflow_groups.push workflow_group_object
        project.get('sorted_workflow_action_definitions').forEach (workflow_action_definition) ->
          workflow_action_definition_object =
            identifier:                workflow_action_definition.get('identifier')
            title:                     workflow_action_definition.get('title')
            position:                  workflow_action_definition.get('position')
            extra_info:                workflow_action_definition.get('extra_info')
            value_type:                workflow_action_definition.get('value_type')
            value_type_select_options: workflow_action_definition.get('value_type_select_options')
            overdue_coulance:          workflow_action_definition.get('overdue_coulance')
            offset_type:               workflow_action_definition.get('offset_type')
            workflow_group:            workflow_action_definition.get('workflow_group.identifier') || null # no undefined
            confirms_workflow_action:  workflow_action_definition.get('confirms_workflow_action_definition.identifier') || null # no undefined
            completes_group:           workflow_action_definition.get('completes_group')
            organization:              workflow_action_definition.get('organization.identifier')
            scenarios:                 workflow_action_definition.get('project_scenarios').mapBy('identifier')
          contract.workflow_actions.push workflow_action_definition_object

          workflow_action_definition.get('workflow_action_offsets').forEach (workflow_action_offset) ->
            workflow_action_definition_object.action_offsets ||= []
            offset_object = {}
            if base_identifier = workflow_action_offset.get('base_workflow_action_definition.identifier')
              offset_object.base_workflow_action = base_identifier
            add_offset_to offset_object, 'offset', workflow_action_offset
            if is_batch_project
              add_offset_to offset_object, 'batch_element_threshold', workflow_action_offset
            workflow_action_definition_object.action_offsets.push offset_object

          workflow_action_definition.get('workflow_action_connections').forEach (workflow_action_connection) ->
            workflow_action_definition_object.action_connections ||= []
            connection_object =
              identifier:        workflow_action_connection.get('base_workflow_action_definition.identifier')
              connection_type:   workflow_action_connection.get('connection_type')
              conditional_value: workflow_action_connection.get('conditional_base_value')
            workflow_action_definition_object.action_connections.push connection_object

          workflow_action_definition.get('workflow_action_constraints').forEach (workflow_action_constraint) ->
            workflow_action_definition_object.action_constraints ||= []
            workflow_action_definition_object.action_constraints.push
              type: workflow_action_constraint.get('type')
              base_workflow_action: workflow_action_constraint.get('constraint_workflow_action_definition.identifier')
        project.get('project_scenarios').forEach (project_scenario) ->
          project_scenario_object =
            identifier: project_scenario.get('identifier')
            name: project_scenario.get('name')
          contract.scenarios.push project_scenario_object
        resolve contract
