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

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.jvnet.lookout.core.adapter.AbstractSearchAdapter;
import org.jvnet.lookout.core.adapter.SearchExecutor;
import org.jvnet.lookout.core.adapter.SearchQueryConverter;
import org.jvnet.lookout.core.model.SearchResult;
import org.jvnet.lookout.core.model.SearchResultLocation;
import org.jvnet.lookout.core.model.SearchResultProperty;
import org.jvnet.lookout.core.model.SearchResultSource;
import org.jvnet.lookout.core.model.SearchResultType;
import org.jvnet.lookout.core.util.Utilities;
import org.jvnet.lookout.google.desktop.model.DesktopSearchResult;

public class DesktopSearchAdapter extends AbstractSearchAdapter {
        
    private static final SearchResultSource GOOGLE_DESKTOP_SEARCH_SOURCE = 
    	new SearchResultSource("GOOGLE DESKTOP SEARCH SOURCE");
    
    @Override
    protected List<SearchResult> adaptSearchResults(List rawResults) {        
        
        List<SearchResult> results = new ArrayList<SearchResult>(rawResults.size());
        
        for (Iterator iter = rawResults.iterator(); iter.hasNext();) {
            DesktopSearchResultBean resultBean = (DesktopSearchResultBean) iter.next();
            
            DesktopSearchResult searchResult = new DesktopSearchResult();
            
            //Source
            searchResult.setSource(getSearchResultSource());
            
            //Name
            searchResult.setProperty(SearchResultProperty.NAME, Utilities.stripHTML(resultBean.getTitle()));
            
            //Type
            searchResult.setType(getModelTypeForBeanType(resultBean));
            
            //URL
            String url = (SearchResultType.EMAIL.equals(searchResult.getType())) ? resultBean.getCachedUrl() : resultBean.getUrl();
            searchResult.setProperty(SearchResultProperty.URL, url);
            
            //Snippet
            String snippetValue = resultBean.getSnippet();
            
            if(Utilities.isValidValue(snippetValue)) {
                searchResult.setProperty(SearchResultProperty.SNIPPET, Utilities.stripHTML(snippetValue));
            }
            
            //Location
            searchResult.setProperty(SearchResultProperty.LOCATION, SearchResultLocation.LOCAL);
            
            //Save it
            results.add(searchResult);
        }
        return results;
    }
    
    @Override
    protected SearchExecutor createSearchExecutor() {        
        return new DesktopSearchExecutor();
    }
    
    public SearchQueryConverter createSearchQueryConverter() {
        return new DesktopQueryConverter();
    }
    
    protected SearchResultType getModelTypeForBeanType(DesktopSearchResultBean bean) {
        
        SearchResultType type = SearchResultType.OTHER;
        
        String desktopType = bean.getCategory();
        
        if(DesktopSearchResultBean.WEB.equals(desktopType)) {
            type = SearchResultType.HTML;
        } else if (DesktopSearchResultBean.EMAIL.equals(desktopType)) {
            type = SearchResultType.EMAIL;
        } else if (DesktopSearchResultBean.FILE.equals(desktopType)) {
            type = Utilities.getTypeForURL(bean.getUrl(), SearchResultType.OTHER);            
        } else if (DesktopSearchResultBean.CHAT.equals(desktopType)) {
        	//Chat files are handled through the default mechanisms...
            type = SearchResultType.OTHER;
        }  
        return type;
    }    
    
    public SearchResultSource getSearchResultSource() {
        return DesktopSearchAdapter.GOOGLE_DESKTOP_SEARCH_SOURCE;
    }
    
}
