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

  grouped_tracker_groups_setter: (->
    base = @
    group = Ember.A()
    @get("model.tracker_groups").then (tracker_groups)->
      tracker_groups.forEach (tracker_group)->
        item_group = Ember.Object.create
          model: tracker_group
          projects: Ember.A()
        tracker_group.get('workflow_groups').then (workflow_groups)->
          workflow_groups.forEach (workflow_group)->
            hasProject = existing_project_group = item_group.get("projects").findBy("model.id", workflow_group.get("project.id"))
            if hasProject
              existing_project_group.get("workflow_groups").push(workflow_group)
            else
              item_group.get("projects").pushObject Ember.Object.create(model: workflow_group.get('project'), workflow_groups: Ember.A([workflow_group]))
            base.incrementProperty "change_iteration"
        group.push item_group
    @set("grouped_tracker_groups", group)
  ).observes  "model.tracker_groups.@each.workflow_groups.@each.project"

  #graph_content: Ember.computed "grouped_tracker_groups", ->
  #graph_content: Ember.computed "model.tracker_groups.@each.workflow_groups.@each.project", ->
  graph_content: Ember.computed "change_iteration", ->
    txt = "digraph prof {\n  ratio=fill;\n   node [style=filled fillcolor=\"#A2D9CE\"];\n  rankdir=TB;\n"
    labeled_projects = {}
    item_projects = {}
    tracker_group_color     = '"#AAAAAA"'
    project_color       = '"#AAFFAA"'
    workflow_group_color = '"#AAAAFF"'

    grouped_tracker_groups = @get("grouped_tracker_groups")
    grouped_tracker_groups.forEach (tracker_group_group)->
      tracker_group_id = "q#{tracker_group_group.get("model.id")}" # start with a letter
      txt += "  #{tracker_group_id} [label=\"#{tracker_group_group.get('model.name')}\" fillcolor=#{tracker_group_color}];\n"
      tracker_group_group.get("projects").forEach (project)->
        project_id = "q#{project.get("model.id")}"
        unless labeled_projects[project_id]
          txt += "  #{project_id} [label=\"#{project.get('model.name')}\" fillcolor=#{project_color}];\n"
          labeled_projects[project_id] = 1
        project.workflow_groups.forEach (workflow_group)->
          workflow_group_id = "q#{workflow_group.id}"
          txt += "  #{workflow_group_id} [label=\"#{workflow_group.get('name')}\" fillcolor=#{workflow_group_color}];\n"

          unless item_projects["#{tracker_group_id}-#{project_id}"]
            txt += "  #{tracker_group_id} -> #{project_id} [color=\"#AAAAAA\" penwidth=1];\n"
            item_projects["#{tracker_group_id}-#{project_id}"] = 1
          txt += "  #{project_id  } -> #{workflow_group_id} [color=\"#AAAAAA\" penwidth=1];\n"
          #txt += "  #{tracker_group_id} -> #{project_id} -> #{workflow_group_id};\n"

    txt += "}"
    txt
  svg_content: Ember.computed 'model.id', 'graph_content', ->
    data = @get('graph_content')
    try
      svg = Viz(data, format: 'svg')
      svg.htmlSafe()
    catch error
      console.log "Error parsing\n\n#{data}"

