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

import javax.microedition.lcdui.Graphics;

import minime.Drawable;
import minime.Font;
import minime.Portability;
import minime.FontManager;
import minime.TextPanelUtil;
import minime.core.Component;
import minime.core.Event;
import minime.core.ResourceManager;

/**
 * The TextPanel class is used to hold and display multi-line text.
 * 
 * @author yishu
 * 
 */
public class TextPanel extends Component
{
	private Font font;
	private int textColor;
    private int nbrDisplayableRows=0;
    private int firstDisplayableRows=0;
    private String text=null;
    private String textArr[]=null;
    private boolean scrolling=false;
 
    Drawable background ;
    
    /**
     * create a text panel for displaying text
     * 
     * @param text
     *            the text for displaying
     * @param textColor
     *            the color for text
     */
    public TextPanel(String text, int textColor)
    {
        this(text, FontManager.getFont(FontManager.TEXTPANEL_FONT), textColor);
    }

    /**
     * create a text panel for displaying text, which get from resource file
     * based on txRscId.If want to set text to null, set txRscId to
     * ResourceManager.NONE_RESOURCE.
     * 
     * @param txRscId
     *            the text resource id, If want to set text to null, set txRscId
     *            to ResourceManager.NONE_RESOURCE.
     * @param textColor
     *            the color for text
     */
    public TextPanel(int txRscId, int textColor)
    {
        this(txRscId, FontManager.getFont(FontManager.TEXTPANEL_FONT), textColor);
    }

    /**
     * create a text panel for displaying text with font, the text get from
     * resource file based on txRscId, If want to set text to null, set txRscId
     * to ResourceManager.NONE_RESOURCE.
     * 
     * @param txRscId
     *            the text resource id,If want to set text to null, set txRscId
     *            to ResourceManager.NONE_RESOURCE.
     * @param font
     *            the specific font
     * @param textColor
     *            the color for text
     */
    public TextPanel(int txRscId, Font font, int textColor)
    {
        this(ResourceManager.getString(txRscId), font, textColor);
    }

    /**
     * create a text panel for displaying text with font
     * 
     * @param text
     *            the text for displaying
     * @param font
     *            the specific font
     * @param textColor
     *            the color for text
     */
    public TextPanel(String text, Font font, int textColor)
    {
        this.textColor = textColor;
        this.font = font;
        setText(text);	
        firstDisplayableRows=0;
    }

    public void layoutImp()
    {
        if (text == null)
            return;
        calcLayoutParameter(); 

    }
    private void calcLayoutParameter()
    {
        textArr = TextPanelUtil.slitStrIntoArrayStr(font, text, getWidth());
        nbrDisplayableRows = TextPanelUtil.calRowCount(font, getHeight());
        // sets showMore true if msgStr can't be displayed outright on a screen
        if (nbrDisplayableRows < textArr.length)
        {
            scrolling = true;
        }
        else
        {
            scrolling = false;
            nbrDisplayableRows = textArr.length;
        }
    }
    
    /**
     * Sets the text for TextPanel.
     * 
     * @param text
     *            the string to be set
     */
    public void setText(String text)
    {
        this.text = text;
        setNeedLayout() ;
    }

    /**
     * Sets the text which is gotten from text resource file based on text
     * resource id.If want to set text to null, set txRscId to
     * ResourceManager.NONE_RESOURCE.
     * 
     * @param txRscId
     *            the text resource id.If want to set text to null, set txRscId
     *            to ResourceManager.NONE_RESOURCE.
     */
    public void setText(int txRscId)
    {
        setText(ResourceManager.getString(txRscId));
    }

    /**
     * Adds the string to TextPanel.
     * 
     * @param content
     *            the String te be added.
     */
    public void append(String content)
    {
        if (content != null)
        {
            if (text != null)
                text += content;
            else
                text = content;
            setText(text);
        }
    }

    /**
     * Append the text to the text panel, and this text is gotten from text
     * resource file based on text resource id.If want to append nothing, set
     * txRscId to ResourceManager.NONE_RESOURCE.
     * 
     * @param txRscId
     *            the text resource id.If want to append nothing, set txRscId to
     *            ResourceManager.NONE_RESOURCE.
     */
    public void append(int txRscId)
    {
        append(ResourceManager.getString(txRscId));
    }

    /**
     * Sets the application's preferred font for rendering text.
     * 
     * @param font
     *            the preferred font to use to render text.
     */
    public void setFont(Font font)
    {
        this.font = font;
    }

	public int getTextColor() {
		return textColor;
	}

	public void setTextColor(int textColor) {
		this.textColor = textColor;
	}
	
    /*
     * (non-Javadoc)
     * 
     * @see minime.Drawable#renderImpl(javax.microedition.lcdui.Graphics)
     */
    public void renderImpl(Graphics gc)
    {
    	// draw the background if necessary
    	if (background!=null)
    		background.render(gc) ;
    	
        gc.setColor(textColor);
        // display all displayed string
        int tmpY = 0;
        for (int i = 0; i < nbrDisplayableRows; i++)
        {
            font.drawString(gc, 0, tmpY, textArr[firstDisplayableRows + i]);
            tmpY += font.getHeight();
        }
    }
    
    public void scrollToEnd()
    {
    	calcLayoutParameter(); // we need re-produce the textArr and nbrDisplayableRows. otherwise we need layout first.
    	firstDisplayableRows = textArr.length - nbrDisplayableRows; 
    }
    public boolean scrollDown()
    {
        if (firstDisplayableRows < (textArr.length - nbrDisplayableRows ))
        {
            firstDisplayableRows++;
            return true;
        }
        return false;
    }
    public boolean scrollUp()
    {
        if (firstDisplayableRows > 0)
        {
            firstDisplayableRows--;
            return true;
        }
        return false;
    }
 
    /*
     * (non-Javadoc)
     * 
     * @see minime.EventListener#onEvent(minime.Event)
     */
    public boolean onEvent(Event evt)
    {
        if (evt.type != Event.KEY_EVENT || !scrolling)
        {
            return false;
        }
        boolean handled = false;
        if (evt.paramA == Portability.KEY_UP)
        { // go up
            if( scrollUp() )
            {
            	handled = true;
            }
        }
        else if (evt.paramA == Portability.KEY_DOWN)
        { // go down
            if ( scrollDown() )
            {
                handled = true;
            }
        }

        if (handled)
        {
            minime.core.Runtime.getInstance().repaint();
            return true;
        }
        return false;
    }

	public void setBackground(Drawable background) {
		this.background = background;
	}
}
