package org.oscii;

import com.google.gson.Gson;
import com.google.gson.stream.JsonWriter;
import org.apache.commons.cli.*;
import org.oscii.concordance.AlignedCorpus;
import org.oscii.lex.Meaning;
import org.oscii.panlex.PanLexDir;
import org.oscii.panlex.PanLexJSONParser;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.regex.Pattern;

public class Main {

    public static void main(String[] args)
            throws java.io.IOException,
            java.lang.InterruptedException,
            ParseException {
        CommandLine line = ParseArgs(args);
        Lexicon lexicon = new Lexicon();

        // Parse PanLex
        if (line.hasOption("p")) {
            String path = line.getOptionValue("p");
            PanLexJSONParser panLex = new PanLexJSONParser(new PanLexDir(path));

            List<String> languages = Arrays.asList("en", "es");
            if (line.hasOption("l")) {
                languages = Arrays.asList(line.getOptionValue("l").split(","));
            }
            panLex.addLanguages(languages);

            Pattern pattern = Pattern.compile("(?U)\\p{Lower}*");
            if (line.hasOption("pattern")) {
                pattern = Pattern.compile(line.getOptionValue("pattern"));
            }
            panLex.read(pattern);
            panLex.forEachMeaning(lexicon::add);
        } else if (line.hasOption("r")) {
            lexicon.read(new File(line.getOptionValue("r")));
        } else {
            System.out.println("Must parse (-p) or read (-r) a lexicon");
            System.exit(1);
        }

        // Index corpus
        if (line.hasOption("c")) {
            String corpusPath = line.getOptionValue("c");
            AlignedCorpus corpus = new AlignedCorpus();
            corpus.read(corpusPath, "en", "es",
                    line.hasOption("m") ? Integer.parseInt(line.getOptionValue("m")) : 0);
            lexicon.addFrequencies(corpus);
        }

        if (line.hasOption("w")) {
            lexicon.write(new File(line.getOptionValue("w")));
        }

        // Serve lexicon
        if (line.hasOption("s")) {
            String host = line.getOptionValue("t", "localhost");
            String queue = line.getOptionValue("q", "lexicon");
            String username = line.getOptionValue("u", "");
            String password = line.getOptionValue("v", "");
            RabbitHandler handler = new RabbitHandler(host, queue, username, password, lexicon);
            handler.ConnectAndListen();
        }
    }

    /*
     * Parse command-line arguments.
     */
    private static CommandLine ParseArgs(String[] args) throws ParseException {
        Options options = new Options();
        options.addOption("h", "help", false, "print this message");

        // Vanilla I/O
        options.addOption("r", "read", true, "read JSON file");
        options.addOption("w", "write", true, "write JSON file");

        // Rabbitmq
        options.addOption("s", "serve", false, "listen on local rabbitmq");
        options.addOption("t", "host", true, "rabbitmq host");
        options.addOption("q", "queue", true, "rabbitmq queue");
        options.addOption("u", "username", true, "rabbitmq username");
        options.addOption("v", "password", true, "rabbitmq password");

        // Parsing PanLex
        options.addOption("p", "panlex", true, "parse PanLex JSON");
        options.addOption("x", "pattern", true, "expression pattern");
        options.addOption("l", "languages", true, "comma-separated languages");

        // Concordance
        options.addOption("c", "corpus", true, "path to corpus (no suffixes)");
        // TODO(denero) make -m an int value with default 0
        options.addOption("m", "max", true, "maximum number of sentence pairs");

        CommandLineParser parser = new BasicParser();
        CommandLine line = parser.parse(options, args);

        if (line.hasOption("h")) {
            HelpFormatter formatter = new HelpFormatter();
            formatter.printHelp("lex", options);
        }
        return line;
    }
}

@@ -701,23 +701,59 @@
     
-// Parse PanLex
+List%3CString%3E languages = Arrays.asList(%22en%22, %22es%22);
 %0A   
@@ -777,17 +777,17 @@
 Option(%22
-p
+l
 %22)) %7B%0A  
@@ -800,22 +800,34 @@
     
-String path = 
+languages = Arrays.asList(
 line
@@ -843,18 +843,30 @@
 nValue(%22
-p%22
+l%22).split(%22,%22)
 );%0A     
@@ -872,150 +872,35 @@
     
+%7D%0A%0A
     
-PanLexJSONParser panLex = new PanLexJSONParser(new PanLexDir(path));%0A%0A            List%3CString%3E languages = Arrays.asList(%22en%22, %22es%22);%0A    
+    // Parse PanLex%0A
     
@@ -915,33 +915,33 @@
 line.hasOption(%22
-l
+p
 %22)) %7B%0A          
@@ -946,38 +946,22 @@
     
-    languages = Arrays.asList(
+String path = 
 line
@@ -973,38 +973,26 @@
 ptionValue(%22
-l%22).split(%22,%22)
+p%22
 );%0A         
@@ -986,33 +986,100 @@
 %22);%0A            
-%7D
+PanLexJSONParser panLex = new PanLexJSONParser(new PanLexDir(path));
 %0A            pan
@@ -1103,25 +1103,24 @@
 languages);%0A
-%0A
             
@@ -1793,24 +1793,83 @@
 edCorpus();%0A
+            // TODO(denero) Load all corpora for languages%0A
             

move languages out of panlex parsing