/*
 * Copyright (c) 2006-2008 MiniMe. Code released under The MIT/X Window System
 * License. Full license text can be found in license.txt
 */
package minime.ui;

import javax.microedition.lcdui.Graphics;

import minime.FontManager;
import minime.Image;
import minime.Portability;
import minime.core.CanSelect;
import minime.core.ResourceManager;

/**
 * The MemuItem class is used as menu item.
 * 
 * @author yishu
 * 
 */
public class MenuItem extends LabelPanel implements CanSelect
{
	private boolean selected ;
    private Image selectedImage;
    private Image refOnBgImage;     // just need to for the switch between selected and non-selected image    

    //
    // Factory methode to create MenuItem and MenuItem array
    //
    public static MenuItem[] createMenuItems(int[] txtId, int textColor)
    {
    	return createMenuItemsWithFrameAndBackgroundImage(txtId, textColor, 0, 0, (Image) null, (Image) null) ;
    }

    public static MenuItem[] createMenuItemsWithSelectionFrame(int[] txtId, int textColor, int selFrameThickness, int selFrameColor)
    {
    	return createMenuItemsWithFrameAndBackgroundImage(txtId, textColor, selFrameThickness, selFrameColor, (Image) null, (Image) null) ;
    }
    
    public static MenuItem[] createMenuItemsWithSelectionFrame(String[] strings, int textColor, int selFrameThickness, int selFrameColor)
    {
    	return createMenuItemsWithFrameAndBackgroundImage(strings, textColor, selFrameThickness, selFrameColor, (Image) null, (Image) null) ;
    }
    
    public static MenuItem[] createMenuItemsWithBackgroundImage(int[] txtId, int textColor, int bgImageId, int selectedImageId)
    {
    	Image bgImage = new Image(ResourceManager.getImage(bgImageId)) ;
    	Image selectedImage = new Image(ResourceManager.getImage(selectedImageId)) ;
    	return createMenuItemsWithFrameAndBackgroundImage(txtId, textColor, 0,0, bgImage, selectedImage) ;    	
    }

    public static MenuItem[] createMenuItemsWithBackgroundImage(String[] strings, int textColor, Image bgImage, Image selImage)
    {
    	return createMenuItemsWithFrameAndBackgroundImage(strings, textColor, 0, 0, bgImage, selImage) ;    	
    }

    public static MenuItem[] createMenuItemsWithFrameAndBackgroundImage(int[] texRscId, int textColor, int selFrameThickness, int selFrameColor, Image bgImage, Image selectedImage)
    {
    	MenuItem[] items = new MenuItem[texRscId.length] ; 
    	for(int i=0;i<items.length;i++)
    		items[i] = createSingleMenuItemWithFrameAndBackgroundImage(texRscId[i], textColor, selFrameThickness, selFrameColor, bgImage, selectedImage) ;
    	return items ;
    }

    public static MenuItem[] createMenuItemsWithFrameAndBackgroundImage(String[] strings, int textColor, int selFrameThickness, int selFrameColor, Image bgImage, Image selectedImage)
    {
    	MenuItem[] items = new MenuItem[strings.length] ; 
    	for(int i=0;i<items.length;i++)
    		items[i] = createSingleMenuItemWithFrameAndBackgroundImage(strings[i], textColor, selFrameThickness, selFrameColor, bgImage, selectedImage) ;
    	return items ;
    }

    public static MenuItem createSingleMenuItemWithFrameAndBackgroundImage(String s, int textColor, int selFrameThickness, int selFrameColor, int bgImageRscId, int selImageRscId)
    {
    	Image bgImage = null ;
    	Image selectedImage = null ;

    	if (bgImageRscId!=-1)
    		bgImage = new Image(ResourceManager.getImage(bgImageRscId)) ;
    	if (selImageRscId!=1)
    		selectedImage = new Image(ResourceManager.getImage(selImageRscId)) ;
    	
    	return createSingleMenuItemWithFrameAndBackgroundImage(s, textColor, selFrameThickness, selFrameColor, bgImage, selectedImage) ;
    }

    public static MenuItem createSingleMenuItemWithFrameAndBackgroundImage(int texRscId, int textColor, int selFrameThickness, int selFrameColor, int bgImageRscId, int selImageRscId)
    {
    	Image bgImage = null ;
    	Image selectedImage = null ;

    	if (bgImageRscId!=-1)
    		bgImage = new Image(ResourceManager.getImage(bgImageRscId)) ;
    	if (selImageRscId!=1)
    		selectedImage = new Image(ResourceManager.getImage(selImageRscId)) ;
    	
    	return createSingleMenuItemWithFrameAndBackgroundImage(texRscId, textColor, selFrameThickness, selFrameColor, bgImage, selectedImage) ;
    }

