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

import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JFrame;
import javax.swing.JPanel;

import edu.umd.cs.piccolo.PNode;
import edu.umd.cs.piccolo.util.PAffineTransform;

public class DebugPanel extends JPanel implements MouseListener, Runnable {
	private static final long serialVersionUID = -3492532094809305319L;
	PNode target;
	JFrame jf;
	Thread thread;
	public DebugPanel(PNode _target, String title){
		target = _target;
		jf = new JFrame(title+" DebugPanel");
		jf.setSize(400,300);
		jf.getContentPane().add(this);
		jf.setVisible(true);
		addMouseListener(this);
		thread = new Thread(this);
		thread.start();
	}
	public void paint(Graphics g){
		int x = 0, y=10;
		g.clearRect(0,0,getWidth(),getHeight());
		g.drawString("B ", x, y);
		x = x + 40;
		g.drawString(String.format("%4.2f", target.getBounds().x), x, y);
		x = x + 40;
		g.drawString(String.format("%4.2f", target.getBounds().y), x, y);
		x = x + 40;
		g.drawString(String.format("%4.2f", target.getBounds().width), x, y);
		x = x + 40;
		g.drawString(String.format("%4.2f", target.getBounds().height), x, y);
		
		x = 0;
		y += 15;
		g.drawString("GB ", x, y);
		x = x + 40;
		g.drawString(String.format("%4.2f", target.getGlobalBounds().x), x, y);
		x = x + 40;
		g.drawString(String.format("%4.2f", target.getGlobalBounds().y), x, y);
		x = x + 40;
		g.drawString(String.format("%4.2f", target.getGlobalBounds().width), x, y);
		x = x + 40;
		g.drawString(String.format("%4.2f", target.getGlobalBounds().height), x, y);

		PAffineTransform at = target.getTransform();
		double mat[] = new double[6];
		at.getMatrix(mat);
		x = 0;
		y += 15;
		g.drawString("T ", x, y);
		x = x + 40;	g.drawString(String.format("%4.2f", mat[0]), x, y);
		x = x + 40; g.drawString(String.format("%4.2f", mat[2]), x, y);
		x = x + 40; g.drawString(String.format("%4.2f", mat[4]), x, y);
		x = 0;
		y += 15;
		x = x + 40; g.drawString(String.format("%4.2f", mat[1]), x, y);
		x = x + 40; g.drawString(String.format("%4.2f", mat[3]), x, y);
		x = x + 40; g.drawString(String.format("%4.2f", mat[5]), x, y);
		
		x = 0;
		y += 15;
		x = x + 40;
		g.drawString(String.format("S %4.2f", at.getScale()), x, y);
		x = x + 40;
		g.drawString(String.format("R %4.2f", at.getRotation()), x, y);
		x = x + 40;
		
		at = target.getGlobalToLocalTransform(at);
		at.getMatrix(mat);
		x = 0;
		y += 15;
		g.drawString("G2LT ", x, y);
		x = x + 40;	g.drawString(String.format("%4.2f", mat[0]), x, y);
		x = x + 40; g.drawString(String.format("%4.2f", mat[2]), x, y);
		x = x + 40; g.drawString(String.format("%4.2f", mat[4]), x, y);
		x = 0;
		y += 15;
		x = x + 40; g.drawString(String.format("%4.2f", mat[1]), x, y);
		x = x + 40; g.drawString(String.format("%4.2f", mat[3]), x, y);
		x = x + 40; g.drawString(String.format("%4.2f", mat[5]), x, y);
		
		x = 0;
		y += 15;
		x = x + 40;
		g.drawString(String.format("S %4.2f", at.getScale()), x, y);
		x = x + 40;
		g.drawString(String.format("R %4.2f", at.getRotation()), x, y);
		x = x + 40;

		
		g.drawString(String.valueOf(System.currentTimeMillis()), 0, getHeight()-10);
	}
	@Override
	public void mouseClicked(MouseEvent arg0) {
		if (arg0.getButton()==1) new DebugPanel(target.getParent(), "Parent");
		else repaint();
	}
	@Override
	public void mouseEntered(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void mouseExited(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void mousePressed(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void mouseReleased(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void run() {
		while(thread != null){
			repaint();
			try {
				Thread.sleep(500);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
}
