package jp.ac.kyutech.mns.ist.util;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.Timer;

import edu.umd.cs.piccolo.PNode;

public class FadeTimerForNotify implements ActionListener, Runnable{
	int keepMsec;
	int fadeMsec;
	PNode pn;
	Thread t;
	Timer timer = null;
	public static ArrayList<FadeTimerForNotify> messageFadeTimersQueue= new ArrayList<FadeTimerForNotify>();
//	public FadeTimer(PNode p, int kM, int fM){
//		this(p,kM,fM,false);
//	}
	public FadeTimerForNotify(PNode p, int kM, int fM, boolean camCenter) {
		keepMsec = kM;
		pn = p;
		fadeMsec = fM;
		t = new Thread(this);
		t.start();
		if (camCenter){
			if (messageFadeTimersQueue.size()>0) {
				for(FadeTimerForNotify f: messageFadeTimersQueue){
					if (f.pn.getOffset().getY() >= pn.getOffset().getY()){
						pn.setOffset(pn.getOffset().getX(),
								f.pn.getOffset().getY()+f.pn.getHeight()*f.pn.getScale()+3);
					}
				}
			}
			messageFadeTimersQueue.add(this);
		}

	}
	public void run() {
		try {
			Thread.sleep(keepMsec);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		Timer t = new Timer(fadeMsec, this);
		t.setRepeats(false);
		pn.animateToTransparency(0f, fadeMsec);
		t.start();
	}

	public void actionPerformed(ActionEvent e) {
		pn.removeFromParent();
		messageFadeTimersQueue.remove(this);
	}
}