    public static MenuItem createSingleMenuItemWithFrameAndBackgroundImage(int texRscId, int textColor, int selFrameThickness, int selFrameColor, Image bgImage, Image selectedImage)
    {
    	Label l = new Label(texRscId,FontManager.getFont(FontManager.MENUITEM_FONT),textColor, selFrameThickness, selFrameColor) ;
    	MenuItem item = new MenuItem(l,bgImage,selectedImage) ;
    	return item ;
    }

    public static MenuItem createSingleMenuItemWithFrameAndBackgroundImage(String s, int textColor, int selFrameThickness, int selFrameColor, Image bgImage, Image selectedImage)
    {
    	Label l = new Label(s,FontManager.getFont(FontManager.MENUITEM_FONT),textColor, selFrameThickness, selFrameColor) ;
    	MenuItem item = new MenuItem(l,bgImage,selectedImage) ;
    	return item ;
    }
    
    protected MenuItem()
    {}
    
    /**
     * Constructs MenuItem instance by given label, it doesn't have background
     * image and selected image.
     * 
     * @param l
     *            the Label used to display menuItem's label
     */
    public MenuItem(Label l)
    {
        this(l, (Image) null, (Image) null);
    }


    /**
     * Create an instance of MenuItem with given Label instance, background
     * image and selected image resource index ids, images will accesses
     * ResourceManager to get the images data from resource file(.bin) based on
     * image resource ids.if want to set image to null, imamge resource id
     * should be ResourceManager.NONE_RESOURCE.
     * 
     * @param l
     *            the Label used to display menuItem's label
     * @param bgImRscId
     *            the background image resource id, if image is null, set
     *            bgImRscId to ResourceManager.NONE_RESOURCE.
     * @param selectedImRscId
     *            the selected image resource id, if image is null, set
     *            selectedImRscId to ResourceManager.NONE_RESOURCE.
     */
    public MenuItem(Label l, int bgImRscId, int selectedImRscId)
    {
    	this(l, new Image(ResourceManager.getImage(bgImRscId)),  new Image(ResourceManager.getImage(selectedImRscId))) ;
    }

    /**
     * Constructs a MenuItem instance by given label instance, background image
     * and selected image.
     * 
     * @param l
     *            the Label used to display menuItem's label
     * @param bgImage
     *            the image for MenuItem's background
     * @param selectedImage
     *            the image for MenuItem been selected.
     */
    public MenuItem(Label l, Image bgImage, Image selectedImage)
    {
        super(l, bgImage);
        this.refOnBgImage = bgImage;
        this.selectedImage = selectedImage;
    }


    /*
     * Overriden because need to keep ref on the default bgImage
     * @see minime.ui.LabelPanel#setBgImage(minime.Image)
     */
    public void setBgImage(Image im)
    {
    	refOnBgImage = im ;
    	super.setBgImage(im) ;
    }
    
    public void setSelectedImage(Image im)
    {
    	selectedImage = im ; 
    }

	public boolean isSelected()
	{
		return selected ;
	}

	public void renderImplSelected(Graphics gc)
	{
		renderImpl(gc) ;
	}

	/*
	 * Set selected.   
	 * 
	 * Set the frame on/off, set the selected/non selected image
	 * 
	 * @see minime.core.CanSelect#setSelected(boolean)
	 */
	public void setSelected(boolean select)
	{
		if (select!=selected)
		{
			if (select)
			{
				setFrame(true) ;
				setBgImage(selectedImage) ;
			}
			else
			{
				setFrame(false) ;
				setBgImage(refOnBgImage) ;
			}
			setNeedLayout() ;
		}
		
		selected = select ;
	}

//  /**
//  * Calculates the size and the position.
//  */
// public void layoutImp()
// {
//     super.layoutImp();
//     if (selectedImage != null)
//     {
//         setHeight(Math.max(super.getHeight(), selectedImage.getHeight()));
//         if (selectedImage.getWidth() > super.getWidth() || refOnBgImage == null)
//             setWidth(selectedImage.getWidth());
//     }
// }
//
// /**
//  * Renders MenuItem.If MenuItem is selected, render the selected image,
//  * otherwise render the background image.
//  */
// public void renderImpl(Graphics gc)
// {
//     if (isSelected())
//     {
//         setFrame(true);
//         super.setBgImage(selectedImage);
//     }
//     else
//     {
//         setFrame(false);
//         super.setBgImage(refOnBgImage);
//     }
//     super.renderImpl(gc);
// }

}
