package com.toomasr.sgf4j;

import java.io.BufferedReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Iterator;
import java.util.Map;

import com.toomasr.sgf4j.parser.Game;
import com.toomasr.sgf4j.parser.GameNode;

public class Sgf {
  private Parser parser;
  private Game game;

  private Sgf(String sgf) {
    parser = new Parser(sgf);
    game = parser.parse();
    game.postProcess();
  }

  public static Game createFromPath(Path path) {
    try {
      String gameAsString = new String(Files.readAllBytes(path));
      return createFromString(gameAsString);
    }
    catch (IOException e) {
      throw new RuntimeException(e);
    }
  }

  public static Game createFromString(String gameAsString) {
    Sgf rtrn = new Sgf(gameAsString);
    return rtrn.getGame();
  }

  public static Game createFromInputStream(InputStream in) {
    try (BufferedReader reader = new BufferedReader(new InputStreamReader(in))) {
      StringBuilder out = new StringBuilder();
      String line;
      while ((line = reader.readLine()) != null) {
        out.append(line);
      }
      Sgf rtrn = new Sgf(out.toString());
      return rtrn.getGame();
    }
    catch (IOException e) {
      throw new RuntimeException(e);
    }

  }

  public static void writeToFile(Game game, Path destination) throws Exception {
    FileWriter fw = new FileWriter(destination.toFile());

    fw.write("(");
    GameNode node = game.getRootNode();
    do {
      fw.write(";");
      for (Iterator<Map.Entry<String, String>> ite = node.getProperties().entrySet().iterator(); ite.hasNext();) {
        Map.Entry<String, String> entry = ite.next();
        fw.write(entry.getKey() + "[" + entry.getValue() + "]");
      }
      fw.write("\n");
      System.out.println(node);
    }
    while ((node = node.getNextNode()) != null);
    fw.write(")");

    fw.close();
  }

  private Game getGame() {
    return game;
  }
}

@@ -1432,27 +1432,24 @@
 on) 
-throws Exception %7B%0A
+%7B%0A    try (%0A    
     
@@ -1504,30 +1504,431 @@
 e())
-;%0A%0A    fw.write(%22(%22);%0A
+) %7B%0A%0A      fw.write(%22(%22);%0A%0A      // lets write all the root node properties%0A      Map%3CString, String%3E props = game.getProperties();%0A      if (props.size() %3E 0) %7B%0A        fw.write(%22;%22);%0A      %7D%0A%0A      for (Iterator%3CMap.Entry%3CString, String%3E%3E ite = props.entrySet().iterator(); ite.hasNext();) %7B%0A        Map.Entry%3CString, String%3E entry = ite.next();%0A        fw.write(entry.getKey() + %22%5B%22 + entry.getValue() + %22%5D%22);%0A      %7D%0A  
     
@@ -1971,13 +1971,17 @@
     
+  
 do %7B%0A
+  
     
@@ -1999,24 +1999,26 @@
 %22;%22);%0A      
+ 
+ 
 for (Iterato
@@ -2114,32 +2114,34 @@
 t();) %7B%0A        
+  
 Map.Entry%3CString
@@ -2162,32 +2162,34 @@
 y = ite.next();%0A
+  
         fw.write
@@ -2239,26 +2239,30 @@
 %22%5D%22);%0A      
-%7D%0A
+  %7D%0A  
       fw.wri
@@ -2276,16 +2276,21 @@
 );%0A     
+   //
  System.
@@ -2312,22 +2312,26 @@
 e);%0A    
+  
 %7D%0A    
+ 
+ 
 while ((
@@ -2367,24 +2367,26 @@
  null);%0A    
+  
 fw.write(%22)%22
@@ -2393,16 +2393,18 @@
 );%0A%0A    
+  
 fw.close
@@ -2403,24 +2403,101 @@
 fw.close();%0A
+    %7D%0A    catch (IOException e) %7B%0A      throw new RuntimeException(e);%0A    %7D%0A
   %7D%0A%0A  priva

Support writing out SGF files