/*
 * Copyright
 *   2007 axYus - www.axyus.com
 *   2005 C.Marchand - christophe.marchand@axyus.com
 *
 * This file is part of XEMELIOS.
 *
 * XEMELIOS is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * XEMELIOS is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with XEMELIOS; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

package fr.gouv.finances.cp.utils.ui;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionAdapter;
import java.awt.event.MouseMotionListener;
import java.awt.geom.AffineTransform;

import javax.swing.JComponent;
import javax.swing.SwingUtilities;
/**
 * This class draws the rotating clock-like panel while it is visible
 * @author chm
 *
 */
public class JRotatePanel extends JComponent {
	/**
	 * 
	 */
	private static final long serialVersionUID = -8892664236789395368L;
	private boolean stop;
//	private boolean alreadyPaintBack = false;
	MouseListener ml = null;
	MouseMotionListener mml = null;
	KeyListener kl = null;
	public JRotatePanel() {
		super();
		ml = new MouseAdapter(){};
		mml = new MouseMotionAdapter() {};
		kl = new KeyAdapter() {};
	}
	
	public void start() {
		setVisible(true);
	}

	@Override
	protected void paintComponent(Graphics g) {
		super.paintComponent(g);
		Graphics2D g2d = (Graphics2D)g;
		Image drkImg = createImage(false);
		Image clrImg = createImage(true);

		int midX = getWidth()/2;
		int midY = getHeight()/2;
		int imgWidth = drkImg.getWidth(this)/2;
		int imgHeight = drkImg.getHeight(this)/2;

//		int xRadius = (int)((midX-clrImg.getWidth(this)) * 0.8);
//		int yRadius = (int)((midY-clrImg.getHeight(this)) * 0.8);
		
		int width = clrImg.getWidth(this);
		int xRadius = (int)(Math.min(midX,midY)-width);
		int yRadius = xRadius;
		
//		if(!alreadyPaintBack) {
//			alreadyPaintBack = true;
//		}
		
		int sec = (int)((System.currentTimeMillis() / 500 ) % 12)+1;
		for(int i=1;i<=12;i++) {
			
			double angle = Math.PI/2.0 + Math.PI*2*i/12;
			int xPos = (int)(midX + Math.cos(angle)*xRadius);
			int yPos = (int)(midY + Math.sin(angle)*yRadius);
			
			AffineTransform at = AffineTransform.getTranslateInstance(xPos,yPos);
			at.rotate(angle);
			at.translate(-imgWidth,-imgHeight);
			
			g2d.drawImage(i==sec?clrImg:drkImg,at,this);
		}
	}
	public void paintBackground(Graphics g) {
            if(g!=null) {
		g.setColor(new Color(255,255,255,167));
		g.fillRect(0,0,getWidth(),getHeight());
            }
	}

	@Override
	public void setVisible(boolean aFlag) {
//		System.out.println("setVsibile("+Boolean.toString(aFlag)+")");
		super.setVisible(aFlag);
		if(aFlag) {
			stop = false;
//			alreadyPaintBack=false;
			RePainter rp = new RePainter();
			setFocusTraversalKeysEnabled(false);
			addMouseListener(ml);
			addMouseMotionListener(mml);
			addKeyListener(kl);
			requestFocusInWindow();
			paintBackground(getGraphics());
			rp.start();
		} else {
			stop = true;
			setFocusTraversalKeysEnabled(true);
			removeMouseListener(ml);
			removeMouseMotionListener(mml);
			removeKeyListener(kl);
		}
	}
	
	private Image createImage(boolean light) {
		Image img = createImage(25,10);
                if(img!=null) {
                    Graphics g = img.getGraphics();
                    g.setColor(light?Color.GRAY:Color.LIGHT_GRAY);
                    g.fill3DRect(0,0,25,10,true);
                }
		return img;
	}
	
	private class RePainter extends Thread {
		@Override
		public void run() {
			setPriority(Thread.MIN_PRIORITY);
			while(!JRotatePanel.this.stop) {
				try {
					SwingUtilities.invokeAndWait(new Thread() {
						@Override
						public void run() {
							JRotatePanel.this.paint(JRotatePanel.this.getGraphics());
						}
					});
					Thread.sleep(500);
				} catch(Throwable t) {}
			}
		}
	}
}
