package com.geocities.cgbusch.hieropreter;
import java.applet.Applet;
import java.awt.Graphics;
import java.util.*;
import java.net.URL;
import java.io.StringReader;
import java.io.StringWriter;
import java.io.PrintWriter;
import java.io.BufferedReader;
import java.io.InputStreamReader;

/**
 * Main applet. 
 * <br> All code Copyright 2002 by Chris Busch cgbusch@yahoo.com
 * @author Chris Busch (cbusch) $Author: cbusch $
 * @since Mon Jan 07 15:27:05 2002
 * @version $Revision: 1.1 $ $Date: 2002/08/03 19:07:32 $
 * @see 
 */
public class Hieropreter extends Applet {

    private String status=CodeMatch.TITLE;

    public String getAppletInfo() {
	return CodeMatch.TITLE;
    }

    boolean ready=false;

    CodeMatch cm=new CodeMatch();


   public void init() {
       URL dicturl=null, gardurl=null;
       try{
	   showStatus("fetching dictionaries... AE to English");
	   String dictlocation, gardlocation;
	   dictlocation="egyptdictionary.txt";
	   gardlocation="gardiner2mdc.txt";
	   dicturl=new URL(getDocumentBase(),dictlocation);
	   BufferedReader dictin= new BufferedReader(new 
	       InputStreamReader(dicturl.openStream()));

	   showStatus("fetching dictionaries... Gardiner Map");
	   gardurl=new URL(getDocumentBase(),gardlocation);
	   BufferedReader gardin= new BufferedReader(new 
	       InputStreamReader(gardurl.openStream()));

	   showStatus("loading dictionaries... Gardiner Map");
	   cm.loadGMap(gardin);

	   showStatus("loading dictionaries... AE to English");
	   cm.loadDict(dictin);


	   dictin.close();
	   gardin.close();

	   showStatus("Ready");
	   ready=true;
       }catch(Exception x) {
	   StringWriter sw=new StringWriter();
	   PrintWriter pw=new PrintWriter(sw);
	   x.printStackTrace(pw);
	   pw.flush();
	   //status=" u="+dicturl+" u="+gardurl+" "+sw;
	   status = ""+sw;
	   showStatus(status);
       }
       repaint();
   }
    
    public void paint(Graphics g) {
	g.drawString(status, 25, 10);
    }

    public String findByEnglish(String in) {
	if( ready ) {
	    showStatus("Looking...");
	    String ans=cm.findByEnglish(in).toString();
	    showStatus("Ready...");
	    repaint();
	    return ans;
	}else{
	    return "Not Ready:"+status;
	}
    }
    
    public String translate(String in,String maxDepth) {
	int mx=100;
	try{
	    mx = Integer.parseInt(maxDepth);
	}catch(Exception x) {
	    //ignore it
	}
	if( ready ) {
	    showStatus(status="Translating... Please wait...");
	    repaint();
	    String ans=cm.translate(in,mx,0);
	    showStatus("Done translating.");
	    status=CodeMatch.TITLE;
	    repaint();
	    return ans;
	}else{
	    return "Not Ready:"+status;
	}
    }
}
