import java.util.Map;
import java.util.Set;
import java.net.MalformedURLException;
import java.net.URL;

public class WebSearch {
	/**
	 * Search the index for entries that match the keywords at least
	 * minFrequency times. Return a map from keyword matches to sets of webpage
	 * addresses.
	 * 
	 * @param index The webpageIndex to search
	 * @param minFrequency the minimum frequency a keyword must appear in an
	 *            entry for it to be counted as a match
	 * @param keywords The keywords to search.
	 * @return A map where the keys are the number of keywords that were matched
	 *         and the value is the set of webpage addresses that matched the
	 *         given number of keywords.
	 */
	public static Map<Integer, Set<URL>> search(WebpageIndex index, int minFrequency,
			String[] keywords) {

		return null;
	}

	/**
	 * A simple String formatter for a result returned by search.
	 * 
	 * @param results the result being formated.
	 * @return A formated representation of the results.
	 */
	public static String resultString(Map<Integer, Set<URL>> results) {
		String text = "";
		for (Integer count : results.keySet()) {
			if (count > 1)
				text += "Results with " + count + " keywords:\n";
			else
				text += "Results with only " + count + " keyword:\n";
			for (URL webpage : results.get(count)) {
				text += "\t" + webpage + "\n";
			}
		}
		return text;
	}

	/**
	 * An example of a test program for a web search. 
	 */
	public static void main(String[] args) {
		//index the first 50 websites it sees from cnn.com
		URL cnn = null;
		try {
			cnn = new URL("http://www.cnn.com/");
		} catch (MalformedURLException e) {
			e.printStackTrace();
		}
		
		WebpageIndex index = Webcrawler.createWebpageIndex(50, cnn);
		
		//get results that have the keywords at least two times
		Map<Integer, Set<URL>> warResults = search(index, 2, new String[] {"iraq",
				"terror", "afganistan"});
		System.out.println(resultString(warResults));
		
		Map<Integer, Set<URL>> econlmyResults = search(index, 2, new String[] {"economy",
				"stock"});
		System.out.println(resultString(econlmyResults));
	}
}
