package com.matthewjensen.project;

import com.matthewjensen.project.modules.Anagrams;
import com.matthewjensen.project.modules.LetterInventory;
import com.matthewjensen.project.modules.AnagramMain;
import java.io.FileNotFoundException;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.*;
import java.io.*;
import org.slf4j.*;

@SpringBootApplication
public class ProjectApplication {

	public static void main(String[] args) {
		SpringApplication.run(ProjectApplication.class, args);
	}

}
@RestController
class TwentyQuestions {
    private static final Logger logger = LoggerFactory.getLogger(TwentyQuestions.class);

    @RequestMapping("/")
    public void home() {
        if (1 == 1) {
            logger.info("Hello mobile user!");
        } else if (3 == 3) {
            logger.info("Hello tablet user!");
        } else {
            logger.info("Hello desktop user!");
        }
    }
    // 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 = true;
    
    @RequestMapping("/20questions")
    public String index() 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);
        
        int i = 0;
        // loop to get/solve each phrase
        while (phrase.length() > 0 || i > 1) {
            i++;
            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;
            if (DEBUG) {
                line = "4";
            } else {
                line = console.nextLine().trim();
            }
            
            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);
        }
        return "Hello There";
    }
    
    // Helper to prompt for a phrase to generate anagrams.
    public static String getPhrase(Scanner console) {
        return "hello";
        /*
        System.out.println();
        System.out.print("Phrase to scramble (Enter to quit)? ");
        String phrase = console.nextLine().trim(); 
        if (DEBUG) {
            phrase = "barbara bush";
        }
       return phrase;
       */
    }
}
