// Copyright 1996, Marimba Inc. All Rights Reserved.
// @(#)Dict.java, 1.1, 11/18/96

package wordsmith;

import java.net.*;
import java.io.*;
import java.awt.*;
import java.util.*;

import marimba.channel.*;
import marimba.gui.*;
import marimba.io.*;
import marimba.desktop.*;

/**
 * Comment here
 *
 * @author      Carl W. Haynes III
 * @version     1.1, 11/18/96
 */

public class Dict {

    ApplicationContext app;

    String filePath = "c:\\work\\channels\\wordsmith\\dict\\dict.txt";	
    String dictPath = "dict/dict.txt";

    int letterIndex[] = {
	0,
	59432,
	117813,
	213258,
	271128,
	311335,
	354023,
	386986,
	422847,
	464338,
	473158,
	483380,
	514246,
	571414,
	591390,
	629389,
	709056,
	714051,
	780137,
	890271,
	941100,
	968570,
	985442,
	1009779,
	1010442,
	1013573
    };

    public Dict() {
    }

    public Dict(ApplicationContext app) {
	this.app = app;
    }


    public boolean checkWord(String word) {
	int idx = letterIndex[(word.charAt(0) - 'A')];

	FastInputStream fis;
	if (app == null) {
	    File f = new File(filePath);
	    try {
		fis = new FastInputStream(f); 
	    }
	    catch(IOException e) {
		e.printStackTrace();
		return(false);
	    }
	}
	else {
	    URL url;
	    try {
		url = new URL(app.getBase(), dictPath);
		try {
		    fis = new FastInputStream(url.openStream()); 
		}
		catch(IOException e) {
		    e.printStackTrace();
		    return(false);
		}
	    }
	    catch(MalformedURLException e) {
		e.printStackTrace();
		return(false);;
	    }
	}


	try {
	    fis.skipBytes(idx);
	    String s = "";
	    while ((s = fis.readLine()) != null) {
		int i = s.compareTo(word);
		if (i == 0)
		    return(true);
		else if (i > 0) {
		    //DebugStr(s);
		    return(false);
		}
	    }
	    fis.close();
	}
	catch(IOException e) {
	    e.printStackTrace();
	    return(false);
	}


	return(false);
    }


    public boolean loadDict() {
	String target = "A";
	int idx = 0;
	DebugStr(target + " / " + idx);

	FastInputStream fis;
	if (app == null) {
	    File f = new File(filePath);
	    try {
		fis = new FastInputStream(f); 
	    }
	    catch(IOException e) {
		e.printStackTrace();
		return(false);
	    }
	}
	else {
	    URL url;
	    try {
		url = new URL(app.getBase(), dictPath);
		try {
		    fis = new FastInputStream(url.openStream()); 
		}
		catch(IOException e) {
		    e.printStackTrace();
		    return(false);
		}
	    }
	    catch(MalformedURLException e) {
		e.printStackTrace();
		return(false);;
	    }
	}

	try {
	    String s = "";
	    while ((s = fis.readLine()) != null) {
		idx += s.length() + 1;
		String newTarget = s.substring(0, 1);
		if (!newTarget.equals(target)) {
		    target = newTarget;
		    DebugStr(target + " / " + idx);
		}
	    }
	    fis.close();
	}
	catch(IOException e) {
	    e.printStackTrace();
	    return(false);
	}


	return(false);
    }

    /**
     * for debugging
     */
    public void DebugStr(String s) {
        System.out.println(this.getClass().getName() + ": " + s);
	System.out.flush();
    }

}



