import Ember from 'ember'
import { mapHasMany } from 'ember-cli-dunlop/utils/map-action'
import Viz from 'npm:viz.js' # browserify
export default Ember.Component.extend
  #workflow_action_constraints: mapHasMany 'project.workflow_action_definitions', 'workflow_action_constraints'
  has_content: true
  selected_project_scenario: null

  digraph_content: Ember.computed 'project.update_revision', 'active_workflow_action_definitions.@each.id', ->
    labels = ''
    connections = ''
    component = @
    return unless @get('active_workflow_action_definitions.length')
    active_workflow_action_definition_ids = @get('active_workflow_action_definitions').mapBy('id').toArray()
    @get('active_workflow_action_definitions').forEach (workflow_action_definition) ->
      constraint_connections = workflow_action_definition.get('workflow_action_connections').filterBy('connection_type', 'constraint')
      #return unless workflow_action_definition.get('workflow_action_constraints.length')
      return unless constraint_connections.get('length')
      workflow_action_definition_id = "q#{workflow_action_definition.id}" # start with a letter
      unless labels.indexOf(workflow_action_definition_id) > -1
        labels += "  #{workflow_action_definition_id} [label=\"#{workflow_action_definition.get('title')}\" tooltip=\"#{workflow_action_definition.get('workflow_group.name')}\"];\n"
      constraint_connections.forEach (constraint) ->
        return if constraint.get('isDeleted')
        #return unless constraint.get('constraint_workflow_action_definition.id') in active_workflow_action_definition_ids
        constraint_workflow_action_definition_id = "q#{constraint.get('base_workflow_action_definition.id')}" # start with a letter
        unless labels.indexOf(constraint_workflow_action_definition_id) > -1
          labels += "  #{constraint_workflow_action_definition_id} [label=\"#{constraint.get('base_workflow_action_definition.title')}\" tooltip=\"#{constraint.get('base_workflow_action_definition.workflow_group.name')}\"];\n"
        #actions_label = " label=\"#{constraint.get('type')}\""
        actions_label = " label=\"☝\""
        #if constraint.get('type') is "restrict"
        #  connections += "  #{constraint_workflow_action_definition_id} -> #{workflow_action_definition_id} [color=\"#C0392B\" penwidth=2 #{actions_label}];\n"
        #else
        #  connections += "  #{constraint_workflow_action_definition_id} -> #{workflow_action_definition_id} [color=\"#F5B041\" #{actions_label}];\n"
        #connections += "  #{constraint_workflow_action_definition_id} -> #{workflow_action_definition_id} [color=\"#F5B041\" #{actions_label}];\n"
        connections += "  #{workflow_action_definition_id} -> #{constraint_workflow_action_definition_id} [color=\"#F5B041\" #{actions_label}];\n"
    #@set 'has_content', !!labels
    "digraph \"Constraints diagram\" {\n  ratio=fill;\n   node [style=filled fillcolor=\"#A2D9CE\" shape=box];\n  rankdir=TB;\n#{labels}#{connections}}"

  svg_content: Ember.computed "digraph_content", ->
    data = @get('digraph_content')
    svg = Viz(data, format: 'svg')
    svg.htmlSafe()

  attach_events: Ember.on 'didRender', Ember.observer 'svg_content', ->
    component = @
    @$('.edge').each (index, edge) =>
      edge = $(edge)
      return unless title = edge.find('title').text()
      target_workflow_action_definition_id = title.replace(/.*->q/, '')
      return unless target_workflow_action_definition = @store.peekRecord('panda/workflow-action-definition', target_workflow_action_definition_id)
      edge.click ->
        component.modal 'workflow-action-definition/edit-workflow-action-definition',
          model: target_workflow_action_definition

