//56729
/**
 * @author kst
 */
package commons.collections.trie;


import java.io.BufferedReader;
import java.io.File;
import java.io.InputStreamReader;


/**
 * Einzige ausfuehrbare Klasse des Projekts
 */
public class DictFSA //implements StringProcessorInterface
{

   public static boolean  quiet   = false;

   ITrie                  T       = null;


   public static void main (String[] argv) throws Exception
   {
      if (argv.length < 2)
      {
         println("Main <triePackage> <lexFile> <true: run as server>");
         System.exit(-1);
      }
      //    public static int PORT = 10025;

      new DictFSA(argv[0], argv[1], argv.length == 3 ? Integer
            .parseInt(argv[2]) : -1);
   }


   public DictFSA (String triePackage, String lexFile, int port)
         throws Exception
   {

      T = ITrie.getTrie(triePackage);

      //Parser geladen? dann noch ein paar Einstellungen und satzweise parsen:
      if (T != null)
      {

         ITrie temp = T.loadTrie(lexFile);

         if (null != temp)
         {
            T = temp;
         }
         else
         {
            File file = new File(lexFile);
            if (file.exists() && file.canRead())
            {
               println("Constructing ...");

               long x1 = System.currentTimeMillis();

               DictionaryReader lexReader = new DictionaryReader(file);
               lexReader.fillTrie(T);

               ITrieInfo iti = new ITrieInfo((ITrie) T, lexReader);
               long x2 = System.currentTimeMillis();
               System.out.println("\nTime: " + (x2 - x1) + " millis");
               System.out.println("\n" + iti.getInfo());

               T.storeTrie(lexFile);
            }
            else
            {
               println("No such dictionary file: " + file.getAbsolutePath());
               return;
            }
         }

         System.out.println(T.getTotalStates() + " States");


//         if (port != -1)
//         {
//            System.out.println("\nStarting socket server...");
//            StringServer server = new StringServer(this, port);
//         }
//         else
         {

            System.out
                  .print("\nEnter # for trie printout. Empty line terminates.\n");

            BufferedReader d = new BufferedReader(new InputStreamReader(
                  System.in));

            while (d != null)
            {
               System.out.print("Input: ");
               String cmd = d.readLine().trim();

               if (cmd.trim().length() == 0)
               {
                  break;
               }
               else if (cmd.trim().equals("#"))
               {
                  new ITriePrinter(T).println();
               }
               else
               {
                  String r = T.accept(cmd);
                  if (r != null)
                  {
                     System.out.println(r);
                  }
                  else
                  {
                     System.out.println("?");
                  }
               }
            }
         }
      }

      //----------------------------------------------------------------------
   }


   public String processString (String in)
   {
      String r = T.accept(in);
      return r == null ? "" : r;
   }


   public String processString2 (String in)
   {
      String r = "";

      if (in != null)
      {
         if (in.indexOf(" ") != -1)
         {
            StringBuffer sb = new StringBuffer();
            String[] w = in.split(" ");

            for (int i = 0; i < w.length; i++)
            {
               String v = T.accept(w[i]);

               sb.append(v == null ? w[i] : v);
               sb.append(i < w.length ? " " : "");
            }

            r = sb.toString();
         }
         else
         {
            r = T.accept(in);
            r = r == null ? in : r;
         }
      }

      return r;
   }


   public static void println (String v)
   {
      System.out.println(v);
   }


   public static void print (String v)
   {
      System.out.print(v);
   }

}