package de.tuebingen.uni.sfs.spellcheck.cli;

import com.google.common.base.Charsets;
import de.tuebingen.uni.sfs.spellcheck.dictionary.DictomatonDictionary;
import de.tuebingen.uni.sfs.spellcheck.util.DictionaryUtil;
import eu.danieldk.dictomaton.DictionaryBuilderException;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * A simple spell checker.
 */
public class SpellChecker {
	public static void main(String[] args) throws IOException, DictionaryBuilderException {
		
		
		DictomatonDictionary dictomatonDictionaryWhitelisted = DictionaryUtil.loadDictionary(DictionaryUtil.loadWhitelist()); 

        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in, Charsets.UTF_8));
        try {
            String word;
            while ((word = reader.readLine()) != null) {
                word = word.trim();
                
                if (word.split("\\s+").length != 1) {
                	
                	System.out.println("Enter one word");

                } else {
                	
                	if (dictomatonDictionaryWhitelisted.suggest(word) == null || dictomatonDictionaryWhitelisted.suggest(word).size() == 0) {
                		
                		if (dictomatonDictionaryWhitelisted.containsOrNot(word) == false) {
                			
                			System.out.print("The word is not in the dictionary, and no suggestion is available");
                			
                		} else {
                			
                			System.out.print(word + " (p = " + dictomatonDictionaryWhitelisted.probability(word) + ")");
                			
                		}
                		
                	} else {
                		
                		
                		Map<String, Double> candidates = new HashMap<String, Double>();
                		
                		for (String candidate : dictomatonDictionaryWhitelisted.suggest(word)) {
                			
                			candidates.put(candidate, dictomatonDictionaryWhitelisted.probability(candidate));
                			
                		}
                		
                		if (dictomatonDictionaryWhitelisted.containsOrNot(word) == true) {
                			
                			candidates.put(word, dictomatonDictionaryWhitelisted.probability(word));
                			
                		}
                		
                		Map<String, Double> result = new LinkedHashMap<>();

                		candidates.entrySet().stream()
                                .sorted(Map.Entry.<String, Double>comparingByValue().reversed())
                                .forEachOrdered(x -> result.put(x.getKey(), x.getValue()));

                        //System.out.println(result);
                		
                        for (String key : result.keySet()) {

                        	System.out.println(key + " (p = " + result.get(key) + ")");
                        	
                        }
                        
                	}
                	
                }

            }
        } catch (IOException e) {
            System.err.println("Error reading from the standard input.");
        } finally {
            try {
                reader.close();
            } catch (IOException ignore) {
            }
        }

		
		
	}

	private static DictomatonDictionary loadDictionary() {
		DictomatonDictionary dictomatonDictionary = null;
		try {
			dictomatonDictionary = DictionaryUtil.loadDictionary();
		} catch (IOException e) {
			System.err.println("Error loading dictionary.");
			System.exit(1);
		} catch (DictionaryBuilderException e) {
			System.err.println("Error constructing dictionary.");
			System.exit(1);
		}
		return dictomatonDictionary;
	}

}