import Ember from 'ember'
import Viz from 'npm:viz.js' # browserify
export default Ember.Component.extend

  #constraints: Ember.computed.alias "workflow_groups.@each.constraints"
  #workflow_groups: Ember.computed.alias('project.sorted_workflow_groups')
  #digraph_content: Ember.computed "constraints.@each.type", "constraints.@each.constraint_workflow_group.id", "workflow_groups.@each.name", ->
  #digraph_content: Ember.computed "project.update_revision", ->
  digraph_content: Ember.computed "project.update_revision", "project.workflow_groups.@each.constraints.@each.type", "project.workflow_groups.@each.constraints.@each.workflow_action_definitions.length", ->
    txt = "digraph prof {\n  ratio=fill;\n   node [style=filled fillcolor=\"#A2D9CE\"];\n  rankdir=TB;\n"
    @get('project.sorted_workflow_groups').forEach (workflow_group)->
      workflow_group_id = "q#{workflow_group.id}" # start with a letter
      txt += "  #{workflow_group_id} [label=\"#{workflow_group.get('name')}\"];\n"
      workflow_group.get('constraints').forEach (constraint)->
        constraint_workflow_group_id = "q#{constraint.get('constraint_workflow_group.id')}" # start with a letter
        actions_label = ""
        switch length = constraint.get('workflow_action_definitions.length')
          when 0 then # nothing
          when 1 then actions_label = " label=\"#{constraint.get('workflow_action_definitions.firstObject.name')}\""
          else actions_label = " label=\"#{length} actions\""
        if constraint.get('type') is "restrict"
          txt += "  #{constraint_workflow_group_id} -> #{workflow_group_id} [color=\"#C0392B\" penwidth=2 #{actions_label}];\n"
        else
          txt += "  #{constraint_workflow_group_id} -> #{workflow_group_id} [color=\"#F5B041\" #{actions_label}];\n"
    txt += "}"
  svg_content: Ember.computed "digraph_content", ->
    data = @get('digraph_content')
    svg = Viz(data, format: 'svg')
    svg.htmlSafe()
