/*
 *  PHEX - The pure-java Gnutella-servent.
 *  Copyright (C) 2001 - 2006 Phex Development Group
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
package phex.gui.common;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.net.URL;
import java.util.Hashtable;
import java.util.PropertyResourceBundle;
import java.util.ResourceBundle;

import javax.swing.Icon;
import javax.swing.ImageIcon;

import phex.utils.NLogger;
import phex.utils.NLoggerNames;

/**
 * This class defines an icon pack. It could be a used installed pack or a system
 * internal pack.
 */
public class IconPack
{
    private static final ImageIcon MISSING_IMAGE =
        new ImageIcon( IconPack.class.getResource("/phex/gui/resources/image-failed.gif") );
    public static final ImageIcon EMPTY_IMAGE_16;
    
    static
    {
        BufferedImage bufImg = new BufferedImage( 16, 16, BufferedImage.TYPE_INT_ARGB );
        /*int[] rgbArray = new int[16*16];
        Arrays.fill( rgbArray, 0x00ff00 );
        bufImg.setRGB( 0, 0, 16, 16, rgbArray, 0, 16 );*/
        EMPTY_IMAGE_16 = new ImageIcon( bufImg );
    }

    /**
     * The resource bundle of the icon pack.
     */
    private ResourceBundle resource;
    
    /**
     * This table makes sure that each image file is only loaded once and is 
     * afterwards cached. They can be fetched with its associated resource path.
     */
    private Hashtable imageCache;

    public IconPack( String resourceFileName )
    {
        this();
        resource = PropertyResourceBundle.getBundle( resourceFileName );
    }
    
    protected IconPack()
    {
        imageCache = new Hashtable();
    }

    /**
     * the method to fetch an icon using the keys defined
     * in Images.properties file.
     */
    public Icon getIcon( String key )
    {
        return new TKIcon( key );
    }

   /**
    * The class used to define an icon. It wrapps the functionality
    * of only loading a image when required.
    */
    class TKIcon extends ImageIcon
    {
        private String key;
        private boolean loaded;

        TKIcon( String aKey )
        {
            super();
            key = aKey;
            loaded = false;
        }

        /**
         * returns the image (overloaded from ImageIcon)
         */
        public Image getImage()
        {
            loadIcon();
            return super.getImage();
        }

        /**
         * paints the icon (overloaded from ImageIcon)
         * if its not already loaded then load it
         */
        public void paintIcon( Component c, Graphics g, int x, int y )
        {
            loadIcon();
            super.paintIcon( c, g, x, y );
        }

        /**
         * returns the width of an icon depending on it's offset
         * (overloaded from ImageIcon)
         */
        public int getIconWidth()
        {
            loadIcon();
            return super.getIconWidth();
        }

        /**
         * returns the width of an icon depending on it's offset
         * (overloaded from ImageIcon)
         */
        public int getIconHeight()
        {
            loadIcon();
            return super.getIconHeight();
        }

        /**
         * loads the icon with its path
         */
        private void loadIcon()
        {
            // watch out that icon is not loaded twice!
            if ( loaded )
            {
                return;
            }

            try
            {
                if ( key != null )
                {
                    String imgURLStr;
                    imgURLStr = resource.getString( key );
                    Image image = (Image)imageCache.get( imgURLStr );
                    if ( image == null )
                    {
                        // try to load image.
                        if ( imgURLStr.equals( "EMPTY_IMAGE_16" ) )
                        {
                            image = new BufferedImage( 16, 16, BufferedImage.TYPE_INT_ARGB );
                        }
                        else
                        {
                            URL imgURL = null;
                            if ( imgURLStr != null )
                            {
                                imgURL = IconPack.class.getResource( imgURLStr );
                            }
                            if ( imgURL != null )
                            {
                                image = Toolkit.getDefaultToolkit().createImage( imgURL );
                            }
                            
                            
                        }
                        if ( image != null )
                        {
                            imageCache.put( imgURLStr, image );
                        }
                        else
                        {
                            NLogger.warn( IconPack.class,
                                "Can't find image for key: " + key + " - URL: " + imgURLStr );
                            image = MISSING_IMAGE.getImage();
                        }
                    }
                    
                    super.setImage( image );
                    loaded = true;
                }
            }
            catch ( Exception exp )
            {
                NLogger.error( NLoggerNames.USER_INTERFACE, exp, exp );
                // TODO integrate gui utilis
                //GUIUtilities.handleException( exp );
            }
        }
    }
}