/**
 * Created on Jul 3, 2005
 * $id:  $
 */
package org.jvnet.lookout.google.web.adapter;

import java.util.List;
import java.util.Map;

import org.jvnet.lookout.core.adapter.SearchQueryConverter;
import org.jvnet.lookout.core.model.SearchQuery;
import org.jvnet.lookout.core.model.SearchQuery.Parameter;
import org.jvnet.lookout.core.util.Utilities;
import org.jvnet.lookout.google.web.Plugin;
import org.jvnet.lookout.google.web.preference.Preferences;

public class WebQueryConverter implements SearchQueryConverter {

	private static final String fileTypeParameterName = "filetype:";
	private static final String inTitleParameterName = "allintitle:";
	private static final String inUrlParameterName = "allinurl:";
	private static final String inTextParameterName = "allintext:";	
	private static final String dateRangeParameterName = "daterange:";	
	private static final String dateSeparator = "-";	
	private static final String siteParameterName = "site:";	
	
	private static final String space = " ";
	private static final String orOperator = "OR";
	private static org.eclipse.core.runtime.Preferences searchWebPreferences;
	
	public WebQueryConverter() {
	    searchWebPreferences = Plugin.getDefault().getPluginPreferences();    	
	}
	
    public String convert(SearchQuery query) {

        Map<Parameter, String> parameters = query.getParameters();

        StringBuffer queryBuffer = new StringBuffer();

        //Check preferences and add any of the needed additional search flags...

        //Document part location params need to done right before the query itself
        queryBuffer.append(getDocumentPartLocationRestriction());

        //Keywords...
        queryBuffer.append(parameters.get(Parameter.KEYWORDS));

        //Site restrictions need to be done after the query itself
        queryBuffer.append(getSiteRestriction());
        
        //Date range needs to be done after the query itself
        queryBuffer.append(getDateRangeRestriction());
        
        //File type params need to done after the query itself
        queryBuffer.append(getFileTypeRestriction());
        
        return queryBuffer.toString();
    }
    
    private String getSiteRestriction() {
    	
    	String result = "";
    	
    	if(!searchWebPreferences.isDefault(Preferences.SiteToSearch)) {    		
        	
    		result = 
        		space +
        		siteParameterName + 
        		searchWebPreferences.getString(Preferences.SiteToSearch);    		    		
    	}
    	return result;
    }
    
	private String getDateRangeRestriction() {
    	
    	String result = "";
    	
    	if(!searchWebPreferences.isDefault(Preferences.DateToSearchFrom)) {    		
        	
    		result = 
        		space +
        		dateRangeParameterName + 
        		searchWebPreferences.getInt(Preferences.DateToSearchFrom) + 
        		dateSeparator + 
        		Utilities.getNowInJulian();    		    		
    	}
    	return result;
    }
    
    private String getDocumentPartLocationRestriction() {
    	
    	String result = "";
    	
    	if(!searchWebPreferences.isDefault(Preferences.PartOfResourceToSearch)) {
    		
    		String partToSearchIn = searchWebPreferences.getString(Preferences.PartOfResourceToSearch);
    		
    		if(Preferences.BodyPart.equals(partToSearchIn)) {
    			result = inTextParameterName;        			
    		} else if(Preferences.UrlPart.equals(partToSearchIn)) {
    			result = inUrlParameterName;        			
    		} else if(Preferences.TitlePart.equals(partToSearchIn)) {
    			result = inTitleParameterName;
    		} else {
    			//Any other part is currently being ignored
    			return result;
    		}    		
    	}
    	return result;
    }
    
    private String getFileTypeRestriction() {
    	
    	String result = "";
    	
    	if(!(searchWebPreferences.getBoolean(Preferences.SearchAllFiles) ||
    			searchWebPreferences.isDefault(Preferences.SearchableFileTypes))) {
    		
    		String resourcesToSearch = searchWebPreferences.getString(Preferences.SearchableFileTypes);
    		
    		List<String> fileTypes = Utilities.createList(resourcesToSearch, ",");
    		
    		StringBuffer resultBuffer = new StringBuffer();
    		
    		int size = fileTypes.size();
    		for (int i = 0; i < size; i++) {
				String fileType = fileTypes.get(i);
				
				resultBuffer.append(space);
				resultBuffer.append(fileTypeParameterName);
				resultBuffer.append(fileType);
				
				if(i + 1 < size) {
					resultBuffer.append(space);
					resultBuffer.append(orOperator);
				}
			}
    		result = resultBuffer.toString();
    	}
    	return result;
    }
}
