/**
 * Created on Jun 18, 2005
 * $id:  $
 */
package org.jvnet.lookout.ui.core.tree.label;

import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.swt.graphics.Image;
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.ui.core.Plugin;
import org.jvnet.lookout.ui.core.preference.Preferences;
import org.jvnet.lookout.ui.core.tree.SearchResultDetail;
import org.jvnet.lookout.ui.core.tree.SearchResultItem;
import org.jvnet.lookout.ui.core.util.Utilities;

abstract public class AbstractTreeLabelProvider extends LabelProvider implements SearchResultsLabelProvider {
    
    protected static Image snippetImage;
    protected static Image powerpointImage;
    protected static Image wordImage;
    protected static Image excelImage;
    protected static Image pdfImage;
    protected static Image htmlImage;
    protected static Image rtfImage;
    protected static Image postscriptImage;
    protected static Image emailImage;
    protected static Image defaultResultImage;
    protected static Image urlDetailImage;
    protected static Image javaImage;
    protected static Image textImage;
    protected static Image iniImage;
    protected static Image imageImage;
    protected static Image audioImage;
    protected static Image videoImage;
    protected static Image javaClassImage;
    
    static {
    	Plugin plugin = Plugin.getDefault();
    	
    	snippetImage = Utilities.getImage(
    			Plugin.getResourceString("snippet-detail-icon"), plugin);
    	powerpointImage = Utilities.getImage(
    			Plugin.getResourceString("powerpoint-detail-icon"), plugin);
    	wordImage = Utilities.getImage(
    			Plugin.getResourceString("word-detail-icon"), plugin);
    	excelImage = Utilities.getImage(
    			Plugin.getResourceString("excel-detail-icon"), plugin);
    	pdfImage = Utilities.getImage(
    			Plugin.getResourceString("pdf-detail-icon"), plugin);
    	htmlImage = Utilities.getImage(
    			Plugin.getResourceString("html-detail-icon"), plugin);
    	rtfImage = Utilities.getImage(
    			Plugin.getResourceString("rtf-detail-icon"), plugin);
    	postscriptImage = Utilities.getImage(
    			Plugin.getResourceString("postscript-detail-icon"), plugin);
    	emailImage = Utilities.getImage(
    			Plugin.getResourceString("email-detail-icon"), plugin);
    	urlDetailImage = Utilities.getImage(
    			Plugin.getResourceString("url-detail-icon"), plugin);    
    	javaImage = Utilities.getImage(
    			Plugin.getResourceString("java-detail-icon"), plugin);     	
    	textImage = Utilities.getImage(
    			Plugin.getResourceString("text-detail-icon"), plugin);     	
    	iniImage = Utilities.getImage(
    			Plugin.getResourceString("ini-detail-icon"), plugin);     	
    	defaultResultImage = Utilities.getImage(
    			Plugin.getResourceString("other-detail-icon"), plugin);
    	imageImage = Utilities.getImage(
    			Plugin.getResourceString("image-detail-icon"), plugin);     	
    	audioImage = Utilities.getImage(
    			Plugin.getResourceString("audio-detail-icon"), plugin);     	
    	videoImage = Utilities.getImage(
    			Plugin.getResourceString("video-detail-icon"), plugin);     	
    	javaClassImage = Utilities.getImage(
    			Plugin.getResourceString("java-class-detail-icon"), plugin);     	
    	
    }
    
    public AbstractTreeLabelProvider() {         
    }

    public Image getImage(Object obj) {
        
        Image image = lookupFromExtensions(obj);
        
        //No image found, so we use the defaults...
        if(image == null) {
            
            if (obj instanceof SearchResultItem) {
                
            	image = getImage((SearchResultItem) obj);
                
            } else if(obj instanceof SearchResultDetail) {
                
            	image = getImage((SearchResultDetail) obj);                
            }
        }        
        return image;              
    }

