/**
 * This package include the UI components such as Button, EditField, Label etc.
 */
/*
 * 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.Rectangle;
import minime.core.Event;
import minime.core.EventDispatcher;
import minime.core.ResourceManager;

/**
 * The view may have title label, SoftBar and displayable area.it can hold other
 * drawables, can listen event and handle it.The contents displayed and their
 * interaction with the user are defined by subclasses.
 * 
 * @author yishu
 * 
 */
public class BaseView extends View
{
    /** The SoftBar for the view */
    protected SoftBar softbar;
    // private Font font;
    protected Label labelTitle;
    private Image bgImage;
    private int bgImgX = 0;
    private int bgImgY = 0;
    private ImagePanel bgImagePanel;

    /**
     * Creates a new BaseView instance without title and softbar.
     * 
     */
    public BaseView()
    {
        this(null, false);
    }

    /**
     * Creates a new BaseView instance with given title, if useSoftBar is true,
     * the view will have softbar, otherwise it doesn't have.
     * 
     * @param title
     *            the label instance for view title
     * @param useSoftBar
     *            whether has softbar or not
     */
    public BaseView(Label title, boolean useSoftBar)
    {
        if (useSoftBar)
            softbar = SoftBar.getInstance() ;
        labelTitle = title;
        setBgColor(Portability.defaultBaseViewBgColor) ;
    }


    /**
     * Creates a new BaseView instance with given title and background image, if
     * useSoftBar is true, the view will have softbar, otherwise it doesn't
     * have.The background image data obtained from the resource file based on
     * imRscId. The name parameter is a resource id as defined by
     * ResourceManager.getImage(imRscId).
     * 
     * @param title
     *            the label instance for view title
     * @param useSoftBar
     *            whether has softbar or not
     * @param imRscId
     *            the image resource id.
     */
    public BaseView(Label title, boolean useSoftBar, int imRscId)
    {
        this(title, useSoftBar);
        setBgImage(imRscId);
    }

    /**
     * Attaches title and softbar for drawing and event listening.
     */
    public void activate()
    {
        // attach component for drawing and event listening
    	// add image panel.
    	if (bgImage != null)
    	{
    		if(bgImagePanel == null)
    			bgImagePanel = new ImagePanel(bgImage);
    		addToView(bgImagePanel);
    	}
        if (softbar != null)
        {
        	// softbar is at the top position to receive event first
            addToView(softbar); 
            softbar.setFocus(true);
        }
        if (labelTitle != null)
            addToView(labelTitle);

        // attach itself for event listing
        EventDispatcher.getInstance().attach(this);
    }

    /**
     * Deattaches title and softbar from view and event listening.
     */
    public void deactivate()
    {
    	if (bgImagePanel != null)
    	{
    		removeFromView(bgImagePanel);
    	}
        if (softbar != null)
        {
            removeFromView(softbar);
        }
        if (labelTitle != null)
            removeFromView(labelTitle);
        EventDispatcher.getInstance().detach(this);
    }

    /**
     * Calculates the view size and updates components' position
     */
    public Rectangle layout()
    {
        // always layout base class first
        Rectangle da = super.layout();
        int x, y ;
        
        //update the bgImagePanel position
        if( bgImagePanel != null )
        {
            // layout the bgImagePanel, then center horizontaly 
        	bgImagePanel.layout() ;
        	x = (getWidth() - bgImagePanel.getWidth())/2 ;
        	y = (getHeight() - bgImagePanel.getHeight())/2 ; 
        	bgImagePanel.setPosition(x, y) ;
        }
        
        // update softbar position
        if (softbar != null)
        {
        	// set the softbar as wide as the view, then layout
        	softbar.setWidth(getWidth()) ;
        	softbar.layout() ;
        	
        	// calculate softbar position
            x = 0;
            y = getHeight() - softbar.getHeight();
            softbar.setPosition(x, y);
            
            // update view displayable area high
            da.h -= softbar.getHeight();
        }
        // update title position
        if (labelTitle != null)
        {
            // layout the title, then center horizontaly 
        	labelTitle.layout() ;
        	x = (getWidth() - labelTitle.getWidth())/2 ;
        	y = 0 ; 
            labelTitle.setPosition(x, y) ;
            
            // update view displayable area high
            da.h -= labelTitle.getHeight();
            da.y += labelTitle.getHeight();
        }

        return da;
    }

	 /**
	  * Sets softbar three buttons' label
	  * 
	  * @param leftText, the softbar left button's label
	  * @param middleText, the softbar middle button's label
	  * @param rightText, the softbar right button's label
	  */
    public void setSoftbar(int leftKey, int centerKey, int rightKey)
    {
    	if (softbar==null)
    		throw new IllegalStateException("Softbar not defined") ;
    	
    	softbar.setSoftkeyLabel(SoftBar.LEFT, leftKey) ;
    	softbar.setSoftkeyLabel(SoftBar.MIDDLE, centerKey) ;
    	softbar.setSoftkeyLabel(SoftBar.RIGHT, rightKey) ;
    }
    /**
     * Sets view background image with given image resource index id, use id to
     * get image from resource file.If want to set backgroumd image null, id
     * should be ResourceManager.NONE_RESOURCE.
     * 
     * @param imRscId
     *            the background image resource index id, If want to set
     *            backgroumd image null, id should be
     *            ResourceManager.NONE_RESOURCE.
     */
    public void setBgImage(int imRscId)
    {
        if (imRscId >= 0)
        {
            bgImage = new Image(imRscId);
            if(bgImagePanel != null)
            {
            	bgImagePanel = new ImagePanel(bgImage);
            }
            else
            {
            	bgImagePanel = new ImagePanel(bgImage);
            	viewComposite.insertDrawable(bgImagePanel, 0);
            }
            setBgColor(Portability.TRANSPARENT);
        }
        else
        {
        	bgImage = null ;
        	if(viewComposite.drawablesInitialized())
        	{
        		if(viewComposite.drawableAt(0) != null &&
        				viewComposite.drawableAt(0).equals(bgImagePanel))
        		{
        			viewComposite.removeDrawable(0);
        			bgImagePanel = null;
        		}
        	}
        }
    }

    /**
     * Sets background image position.
     * 
     * @param x
     *            the x coordinate of the background image to be drawn
     * @param y
     *            the y coordinate of the background image to be drawn
     */
    public void setBgImagePos(int x, int y)
    {
        bgImgX = x;
        bgImgY = y;
    }

    public boolean onEvent(Event evt)
    {
        return false;
    }

    public int getPriority()
    {
        return MEDIUM_PRIORITY;
    }

	public Label getViewTitle()
	{
		return labelTitle;
	}

	public void setViewTitle(Label labelTitle)
	{
		this.labelTitle = labelTitle;
	}
	
	public void setViewTitle(int txtRscId)
	{
		Label label = new Label(txtRscId, Portability.defaultFontTextColor) ;
		this.labelTitle = label ;
	}
}
