package ca.infodata.ofys.ui.controls;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.text.ParseException;
import java.util.List;

import ca.infodata.ofys.ui.controls.hunspell.HunspellDictionary;
import ca.infodata.ofys.ui.controls.hunspell.HunspellWord;

public class Hunspell {

	private HunspellDictionary dict;
	private static Hunspell hunspell = null;
	
	public static Hunspell getInstance(String dic, String aff) {
		if (hunspell==null) {
			hunspell = new Hunspell(dic, aff);
		}
		return hunspell;
	}
	
	public Hunspell(String dic, String aff) {
		InputStream isDic = null;
		InputStream isAff = null;

	    try {
	        isDic = new FileInputStream(dic);
	        isAff = new FileInputStream(aff);
	        dict = new HunspellDictionary(isAff, isDic);
	        
	        isDic.close(); 
	        isAff.close(); 
	    } catch (FileNotFoundException e) {
	        // TODO Auto-generated catch block
	        e.printStackTrace();
	    } catch (IOException e) {
	        // TODO Auto-generated catch block
	        e.printStackTrace();
	    } catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
	    	if (isDic!=null) {
	    		try {
					isDic.close();
				} catch (IOException e) {
				}
	    	}
	    	if (isAff!=null) {
	    		try {
	    			isAff.close();
	    		} catch (IOException e) {
	    		}
	    	}
	    }
	}

	public void add(String badString) {
		// TODO Auto-generated method stub
		
	}

	public void addWithAffix(String word1, String word2) {
		// TODO Auto-generated method stub
		
	}

	public List<String> suggest(String text) {
		List<HunspellWord> words = dict.lookupWord(text.toCharArray(), 0, text.length());
		if (words.isEmpty()) {
			return null;
		}
		for (HunspellWord hw : words) {
			System.out.println(hw);
		}
		return null;
	}

	public boolean isCorrect(String text) {
		List<HunspellWord> words = dict.lookupWord(text.toCharArray(), 0, text.length());
		if (words.isEmpty()) {
			return false;
		}
		for (HunspellWord hw : words) {
			System.out.println(hw);
		}
		return true;
	}

}
