/**
CSE 143, Winter 2010, Marty Stepp
Homework 6 (Anagrams)

AnagramMain is a client program that prompts a user for the name of a
dictionary file and then gives the user the opportunity to find anagrams of
various phrases.  It constructs an Anagrams object to do the actual
search for anagrams that match the user's phrases.

@author Stuart Reges and Marty Stepp
@version originally written by reges on 5/9/2005; modified by stepp on 2/6/2010
*/

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Collections;
import java.util.Scanner;
import java.util.Set;
import java.util.TreeSet;

public class AnagramMain {
    // dictionary file to use for input (change to dict2, dict3)
    private static final String DICTIONARY_FILE = "dict1.txt";
    
    // set to true to test runtime and # of letter inventories created
    private static final boolean TIMING = true;
    private static final boolean DEBUG = false;
    
    
    public static void main(String[] args) throws FileNotFoundException {
        System.out.println("Welcome to the CS 145 anagram solver.");
        System.out.println("Using dictionary file " + DICTIONARY_FILE + ".");

        // read dictionary into a set
        Scanner input = new Scanner(new File(DICTIONARY_FILE));
        Set<String> dictionary = new TreeSet<String>();
        while (input.hasNextLine()) {
            dictionary.add(input.nextLine());
        }
        dictionary = Collections.unmodifiableSet(dictionary);   // read-only

        // create Anagrams object for, well, solving anagrams
        Anagrams solver = new Anagrams(dictionary);

        // get first phrase to solve
        Scanner console = new Scanner(System.in);
        String phrase = getPhrase(console);
        
        if (DEBUG) {
            phrase = "barbara bush";
        }
        // loop to get/solve each phrase
        while (phrase.length() > 0) {
            System.out.println("All words found in \"" + phrase + "\":");
            Set<String> allWords = solver.getWords(phrase);
            System.out.println(allWords);
            System.out.println();

            System.out.print("Max words to include (Enter for no max)? ");
            String line = console.nextLine().trim();
            if (DEBUG) {
                line = "4";
            }
            
            long startTime = System.currentTimeMillis();
            if (line.length() > 0) {
                // use a max
                int max = new Scanner(line).nextInt();
                solver.print(phrase, max);   // print all anagrams of phrase
            } else {
                // no max
                solver.print(phrase);        // print all anagrams of phrase
            }
            long endTime = System.currentTimeMillis();
            System.out.println();
            
            // 12247 ms elapsed, 2594392 unique LetterInventory object(s) created
            if (TIMING) {
                long elapsed = endTime - startTime;
                int inventories = LetterInventory.getInstanceCount();
                System.out.println(elapsed + " ms elapsed, " + inventories + 
                        " unique LetterInventory object(s) created");
                LetterInventory.resetInstanceCount();
            }

            // get next phrase to solve
            phrase = getPhrase(console);
        }
    }
    
    // Helper to prompt for a phrase to generate anagrams.
    public static String getPhrase(Scanner console) {
        System.out.println();
        System.out.print("Phrase to scramble (Enter to quit)? ");
        return console.nextLine().trim();
    }
}
