package transform;

import org.codehaus.jackson.map.ObjectMapper;
import whelk.Document;
import whelk.JsonLd;
import whelk.triples.JsonldSerializer;
import whelk.util.TransformScript;

import java.io.*;
import java.util.Map;

public class Main
{
    private static ObjectMapper mapper = new ObjectMapper();

    public static void main(String[] args) throws IOException, TransformScript.TransformSyntaxException
    {
        if (args.length == 0)
        {
            javax.swing.SwingUtilities.invokeLater(new Runnable()
            {
                public void run()
                {
                    new ExecuteGui();
                }
            });
        }
        else if (args[0].equals("syntax"))
            generateAndPrintSyntax(args);
        else if (args[0].equals("transform"))
            generateAndPrintTransform(args);
        else if (args[0].equals("execute"))
            executeScript(args);
        else
            System.err.println(
                    "Usage:\n" +
                            "java -jar transform.jar syntax [file1]\n" +
                            "  Generate a syntax that convers all json in [file1], one json document per line.\n" +
                            "\n" +
                            "java -jar transform.jar transform [syntax1file] [syntax2file] [file1] [file2]\n" +
                            "  Generate a diff script from syntax1 to syntax2 using values found in the streams\n" +
                            "  each element in order in file1 and file2 is expected to represent the \"the same\"" +
                            "  record, in the various format forms.\n" +
                            "java -jar transform.jar execute [scriptfile] [file]\n" +
                            "  Execute the given script on each json document in [file], one document per line.");
    }

    private static void generateAndPrintSyntax(String[] args) throws IOException
    {
        BufferedReader in = new BufferedReader(new FileReader(args[1]));

        String jsonString;
        Syntax syntax = new Syntax();
        while ( (jsonString = in.readLine()) != null)
        {
            Map data = mapper.readValue(jsonString, Map.class);
            Document doc = new Document(data);
            data = JsonLd.frame(doc.getCompleteId(), data);
            syntax.expandSyntaxToCover(data);
        }
        in.close();
        System.out.println(syntax);
    }

    private static void generateAndPrintTransform(String[] args) throws IOException, TransformScript.TransformSyntaxException
    {
        BufferedReader syntax1Reader = new BufferedReader(new FileReader(args[1]));
        BufferedReader syntax2Reader = new BufferedReader(new FileReader(args[2]));
        Syntax syntax1 = new Syntax(syntax1Reader);
        Syntax syntax2 = new Syntax(syntax2Reader);
        syntax1Reader.close();
        syntax2Reader.close();

        BufferedReader json1Reader = new BufferedReader(new FileReader(args[3]));
        BufferedReader json2Reader = new BufferedReader(new FileReader(args[4]));

        ScriptGenerator scriptGenerator = SyntaxDiffReduce.generateScript(syntax1, syntax2, json1Reader, json2Reader);

        json1Reader.close();
        json2Reader.close();

        System.out.println(scriptGenerator);
    }

    private static void executeScript(String[] args) throws IOException, TransformScript.TransformSyntaxException
    {
        BufferedReader scriptReader = new BufferedReader(new FileReader(args[1]));
        StringBuilder scriptText = new StringBuilder();
        for(String line = scriptReader.readLine(); line != null; line = scriptReader.readLine())
        {
            scriptText.append(line + "\n");
        }

        TransformScript script = new TransformScript(scriptText.toString());

        BufferedReader jsonReader = new BufferedReader(new FileReader(args[2]));
        for(String line = jsonReader.readLine(); line != null; line = jsonReader.readLine())
        {
            Map data = mapper.readValue(line, Map.class);
            Map transformed = script.executeOn(data);
            transformed = JsonLd.flatten(transformed);
            JsonldSerializer.normalize(transformed, (String) Document._get(Document.getRecordIdPath(), data), true);
            System.out.println(mapper.writeValueAsString(transformed));
        }
    }
}

@@ -702,102 +702,16 @@
 ls(%22
-syntax%22))%0A            generateAndPrintSyntax(args);%0A        else if (args%5B0%5D.equals(%22transform
+generate
 %22))%0A
@@ -967,269 +967,16 @@
 jar 
-syntax %5Bfile1%5D%5Cn%22 +%0A                            %22  Generate a syntax that convers all json in %5Bfile1%5D, one json document per line.%5Cn%22 +%0A                            %22%5Cn%22 +%0A                            %22java -jar transform.jar transform %5Bsyntax1file%5D %5Bsyntax2file%5D
+generate
  %5Bfi
@@ -1056,27 +1056,8 @@
 rom 
-syntax1 to syntax2 
 usin
@@ -1258,15 +1258,11 @@
 the 
-various
+new
  for
@@ -1525,22 +1525,25 @@
 AndPrint
-Syntax
+Transform
 (String%5B
@@ -1560,32 +1560,74 @@
 rows IOException
+, TransformScript.TransformSyntaxException
 %0A    %7B%0A        B
@@ -1640,18 +1640,27 @@
 dReader 
-in
+json1Reader
  = new B
@@ -1694,24 +1694,106 @@
 r(args%5B1%5D));
+%0A        BufferedReader json2Reader = new BufferedReader(new FileReader(args%5B2%5D));
 %0A%0A        St
@@ -1809,16 +1809,17 @@
 String;%0A
+%0A
         
@@ -1831,16 +1831,17 @@
 x syntax
+1
  = new S
@@ -1879,18 +1879,27 @@
 tring = 
-in
+json1Reader
 .readLin
@@ -2111,16 +2111,17 @@
   syntax
+1
 .expandS
@@ -2158,18 +2158,27 @@
         
-in
+json1Reader
 .close()
@@ -2171,32 +2171,33 @@
 Reader.close();%0A
+%0A
         System.o
@@ -2194,171 +2194,108 @@
   Sy
-stem.out.println(s
+ntax syntax2 = new S
 yntax
+(
 );%0A    
-%7D%0A%0A
     
-private static void generateAndPrintTransform(String%5B%5D args) throws IOException, TransformScript.TransformSyntaxException%0A
+while ( (jsonString = json2Reader.readLine()) != null)%0A  
     
+  
 %7B%0A  
@@ -2304,217 +2304,168 @@
     
-BufferedReader syntax1Reader = new BufferedReader(new FileReader(args%5B1%5D));%0A        BufferedReader syntax2Reader = new BufferedReader(new FileReader(args%5B2%5D));%0A        Syntax syntax1 = new Syntax(syntax1Reader
+    Map data = mapper.readValue(jsonString, Map.class);%0A            Document doc = new Document(data);%0A            data = JsonLd.frame(doc.getCompleteId(), data
 );%0A 
@@ -2467,38 +2467,35 @@
 a);%0A        
-Syntax
+   
  syntax2
  = new Synta
@@ -2486,68 +2486,45 @@
 tax2
- = new Syntax(syntax2Reader);%0A        syntax1Reader.close();
+.expandSyntaxToCover(data);%0A        %7D
 %0A   
@@ -2528,22 +2528,20 @@
         
-syntax
+json
 2Reader.
@@ -2554,39 +2554,24 @@
 );%0A%0A        
-BufferedReader 
 json1Reader 
@@ -2615,9 +2615,9 @@
 rgs%5B
-3
+1
 %5D));
@@ -2621,39 +2621,24 @@
 ));%0A        
-BufferedReader 
 json2Reader 
@@ -2682,9 +2682,9 @@
 rgs%5B
-4
+2
 %5D));

Make script generation easier.