package cnp.ew.image;

import java.awt.*;
import java.awt.image.*;
import java.util.*;

/**
 * Provides an application-wide manager for images that retrieves
 * them in groups transparently.  To use it:
 * - access the default imagegetter, by sending the static message
 *   getDefault(). Then send this the messages below.
 * - register an image, using either registerImageUrl() or
 *   registerImageName().  This will answer an id that should be stored
 *   to access the image.
 * - access the image, with getImage(), passing in the registered id.
 *
 * When an image is accessed, and has not yet been retrieved, all images
 * that are currently registered that haven't yet been retrieved will
 * be in parallel.  The method will block until all images have been
 * retrieved.
 */
public class CpImageGetter
{
    static CpImageGetter defaultImageGetter;

    public static CpImageGetter getDefault()
    {
        if (defaultImageGetter == null) {
            defaultImageGetter = new CpImageGetter();
        }
        return defaultImageGetter;
    }


    Vector registeredUrls = new Vector();
    // TBD: use a mechanism like properties to store the initial imagePath.
    String imagePath="C:\\src\\cnp\\ew\\images\\";

    Hashtable images = new Hashtable();
    Hashtable grayedImages = new Hashtable();
    Hashtable lightGrayedImages = new Hashtable();

    /**
     * Register an url string, and answer an id to use
     * when retrieving the image with this url.
     */
    public int registerImageUrl(String urlString)
    {
        registeredUrls.addElement(urlString);
        return registeredUrls.size() - 1;
    }

    public void setImagePath(String newImagePath)
    {
        imagePath = newImagePath;
    }

    public String getImagePath()
    {
        return imagePath;
    }

    /**
     * Register an image name, and answer an id to use
     * when retrieving the image with this url.
     * This differs from registerImageUrl in that the current
     * imagePath will be prepended to the name to find the image.
     */
    public int registerImageName(String imageName)
    {
        return registerImageUrl(getImagePath() + imageName);
    }

    /**
     * Retrieve all images that have been registered with this
     * image getter which have not yet been retrieved.  This method will
     * block until all images have been retrieved or an error occurs.
     */
    public void retrieveAllRegisteredImages(Component c)
    throws InterruptedException
    {
        String urlString;
        Image image;
        MediaTracker tracker = new MediaTracker(c);
"http://internal.dstc.edu.au/TU/phelps/index.html";
	    }

	    URL url = new URL(getDocumentBase(), name);

        for (int i = 0; i < registeredUrls.size(); i++) {
            urlString = (String)registeredUrls.elementAt(i);
            if (images.get(urlString) == null) {
                image = Toolkit.getDefaultToolkit().getImage(urlString);
                images.put(urlString, image);
                tracker.addImage(image, i);
            }
        }
        tracker.waitForAll();
    }

    public Image getImage(Component c, int id)
    {
        return getImage(c, (String)registeredUrls.elementAt(id));
    }

    public Image getImage(Component c, String urlString)
    {
        Image image = (Image)images.get(urlString);

        if (image == null) {
            try {
                retrieveAllRegisteredImages(c);
            }
            catch(InterruptedException e) {
                // TBD: this should probably not be caught...
            }
        }
        image = (Image)images.get(urlString);
        return image;
    }

    public Image getGrayedImage(Component c, Image image)
    {
        Image grayedImage = (Image)grayedImages.get(image);

        if (grayedImage == null) {
            ImageFilter f = new CpGrayFilter();
            ImageProducer producer = new FilteredImageSource(image.getSource(), f);
            grayedImage = c.createImage(producer);
            MediaTracker tracker = new MediaTracker(c);
            tracker.addImage(grayedImage, 1);
            try {
                tracker.waitForID(1);
            }
            catch(InterruptedException e) {
            }
            grayedImages.put(image, grayedImage);
        }

        return grayedImage;
    }

    /**
     * Answers an image that is a copy of image, with all gray
     * color converted to light gray.
     */
    public Image getLightGrayedImage(Component c, Image image)
    {
        Image grayedImage = (Image)lightGrayedImages.get(image);

        if (grayedImage == null) {
            ImageFilter f = new CpGrayToLightGrayFilter();
            ImageProducer producer = new FilteredImageSource(image.getSource(), f);
            grayedImage = c.createImage(producer);
            MediaTracker tracker = new MediaTracker(c);
            tracker.addImage(grayedImage, 1);
            try {
                tracker.waitForID(1);
            }
            catch(InterruptedException e) {
            }
            lightGrayedImages.put(image, grayedImage);
        }

        return grayedImage;
    }

}



