package com.github.timp.anagram;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;

public class Query {
  private final Set searchesSoFar;
  ArrayList<String> keys;
  LetterBag ourLetters;
  HashSet<String> possibles;
  boolean done;

  public Query(Set searchesSoFar, ArrayList<String> keys, String key, HashSet<String> possibles, LetterBag letters) {
    this.searchesSoFar = searchesSoFar;
    this.keys = keys;
    keys.add(key);
    done = ! searchesSoFar.add(new ComparableSearchRepresentation(keys).toKey());
    ourLetters = letters.copy().remove(key);
    this.possibles = possibles;
  }

  public boolean producesResults() {
    if (done) {
      return false;
    }
    if (ourLetters.count() != 0) {
      HashSet<String> remainingPossibilities = new HashSet<>();
      for (String p: possibles) {
        if (ourLetters.contains(p)) {
          remainingPossibilities.add(p);
        }
      }
      boolean found = false;
      for (String p : remainingPossibilities) {

        ArrayList<String> childKeys = (ArrayList)keys.clone();
        Query childQuery = new Query(searchesSoFar,
            childKeys, p, remainingPossibilities, ourLetters);
        if (childQuery.producesResults()) {
          found = true;
          keys.add(p);
        }
      }
      return found;
    } else {
      System.err.println("Ended on " + keys );
      return true;
    }
  }
}

@@ -160,16 +160,82 @@
 sSoFar;%0A
+  private final Tree%3CString%3E keyTree;%0A  private final String key;%0A
   ArrayL
@@ -328,21 +328,356 @@
 %0A%0A  
-public Query(
+/**%0A   *%0A   * @param keyTree The results tree of Anagram keys%0A   * @param searchesSoFar All searches, sorted alphabetically%0A   * @param keys The list of previous keys%0A   * @param key the potential key%0A   * @param possibles the collection of legal keys%0A   * @param letters the remaining letters to include%0A   */%0A  public Query(Tree%3CString%3E keyTree, 
 Set 
@@ -771,24 +771,52 @@
  letters) %7B%0A
+    this.keyTree = keyTree;%0A
     this.sea
@@ -873,54 +873,66 @@
     
-keys.add(key);%0A    done = ! searchesSoFar.add(
+this.key = key;%0A    keys.add(key);%0A    String searchKey = 
 new 
@@ -975,16 +975,58 @@
 .toKey()
+;%0A    done = ! searchesSoFar.add(searchKey
 );%0A    o
@@ -1135,24 +1135,25 @@
 Results() %7B%0A
+%0A
     if (done
@@ -1178,24 +1178,25 @@
 alse;%0A    %7D%0A
+%0A
     if (ourL
@@ -1214,16 +1214,150 @@
 t() 
-!
+=
 = 0) %7B%0A
+      Tree%3CString%3E childKeyResultTree = new Tree%3CString%3E(key);%0A      keyTree.add(childKeyResultTree);%0A      return true;%0A    %7D else %7B%0A
     
@@ -1542,24 +1542,84 @@
    %7D%0A      %7D
+%0A%0A      Tree%3CString%3E childKeyResultTree = new Tree%3C%3E(key);%0A%0A
 %0A      boole
@@ -1785,16 +1785,61 @@
 w Query(
+%0A            childKeyResultTree,%0A            
 searches
@@ -1871,11 +1871,35 @@
 eys,
- p,
+%0A            p,%0A           
  rem
@@ -1918,16 +1918,28 @@
 ilities,
+%0A           
  ourLett
@@ -1952,20 +1952,35 @@
         
-if (
+boolean terminal = 
 childQue
@@ -1999,16 +1999,38 @@
 esults()
+;%0A        if (terminal
 ) %7B%0A    
@@ -2053,24 +2053,26 @@
 ue;%0A        
+//
   keys.add(p
@@ -2078,105 +2078,70 @@
 p);%0A
-        %7D
 %0A     
- %7D%0A
      
- return found;%0A    %7D else %7B%0A      System.err.println(%22Ended on %22 + keys );
+keyTree.add(childKeyResultTree);%0A%0A        %7D%0A      %7D
 %0A   
@@ -2146,28 +2146,29 @@
      return 
-true
+found
 ;%0A    %7D%0A  %7D%0A

Refactor until it works