/*
 * 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.Portability;
import minime.core.Event;
import minime.core.EventDispatcher;
import minime.core.PopupComponent;
import minime.core.ResourceManager;
import minime.core.Runtime;
import minime.gen.TextRscId;

public class PopupAlertComponent extends PopupComponent
{	
	public static final int YES_CHOICE = 1 ; 
	public static final int NO_CHOICE = 2 ; 
	public static final int CANCEL_CHOICE = 3 ; 
	
	public static PopupAlertComponent createAlertBox(String text, int yesKeyId)
	{
		return new PopupAlertComponent(text,ResourceManager.NONE_RESOURCE, yesKeyId, ResourceManager.NONE_RESOURCE) ;		
	}
	
	public static PopupAlertComponent createChoiceBox(String text, int yesKeyId, int noKeyId)
	{
		return new PopupAlertComponent(text,ResourceManager.NONE_RESOURCE, yesKeyId, noKeyId) ;		
	}

	public static PopupAlertComponent createOptionBox(String text, int yesKeyId, int noKeyId, int cancelKeyId)
	{
		return new PopupAlertComponent(text,cancelKeyId, yesKeyId, noKeyId) ;		
	}

	int cancelKeyId ;
	int yesKeyId ;
	int noKeyId ;
	Label label ;
	
	private PopupAlertComponent(String text, int cancelKeyId, int yesKeyId, int noKeyId)
	{
		setText(text);
		this.cancelKeyId = cancelKeyId ;
		this.yesKeyId = yesKeyId ;
		this.noKeyId = noKeyId ;
		setBgColor(Portability.defaultPopUpMessageColor);
	}

	public void setText(String text)
	{
		label = new Label(text,FontManager.getFont(FontManager.TEXTPANEL_FONT),Portability.RED) ;
		setNeedLayout() ;
	}
	
	/*
	 * Perform the layout if the alert box.   Will be called from PopupComponent.show()
	 * 
	 * @see minime.Drawable#layoutImp()
	 * @see minime.core.PopupComponent#show()
	 */
	protected void layoutImp()
	{
		// set the center softkey
		SoftBar.setCenterSoftkey(yesKeyId) ;
		SoftBar.setLeftSoftkey(cancelKeyId) ;
		SoftBar.setRightSoftkey(noKeyId) ;
		
		
		// set the Alert in the Middle of the screen (taking into account softbar hight) 
		int w = (view.getWidth() / 3)*2 ;
		int h = ((view.getHeight()-SoftBar.getInstance().getHeight()) / 5)*2 ;
		int l = (view.getWidth()-w)/2 ;
		int t = (view.getHeight()-h)/2 ;
		
		setWidth(w) ;
		setHeight(h) ;
		setPosition(l, t) ;
		
		// set and center the label
		label.setWidth(w) ;
		label.layout() ;
		t = (h - label.getHeight())/2 ;
		label.setPosition(0, t) ;

		// set Bg Color same as view
//		setBgColor(view.getBgColor()) ;
	}

	public void renderImpl(Graphics gc)
	{
		int c = gc.getColor() ;
		gc.setColor(Portability.RED) ;
		gc.drawRect(0, 0, getWidth(), getHeight()) ;
		gc.setColor(c) ;

		//draw the label
//		gc.translate(label.getLeft(), label.getTop()) ;
		label.render(gc) ;
//		gc.translate(-label.getLeft(), -label.getTop()) ;
	}

	public boolean onEvent(Event evt)
	{
		int choice=-1 ;
		if (evt.type==Event.SB_EVENT)
		{
			if (evt.paramA== Portability.KEY_SOFT_LEFT && evt.paramB==cancelKeyId)
			{
				choice = CANCEL_CHOICE ;
			}
			else if (evt.paramA== Portability.KEY_SOFT_RIGHT && evt.paramB==noKeyId)
			{
				choice = NO_CHOICE ;
			}
			else if (evt.paramA== Portability.KEY_FIRE && evt.paramB==yesKeyId)
			{
				choice = YES_CHOICE ;
			}
		}
		
		if (choice>-1)
		{
			// fire the popup event and dispose (close) the popup
			EventDispatcher.getInstance().fire(Event.createEvent(Event.POPUP_EVENT, choice, 0)) ;
			dispose() ;
			Runtime.getInstance().repaint();
			return true ;
		}
		
		return false ;
	}
}
