package genbook;

import java.io.IOException;
import java.math.BigInteger;
import com.yahoo.search.*;

import crawler.UrlGrab;

public class GenBookSearch {
	private String searchWord = null;
	private SearchClient client;
	
	public GenBookSearch()
	{
		//Create the search client. Pass it your application ID.
		client = new SearchClient("Jm3V0PbV34GKpO58IjWbVvW26XjoKlrkriC2D4idXRBm8No3VDoCCjQLhBqsjJ9wRVI");
		
	}

	public String search( String searchString){
		try {	
			
			this.searchWord = searchString;

			//this is the search string

			//this one works for google
			//String s1 = "intext:"+"\""+search+"\"";

			//for now this is for Yahoo
			//String s1 = "\""+searchWord+"\"";
			String s1 = searchWord;
			System.out.println("search string is.. " + s1);
			WebSearchRequest request = new WebSearchRequest(s1);
			request.setFormat("html");
			//request.setAdultOk(true);
			request.setType("phrase");
	
			//request.setType("phrase");
			

			WebSearchResults results;
			results = client.webSearch(request);
			//to look at the results
			//System.out.println("results.listResults().length"+results.listResults().length);
			int randomWord = (int) (Math.random() * results.listResults().length);
			
			for (int i = 0; i < results.listResults().length; i++) {
				WebSearchResult result = results.listResults()[i];
				//String summary = result.getSummary();
				if (i == randomWord){
					
					
					String summary = result.getSummary();
					WordInSentence tempContent = new WordInSentence(summary,searchWord);
					String output = cleanupResult(tempContent.getSentences().toString());
					
					
					/*
					 //this bit goes to the result url and gets back content
					//System.out.println("RAWTEXT --> "+summary);
					//maybe  you will have to go to the page here and get results
					System.out.println("grabbing content from URL "+result.getUrl());
					
	            	UrlGrab getContent = new UrlGrab(result.getUrl());
	     
	            	//picks out the sentence that contains the searched word/phrase
	            	System.out.println("looking for.. "+ searchWord.toUpperCase());
	            	WordInSentence tempContent = new WordInSentence(getContent.getUrlContent(),searchWord);
	            	
	            	System.out.println("cleaning up the matched sentence.. ");
					String output = cleanupResult(tempContent.getSentences().toString());
					
					
					System.out.println("clean result--> "+output + "\n");
					*/
					
					return output;
				}
				//System.out.println("   " + (i + 1) + ": " + result.getTitle() + " - " + result.getUrl() + " /n " + summary);
				
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SearchException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	public String getRandomResult(String keyword_){
		StringBuffer resultBuffer = new StringBuffer();
		//resultBuffer = search(keyword_);
		//take the search results and return one randomly selected result
		//strip out the tags and return string
		return null;
	}

	public String cleanupResult(String r_){
		String dirtyResult = r_;
		//here is remove the tags once all the results (10) from a search query are back
		TagRemover tagremove = new TagRemover(dirtyResult);
		String cleanOutput = tagremove.removeTags();
		return cleanOutput;
	}
}
