package org.yoooo.se1;

import java.awt.*;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Set;

public class ShowDirectedGraph {
    /**
     * Saves a graph to filename.png
     *
     * @param graph graph to show
     * @param filename filename of generated picture(without extension)
     * @exception RuntimeException throw when file operation failed
     */
    public static void showDirectedGraph(Graph<String, Integer> graph, String filename) throws RuntimeException{
        StringBuilder dotContent = new StringBuilder();
        dotContent.append(String.format("digraph {%n"));
        Set<String> vertexSet = graph.vertexSet();
        for (String currentVertex : vertexSet) {
            Set<Integer> outgoingEdges = graph.outgoingEdgesOf(currentVertex);
            for (Integer currentEdge : outgoingEdges) {
                dotContent.append(String.format("\t\"%s\" -> \"%s\"", currentVertex, graph.getEdgeTarget(currentEdge)));
                dotContent.append(String.format(" [label = \"%.0f\"]", graph.getEdgeWeight(currentEdge)));
                dotContent.append(String.format("%n"));
            }
        }
        dotContent.append(String.format("}%n"));
        try (FileWriter writer = new FileWriter(filename + ".dot")) {
            try {
                writer.write(dotContent.toString());
            } catch (IOException e) {
                throw new RuntimeException("fail to write " + filename + ".dot");
            }
        } catch (IOException e) {
            throw new RuntimeException("fail to open " + filename + ".dot");
        }
        try {
            try {
                Runtime.getRuntime().exec("dot -Tpng -o" + filename + ".png " + filename + ".dot").waitFor();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            Desktop.getDesktop().open(new File(filename + ".png"));
        } catch (IOException e) {
            throw new RuntimeException("file to write " + filename + ".png");
        }
    }
}

@@ -187,12 +187,15 @@
   * 
-Save
+Convert
 s a 
@@ -207,20 +207,16 @@
  to 
+dot 
 file
-name.png
 %0A   
@@ -248,20 +248,23 @@
 raph to 
-show
+convert
 %0A     * 
@@ -274,133 +274,115 @@
 ram 
-filename filename of generated picture(without extension)%0A     * @exception RuntimeException throw when file operation
+coloredVertex vertices to color%0A     * @param coloredEdge edges to color%0A     * @return content of dot
  f
-a
 ile
-d
 %0A   
@@ -395,21 +395,22 @@
    p
-ublic
+rivate
  static 
 void
@@ -409,30 +409,28 @@
 tic 
-void showDirectedGraph
+String getDotContent
 (Gra
@@ -460,48 +460,61 @@
 ph, 
+Set%3C
 String
- filename) throws RuntimeException
+%3E coloredVertex, Set%3CInteger%3E coloredEdge) 
 %7B%0A  
@@ -1107,120 +1107,1033 @@
     
-dotContent.append(String.format(%22%25n%22));%0A            %7D%0A        %7D%0A        dotContent.append(String.format(%22%7D%25n%22));
+if (coloredEdge != null && coloredEdge.contains(currentEdge))%0A                    dotContent.append(%22 %5Bstyle = bold, color = dodgerblue%5D%22);%0A                dotContent.append(String.format(%22%25n%22));%0A            %7D%0A        %7D%0A        if (coloredVertex != null)%0A            for (String currentVertex : coloredVertex)%0A                dotContent.append(String.format(%22%5Ct%25s %5Bstyle = filled, fillcolor = lightskyblue%5D%25n%22, currentVertex));%0A        dotContent.append(String.format(%22%7D%25n%22));%0A        return dotContent.toString();%0A    %7D%0A%0A    /**%0A     * Saves a graph with vertices/edges colored to filename.png and open it%0A     *%0A     * @param graph graph to show%0A     * @param filename filename of generated picture(without extension)%0A     * @param coloredVertex vertices to color%0A     * @param coloredEdge edges to color%0A     * @exception RuntimeException throw when file operation failed%0A     */%0A    public static void showDirectedGraph(Graph%3CString, Integer%3E graph, String filename, Set%3CString%3E coloredVertex, Set%3CInteger%3E coloredEdge) %7B
 %0A   
@@ -2246,17 +2246,20 @@
 r.write(
-d
+getD
 otConten
@@ -2259,26 +2259,50 @@
 tContent
-.toString(
+(graph, coloredVertex, coloredEdge
 ));%0A    
@@ -2935,19 +2935,19 @@
 ption(%22f
+a
 il
-e
  to writ
@@ -2988,10 +2988,408 @@
 %7D%0A    %7D%0A
+    /**%0A     * Saves a graph to filename.png and open it%0A     *%0A     * @param graph graph to show%0A     * @param filename filename of generated picture(without extension)%0A     * @exception RuntimeException throw when file operation failed%0A     */%0A    public static void showDirectedGraph(Graph%3CString, Integer%3E graph, String filename) %7B%0A        showDirectedGraph(graph, filename, null, null);%0A    %7D%0A
 %7D%0A

add support to paths visualization