/*
 * 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;
/**  This drawable component is used for loading animation
 * 	 @author chenhongbo
 */

import javax.microedition.lcdui.Graphics;

import minime.FontManager;
import minime.Portability;
import minime.Util;
import minime.core.Component;
import minime.core.Event;
import minime.core.EventDispatcher;
import minime.core.ResourceManager;

public class ImageAnimation  extends Component{
   
	private final int ANIMATIONY_Y_OFFSET_FROM_LOAD_LABEL=25; 
	
    //store the image resource ids
	private  int[] loadAnimationImageId; 
	
    //loading label
	private Label loadLabel;
	//images of animation
	private ImagePanel loadAnimation; 
	
	private int animationTurnTime;
	
	//record times of changing images 
	private int timesOfAnimationTurn=0;
	
	//the switch for start or stop the animation
 	private boolean  isStopped=false;
 	
 	
	public ImageAnimation( int[] imageArray,int animationTurnTime){
          this(ResourceManager.NONE_RESOURCE,Portability.C_BLACK,imageArray,animationTurnTime);
		
	}
 	
	/**  @param:
	 *  txtRsd:the tip text for the animation 
	 *  textColor: the tip text color
	 *  imageArray -> the images for switching
	 *  animationTurnTime -> the interval time between images switching
	 */
	public ImageAnimation(int txtRsd,int textColor,int[] imageArray,int animationTurnTime){
		 loadLabel=new Label(txtRsd,FontManager.getFont(FontManager.LABEL_FONT),textColor);
		 this.loadAnimationImageId=imageArray;
		 this.animationTurnTime=animationTurnTime;
		 loadAnimation=new ImagePanel(loadAnimationImageId[0]);
		 
	     setHeight(loadLabel.getHeight()+ANIMATIONY_Y_OFFSET_FROM_LOAD_LABEL+loadAnimation.getHeight());
	     setWidth(Math.max(loadLabel.getWidth(),loadAnimation.getWidth()));
	     setNeedLayout() ;
		
	}
   
	protected void layoutImp() {
		// TODO Auto-generated method stub
		super.layoutImp();
		loadLabel.layout();
		loadLabel.setPosition(0,0);
		int loadAnimationX=0;
		int loadAnimationY=loadLabel.getHeight()+ANIMATIONY_Y_OFFSET_FROM_LOAD_LABEL;
		loadAnimation.setPosition(loadAnimationX,loadAnimationY);
		setNeedLayout() ;
	}
	/**
	 *  start the animation
	 *
	 */
	public void start(){
		if(isStopped)
			isStopped=false;
		Util.sendDelayTimeEvent(animationTurnTime);
		EventDispatcher.getInstance().fire(Event.createEvent(Event.KEY_EVENT,Portability.KEY_DOWN,-1));
	}
       
	/** 
	 * stop the animation 
	 * 
	 */
	public void stop(){
		if(!isStopped)
			isStopped=true;
		 //ensure the time event can be fired
		 Util.sendDelayTimeEvent(animationTurnTime);
	}
	
	public void renderImpl(Graphics gc) {
		// TODO Auto-generated method stub
		loadLabel.render(gc);
		loadAnimation.render(gc);
	}
	
	public int getHeight() {
		// TODO Auto-generated method stub
		return super.getHeight();
	}

	public int getWidth() {
		// TODO Auto-generated method stub
		return super.getWidth();
	}

	public boolean isVisible() {
		// TODO Auto-generated method stub
		return super.isVisible();
	}

	public void setHeight(int h) {
		// TODO Auto-generated method stub
		super.setHeight(h);
		setNeedLayout() ;
	}

	public void setPosition(int left, int top) {
		// TODO Auto-generated method stub
		super.setPosition(left, top);
		setNeedLayout();
	}

	public void setVisible(boolean visible) {
		// TODO Auto-generated method stub
		super.setVisible(visible);
	}

	public void setWidth(int w) {
		// TODO Auto-generated method stub
		super.setWidth(w);
		setNeedLayout() ;
	}
	
	/**
	 *  Handle the timer event to switch the loading image 
	 *  @param  
	 *     evt: the event 
	 * (non-Javadoc)
	 * @see minime.core.EventListener#onEvent(minime.core.Event)
	 */

	public boolean onEvent(Event evt) {
		// TODO Auto-generated method stub
		boolean handled=false;
		if(evt.type==Event.TIMER_EVENT)
		{ 
			if(!isStopped)
			{   	
				
				++timesOfAnimationTurn;
				loadAnimation.setImage(loadAnimationImageId[timesOfAnimationTurn%loadAnimationImageId.length]);
				Util.sendDelayTimeEvent(animationTurnTime);
				handled=true;
			}
		}
		if(evt.type==Event.KEY_EVENT)
		{
			if(evt.paramA==Portability.KEY_DOWN)
				System.out.println("key down");
		}
        if (handled)
        {
            setNeedLayout();
            minime.core.Runtime.getInstance().repaint();
            return true;
        }
        return false;
	}
	
	
}
