/**
 * Created on Jul 24, 2005
 * $id:  $
 */
package org.jvnet.lookout.ui.core.tree.sort.item;

import java.util.HashMap;
import java.util.Map;

import org.eclipse.jface.viewers.Viewer;
import org.jvnet.lookout.core.model.SearchResultProperty;
import org.jvnet.lookout.core.model.SearchResultType;
import org.jvnet.lookout.ui.core.tree.SearchResultItem;
import org.jvnet.lookout.ui.core.tree.sort.SearchResultItemSorter;

/**
 * <code>TypeSorter</code> sorts SearchResultItem 
 * instances given their types. It makes no use of categoriesto bucket the 
 * SearchResultItem instances. The order within the categories is not altered.  
 * 
 * <p>
 * @author Cleo Barretto
 *
 */
public class TypeSorter implements SearchResultItemSorter {

    private Map<SearchResultType, Integer> typeToIndex;
    
    public TypeSorter() {
        typeToIndex = new HashMap<SearchResultType, Integer>(15);
        
        updateIndex();
    }
    
    /**
     * Does not sort items within each category.
     * 
     * @see org.jvnet.lookout.ui.core.tree.sort.SearchResultItemSorter#compare(org.eclipse.jface.viewers.Viewer, org.jvnet.lookout.ui.core.tree.SearchResultItem, org.jvnet.lookout.ui.core.tree.SearchResultItem)
     */
    public int compare(Viewer viewer, SearchResultItem item1, SearchResultItem item2) {  
    	
    	//Sort only if these are the root results items
    	if(!(item1.getParent() instanceof SearchResultItem)) {
    		return 
    		getSearchResultTypeSortIndex((SearchResultType)item1.getPropertyValue(SearchResultProperty.TYPE)) 
    		-
    		getSearchResultTypeSortIndex((SearchResultType)item2.getPropertyValue(SearchResultProperty.TYPE));
    	} else {
    		return 0;
    	}
    }

    /**
     * Categorizes items in this order:
     * 
     * <ol>
     * <li>EMAIL</li>
     * <li>PDF</li>
     * <li>DOC</li>
     * <li>RTF</li>
     * <li>JAVA</li>
     * <li>HTML</li>
     * <li>PPT</li>
     * <li>XML</li>
     * <li>XSD</li>
     * <li>XSL</li>
     * <li>PS</li>
     * <li>JAVA CLASS</li>
     * <li>OTHER</li>
     * </ol>
     * 
     * @see org.jvnet.lookout.ui.core.tree.sort.SearchResultItemSorter#category(org.jvnet.lookout.ui.core.tree.SearchResultItem)
     */
    public int category(SearchResultItem item) {
        
        int category;
        
        SearchResultType itemType = (SearchResultType)item.getPropertyValue(SearchResultProperty.TYPE);
        
        category = getSearchResultTypeSortIndex(itemType);
        if(category < 0) {
            category = Integer.MAX_VALUE;    
        }
        
        return category;
    }
    
    public void setSearchResultTypeSortIndex(SearchResultType type, int index) {
        typeToIndex.put(type, index);
    }

    /**
     * Returns -1 if this provided type is not indexed.
     *  
     * @param type
     * @return
     */
    public int getSearchResultTypeSortIndex(SearchResultType type) {
        Integer index = typeToIndex.get(type);
        return (null == index) ? -1: index.intValue();
    }

    public void updateIndex() {
        
        int i = 0;
        
        setSearchResultTypeSortIndex(SearchResultType.EMAIL, i++);
        setSearchResultTypeSortIndex(SearchResultType.PDF, i++);
        setSearchResultTypeSortIndex(SearchResultType.DOC, i++);
        setSearchResultTypeSortIndex(SearchResultType.JAVA, i++);
        setSearchResultTypeSortIndex(SearchResultType.CPP, i++);        
        setSearchResultTypeSortIndex(SearchResultType.C, i++);        
        setSearchResultTypeSortIndex(SearchResultType.TXT, i++);
        setSearchResultTypeSortIndex(SearchResultType.HTML, i++);
        setSearchResultTypeSortIndex(SearchResultType.PPT, i++);
        setSearchResultTypeSortIndex(SearchResultType.XML, i++);
        setSearchResultTypeSortIndex(SearchResultType.XSD, i++);
        setSearchResultTypeSortIndex(SearchResultType.XLS, i++);
        setSearchResultTypeSortIndex(SearchResultType.PS, i++);
        setSearchResultTypeSortIndex(SearchResultType.JAVA_CLASS, i++);
        setSearchResultTypeSortIndex(SearchResultType.PROPERTIES, i++);        
        setSearchResultTypeSortIndex(SearchResultType.RTF, i++);
        
        setSearchResultTypeSortIndex(SearchResultType.INI, i++);        
        setSearchResultTypeSortIndex(SearchResultType.AIFF, i++);        
        setSearchResultTypeSortIndex(SearchResultType.AU, i++);        
        setSearchResultTypeSortIndex(SearchResultType.AVI, i++);        
        
        setSearchResultTypeSortIndex(SearchResultType.BMP, i++);        
        setSearchResultTypeSortIndex(SearchResultType.GIF, i++);        
        setSearchResultTypeSortIndex(SearchResultType.JPEG, i++);        
        setSearchResultTypeSortIndex(SearchResultType.PNG, i++);        
        
        setSearchResultTypeSortIndex(SearchResultType.CDA, i++);        
        setSearchResultTypeSortIndex(SearchResultType.M3U, i++);        
        setSearchResultTypeSortIndex(SearchResultType.MID, i++);        
        setSearchResultTypeSortIndex(SearchResultType.MIDI, i++);        
        setSearchResultTypeSortIndex(SearchResultType.MOV, i++);        
        setSearchResultTypeSortIndex(SearchResultType.MP1, i++);        
        setSearchResultTypeSortIndex(SearchResultType.MP2, i++);        
        setSearchResultTypeSortIndex(SearchResultType.MP3, i++);        
        setSearchResultTypeSortIndex(SearchResultType.MPEG, i++);        
        setSearchResultTypeSortIndex(SearchResultType.MPGA, i++);        
        setSearchResultTypeSortIndex(SearchResultType.PS, i++);        
        setSearchResultTypeSortIndex(SearchResultType.QT, i++);        
        setSearchResultTypeSortIndex(SearchResultType.RAM, i++);        
        setSearchResultTypeSortIndex(SearchResultType.RTF, i++);        
        setSearchResultTypeSortIndex(SearchResultType.WAV, i++);        
        setSearchResultTypeSortIndex(SearchResultType.WMA, i++);        
        setSearchResultTypeSortIndex(SearchResultType.WMV, i++);        
        setSearchResultTypeSortIndex(SearchResultType.OTHER, i++);        
        
    }

}
