package pl.zalewski.mikolaj.symbdev.ui;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;

import org.eclipse.core.runtime.Path;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.swt.graphics.Image;

import pl.zalewski.mikolaj.symbdev.SymbdevPlugin;

/**
 * @author mikolaj
 *
 * A class managing access to images contained in the plug-in
 */
public class SymbdevImages {

	private final static URL BASE_URL = SymbdevPlugin.getDefault().getBundle().getEntry("/"); //$NON-NLS-1$
	public final static String ICONS_PATH = "icons/"; //$NON-NLS-1$
	public final static String ICONS_LANG_PATH = "icons/lang/"; //$NON-NLS-1$
	
	public final static ImageDescriptor DESC_HELP=ImageDescriptor.createFromURL(makeImageURL(ICONS_PATH, "help.gif"));
	public final static ImageDescriptor DESC_DEFINE=ImageDescriptor.createFromURL(makeImageURL(ICONS_PATH, "define_obj.gif"));
	public final static ImageDescriptor LIBRARY=ImageDescriptor.createFromURL(makeImageURL(ICONS_PATH, "lib_obj.gif"));
	public final static ImageDescriptor DESC_INCLUDES_CONTAINER=ImageDescriptor.createFromURL(makeImageURL(ICONS_PATH, "includes_container.gif"));
	public final static ImageDescriptor DESC_PROP_EDIT=ImageDescriptor.createFromURL(makeImageURL(ICONS_PATH, "prop_edt.gif"));
	public final static ImageDescriptor DESC_HFOLDER_OBJ=ImageDescriptor.createFromURL(makeImageURL(ICONS_PATH, "hfolder_obj.gif"));
	public final static ImageDescriptor DESC_RESOURCE_ICON=ImageDescriptor.createFromURL(makeImageURL(ICONS_PATH, "resource_icon.gif"));
	public final static ImageDescriptor DESC_RESOURCE_GROUP=ImageDescriptor.createFromURL(makeImageURL(ICONS_PATH, "resource_group.gif"));
	public final static ImageDescriptor DESC_AIF_ICON=ImageDescriptor.createFromURL(makeImageURL(ICONS_PATH, "aif_icon.gif"));
	public final static ImageDescriptor DESC_MBM_ICON=ImageDescriptor.createFromURL(makeImageURL(ICONS_PATH, "mbm_icon.gif"));
    
    public static SymbdevImages getDefault()
    {
        if (fDefault==null)
            fDefault=new SymbdevImages();
        return fDefault;
    }

    /**
     * Loads the icons for the given language
     * @param code The id of the language
     * @return The icon or <code>null</code> if not found
     */
    public static Image getLanguageIcon(String code)
    {
        HashMap cache=getDefault().fLangCache;
        Object o=cache.get(code);
        if (o!=null)
            return (Image)o;
        URL url=SymbdevPlugin.getDefault().find(new Path(ICONS_LANG_PATH).append(code+".png"));
        if (url==null)
            url=SymbdevPlugin.getDefault().find(new Path(ICONS_LANG_PATH).append(code+".gif"));
        if (url==null)
            return null;
        ImageDescriptor des=ImageDescriptor.createFromURL(url);
        Image img=des.createImage(false);
        cache.put(code, img);
        return img;
    }
    
    public static URL makeImageURL(String prefix, String name)
    {
    	String path = prefix + name;
    	URL url = null;
    	try {
    		url = new URL(BASE_URL, path);
    	}
    	catch (MalformedURLException e) {
    		return null;
    	}
    	return url;        
    }
    
    protected SymbdevImages()
    {
        fLangCache=new HashMap();
    }

    protected static SymbdevImages fDefault;
    protected HashMap fLangCache;
}
