package automatvgi.widgets;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;

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

import automatvgi.Direction;
import automatvgi.tools.Point;

/**
 * 
 * @author lombardy
 * Rose des vents composee de neuf boutons disposes en carre;
 * les 8 boutons peripheriques correspondent a une des 8 directions
 * definies dans Direction
 * le bouton du centre correspond a null
 * 
 * La rose des vent est "ecout�e" par un DirectionSetter
 */

public class WindRose extends JFrame implements ActionListener{

	private static final long serialVersionUID = 1L;
	
	/**
	 * Correspondance Bouton-Direction
	 */
	private HashMap<ButtonRose, Direction> map= new HashMap<ButtonRose, Direction>(9);
	
	private DirectionSetter ds;
	
	
	/**
	 * Constructeur
	 * @arg ds : le DirectionSetter qui écoute la WindRose
	 * Crée le panel qui contiendra les boutons, puis les 9 boutons
	 * en les associant aux directions et en les faisant �couter par la rose des vents 
	 */
	public WindRose(DirectionSetter ds){
		this.ds = ds;
		JPanel p=new JPanel();
		p.setLayout(new GridLayout(3,3));
		Direction[] d={	Direction.NORTH_WEST, Direction.NORTH, Direction.NORTH_EAST,
						Direction.WEST      , null           , Direction.EAST,
						Direction.SOUTH_WEST, Direction.SOUTH, Direction.SOUTH_EAST};
		for(int i=0;i<9;i++){
			ButtonRose br=new ButtonRose(i%3-1,i/3-1);
			map.put(br, d[i]);
			br.addActionListener(this);
			p.add(br);
		}
		setContentPane(p);
		pack();
		setResizable(false);
		setVisible(true);
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
	}

	/**
	 * Lorsque l'on clique sur un bouton, cette m�thode est ex�cut�e et
	 * le DirectionSetter re�oit la direction associ�e
	 */
	
	public void actionPerformed(ActionEvent e) {
		ds.setDirection(map.get(e.getSource()));
		//dispose();
	}

	/**
	 * @author lombardy
	 * Chaque ButtonRose correspond a un des 8 boutons et porte le dessin (translat�
	 * selon la position du bouton) de la rose des vents
	 */
	private static class ButtonRose extends JButton{
		private static final long serialVersionUID = 1L;
		private int nX,nY;
		private ButtonRose(int nX, int nY) {
			this.nX = nX;
			this.nY = nY;
			setPreferredSize(new Dimension(80,80));
		}
		
		private void triangle(Graphics g, Point p, Point q, Point r, Color c){
			int offX=getWidth()*(1-2*nX)/2, offY=getHeight()*(1-2*nY)/2, scale=getWidth()/6;
			int[] x={ offX+scale*(int)p.getX(), offX+scale*(int)q.getX(), offX+scale*(int)r.getX() };
			int[] y={ offY+scale*(int)p.getY(), offY+scale*(int)q.getY(), offY+scale*(int)r.getY() };
			g.setColor(c);
			g.fillPolygon(x, y, 3);
		}
		
		@Override
		public void paintComponent(Graphics g){
			g.setColor(Color.GRAY);
			g.fillRect(0, 0, getWidth(),getHeight());
			Point c=new Point(0,0),
		      a1=new Point(5,5),
		      a2=new Point(-5,5),
		      a3=new Point(-5,-5),
		      a4=new Point(5,-5),
		      b1=new Point(0,3),
		      b2=new Point(-3,0),
		      b3=new Point(0,-3),
		      b4=new Point(3,0);
			triangle(g, c, a1, b1, Color.WHITE);
			triangle(g, c, a2, b2, Color.WHITE);
			triangle(g, c, a3, b3, Color.WHITE);
			triangle(g, c, a4, b4, Color.WHITE);
			triangle(g, c, a1, b4, Color.BLACK);
			triangle(g, c, a2, b1, Color.BLACK);
			triangle(g, c, a3, b2, Color.BLACK);
			triangle(g, c, a4, b3, Color.BLACK);
		      a1=new Point(2,2);
		      a2=new Point(-2,2);
		      a3=new Point(-2,-2);
		      a4=new Point(2,-2);
		      b1=new Point(0,8);
		      b2=new Point(-8,0);
		      b3=new Point(0,-8);
		      b4=new Point(8,0);
			triangle(g, c, a1, b4, Color.WHITE);
			triangle(g, c, a2, b1, Color.WHITE);
			triangle(g, c, a3, b2, Color.WHITE);
			triangle(g, c, a4, b3, Color.WHITE);
			triangle(g, c, a1, b1, Color.BLACK);
			triangle(g, c, a2, b2, Color.BLACK);
			triangle(g, c, a3, b3, Color.BLACK);
			triangle(g, c, a4, b4, Color.BLACK);
		}

	}
}