    protected Image lookupFromExtensions(Object obj) {

        Image image = null;
        
        SearchResultItem item = null;
        
        if (obj instanceof SearchResultDetail) {
            item = (SearchResultItem)((SearchResultDetail)obj).getParent();
        } else if (obj instanceof SearchResultItem) {
            item = (SearchResultItem)obj;
        } 
        
        //Get the source...
        SearchResultSource itemSource = (SearchResultSource)item.getPropertyValue(SearchResultProperty.SOURCE);
        
        //Try to find the image for it from a possible extension... 
        SearchResultsLabelProvider labelProvider = Plugin.getDefault().getLabelProviderForSource(itemSource);
        
        if(labelProvider != null) {
            
            if (obj instanceof SearchResultDetail) {
                image = labelProvider.getImage((SearchResultDetail)obj);
            } else if (obj instanceof SearchResultItem) {
                image = labelProvider.getImage((SearchResultItem)obj);
            }
        }
        return image;
    }   
    
    protected Image getTypeBasedImage(SearchResultType itemType) {
    	
    	Image image = null;
    	
    	if(SearchResultType.HTML.equals(itemType)) {
    		image = htmlImage;
    	} else if (SearchResultType.PDF.equals(itemType)) {
    		image = pdfImage;
    	}else if (SearchResultType.DOC.equals(itemType)) {
    		image = wordImage;
    	}else if (SearchResultType.RTF.equals(itemType)) {
    		image = rtfImage;
    	}else if (SearchResultType.PS.equals(itemType)) {
    		image = postscriptImage;
    	}else if (SearchResultType.EMAIL.equals(itemType)) {
    		image = emailImage;
    	}else if (SearchResultType.PPT.equals(itemType)) {
    		image = powerpointImage;
    	}else if (SearchResultType.XLS.equals(itemType)) {
    		image = excelImage;
    	} else if (SearchResultType.JAVA.equals(itemType)) {
    		image = javaImage;
    	} else if (SearchResultType.TXT.equals(itemType)) {
    		image = textImage;
    	} else if (SearchResultType.INI.equals(itemType)) {
    		image = iniImage;
    	} else if (
    			SearchResultType.GIF.equals(itemType) ||
    			SearchResultType.JPEG.equals(itemType) ||
    			SearchResultType.PNG.equals(itemType) ||
    			SearchResultType.BMP.equals(itemType)) {
    		image = imageImage;
    	} else if (
    			SearchResultType.MP1.equals(itemType) ||
    			SearchResultType.MP2.equals(itemType) ||
    			SearchResultType.MP3.equals(itemType) ||
    			SearchResultType.M3U.equals(itemType) ||
    			SearchResultType.AVI.equals(itemType) ||
    			SearchResultType.WMA.equals(itemType) ||
    			SearchResultType.WAV.equals(itemType) ||
    			SearchResultType.RAM.equals(itemType) ||
    			SearchResultType.MIDI.equals(itemType) ||
    			SearchResultType.AU.equals(itemType) ||
    			SearchResultType.AIFF.equals(itemType) ||
    			SearchResultType.CDA.equals(itemType) ||
    			SearchResultType.MID.equals(itemType) ||
    			SearchResultType.RMI.equals(itemType)
    			) {
    		image = audioImage;
    	} else if (
    			SearchResultType.WMA.equals(itemType) ||
    			SearchResultType.MPEG.equals(itemType) ||
    			SearchResultType.MOV.equals(itemType) ||
    			SearchResultType.MPGA.equals(itemType) ||
    			SearchResultType.QT.equals(itemType)) {
    		image = videoImage;
    	} else if (SearchResultType.JAVA_CLASS.equals(itemType)) {
    		image = javaClassImage;
    	}
    	
    	//CXB_TODO need icons
    	else if (SearchResultType.XML.equals(itemType)) {
    		image = defaultResultImage;
    	}else if (SearchResultType.XSD.equals(itemType)) {
    		image = defaultResultImage;
    	}else if (SearchResultType.OTHER.equals(itemType)) {
    		image = defaultResultImage;
    	} else {
    		image = defaultResultImage;
    	}         
    	
    	return image;
    }
    
    protected String getGroupingMode() {
    	return Plugin.getDefault().getPreferenceStore().getString(Preferences.GroupingMode);
    }
    
 }
