import javax.swing.text.html.*;
import javax.swing.text.html.parser.*;
import javax.swing.*;
import java.net.*;
import java.io.*;
import java.util.*;

public class Kanji extends Thread
{ 
  String theKanjiToLookUp, decodedKanji, href, rawText;
  String[] definitions;
  ArrayList imagesearchers;
  StoryPeek theApplet;
 
 
  Kanji(StoryPeek papp, String s)
  {
    theApplet = papp;
    decodedKanji = s;
    theKanjiToLookUp = URLUTF8Encoder.encode(s);
    href = "http://www.csse.monash.edu.au/~jwb/cgi-bin/wwwjdic.cgi?1MMJ"+theKanjiToLookUp;  
    rawText = "";
    imagesearchers = new ArrayList();
   // theApplet.imgdrawerLeft = new ImageDrawer(theApplet,theApplet.width / 2,theApplet.height,0);
    theApplet.imgdrawerRight = new ImageDrawer(theApplet,theApplet.width / 2,theApplet.height,theApplet.width/2);
    this.start();   
  }
  
  public void start() { super.start(); }
 
  public void run()
  {
    parseURL();
    
    //System.out.println(rawText);
    
    try
    {
      sleep(1000);
    }
    catch (InterruptedException e)
    {
    }
  
    imagesearchers.add(new ImageSearcher(theApplet, decodedKanji, "right"));
    System.out.println("added image searcher on: "+theKanjiToLookUp);
  }
     
  void parseURL()
  { 
    HTMLEditorKit.ParserCallback callback = 
      new HTMLEditorKit.ParserCallback ()
      {
        public void handleText(char[] data, int pos)
        {
          rawText = rawText+(new String(data));
        }
      };
      try
      {
        URL url = new URL(href);
        InputStream instream = url.openStream();
        Reader reader = new BufferedReader(new InputStreamReader(instream));
        new ParserDelegator().parse(reader, callback, true);
        instream.close();
      } 
    catch (IOException e) {
      e.printStackTrace(); 
    }
  }
  
//  String[] getDefinitions()
//  {
//    if (rawText.indexOf("; ") > -1)
//    {
//      definitions = rawText.split("; ");                                                        // divide the text into an array of Strings, separating by semicolons;
//      definitions[0] = definitions[0].split(" ")[(definitions[0].split(" ").length - 1)];       // take the first chunk and cut out all but the last word (ie, the first definition
//      
//      for(int i = 0; i < definitions.length; i++)
//      {
//        if (definitions[i].indexOf("SOD") > -1)
//        {
//          definitions[i] = definitions[i].split(" ")[definitions[i].split(" ").length - 1];
//          
//          if (definitions[definitions.length - 1].equals("(Details)"));
//          {
//            java.lang.reflect.Array.set(definitions, definitions.length - 1, "");
//          }
//        }
//      }
//    }
//    else
//    {
//      definitions = rawText.split(" SOD");
//      for(int i = 0; i < definitions.length; i++)
//      {
//        definitions[i] = definitions[i].split(" ")[(definitions[i].split(" ").length - 1)];
//      }
//      
//      if (definitions[definitions.length - 1].equals("(Details)"));
//      {
//        java.lang.reflect.Array.set(definitions, definitions.length - 1, "");
//      }
//    }
//    
//    try
//    {
//      this.sleep(10);  
//    }
//    catch (InterruptedException e)
//    {
//      return definitions;
//    }
//    
//    return definitions;
//   
//  } 
} 
