package gui;

import java.awt.BorderLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JFrame;
import javax.swing.JOptionPane;

import main.Game;
import pawn.APawn;

/**
 * 
 * @author giuliano
 * 
 */
public class WinGame extends JFrame {

	private static final long serialVersionUID = 1L;
	public PaneGame pane;
	public int posX, posY;
	public Game grid;
	public APawn focus;
	public int[] arrow = { -1, -1, -1, -1, -1, -1 };
	public final int[] arrowN = { -1, -1, -1, -1, -1, -1 };
	public boolean att = false;
	JOptionPane jop;

	/**
	 * 
	 */
	@SuppressWarnings("static-access")
	public WinGame(Game ngrid) {
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.grid = ngrid;

		pane = new PaneGame(grid);
		pane.setLayout(new BorderLayout());
		this.setSize(700, 700);
		this.setResizable(true);
		this.setContentPane(pane);
		this.setVisible(true);
		pane.addMouseListener(new MouseAdapter() {
			public void mouseReleased(MouseEvent e) {

				if (e.getButton() == MouseEvent.BUTTON1
						&& grid.getPlayer() == 2) {
					posX = e.getX();
					posY = e.getY();
					int[] res = getRes(posX, posY);
					int line = res[0];
					int row = res[1];
					grid.showGrid();
					APawn pawn = grid.get(line, row);

					if (focus != null) {
						if (focus.movePoss(grid, line, row)) {
							grid = focus.move(grid, line, row);
							att = true;
							grid.addTurn();
							grid.setView(3);
							pane.recupArrow(arrowN);
							repaint();
							jop = new JOptionPane();
							jop.showMessageDialog(null,
									"Joueur "
											+ (((grid.getTurn() + 1) % 2) + 1)
											+ "  vous de jouer !",
									"Tour termin",
									JOptionPane.INFORMATION_MESSAGE);
							grid.setView((((grid.getTurn() + 1) % 2) + 1));
							repaint();
						}
					}
					if (pawn != null) {
						if (pawn.getTeam() == ((grid.getTurn() + 1) % 2) + 1) {

							if (pawn != null && !att) {
								focus = pawn;
								arrow = pawn.focus(grid);
								pane.recupArrow(arrow);
								repaint();
							} else {
								pane.recupArrow(arrowN);
								focus = null;
								repaint();
							}
							att = false;
							repaint();

						}
					}
				}
			}
		});
	}

	/**
	 * Transforms the coordinates of the cursor into coordinates of the grid.
	 * 
	 * @param posX
	 *            The abscissa of the cursor.
	 * 
	 * @param posY
	 *            The ordinate of the cursor.
	 * 
	 * @return An array with the abscissa and the ordinate in the grid.
	 */
	public int[] getRes(int posX, int posY) {

		int[] res = { 0, 0 };
		res[1] = (posX - (posX % (pane.getWidth() / (grid.getLine() + 1))))
				/ (pane.getWidth() / (grid.getLine() + 1));
		res[0] = (posY - (posY % (pane.getHeight() / (grid.getRow() + 1))))
				/ (pane.getHeight() / (grid.getRow() + 1));
		return res;

	}

}

@@ -1036,26 +1036,24 @@
 Event e) %7B%0D%0A
-%0D%0A
 %09%09%09%09if (e.ge
@@ -1312,26 +1312,108 @@
 ine, row);%0D%0A
+%09%09%09%09%09System.out.println(%22pawn= %22+pawn);%0D%0A%09%09%09%09%09System.out.println(%22focus= %22+focus);
 %0D%0A
-
 %09%09%09%09%09if (foc
@@ -1582,24 +1582,24 @@
 etView(3);%0D%0A
-
 %09%09%09%09%09%09%09pane.
@@ -1615,24 +1615,46 @@
 w(arrowN);%0D%0A
+%09%09%09%09%09%09%09focus = null;%0D%0A
 %09%09%09%09%09%09%09repai
@@ -2374,18 +2374,16 @@
 int();%0D%0A
-%0D%0A
 %09%09%09%09%09%09%7D%0D
@@ -2387,24 +2387,43 @@
 %09%7D%0D%0A%09%09%09%09%09%7D%0D%0A
+%09%09%09%09%09att = false;%0D%0A
 %09%09%09%09%7D%0D%0A%09%09%09%7D%0D

fix double click after change turn