package main.java.gui.board;

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.image.BufferedImage;

import javax.swing.TransferHandler;

import main.java.data.Tile;
import main.java.gui.components.Panel;
import main.java.util.Direction;

@SuppressWarnings("serial")
public class TilePanel extends Panel implements Transferable, MouseListener, MouseMotionListener {
	
	// Properties
	
	private Tile tile = null;
	private boolean draggable = false;
	private boolean editable = false;
	private boolean isDragging = false;
	
	// Constructors
	
	public TilePanel() {
		super();
		this.addMouseListener(this);
		this.addMouseMotionListener(this);
		this.setTransferHandler(new TilePanelDragTransferHandler());
	}
	
	public TilePanel(Tile tile) {
		this();
		this.setTile(tile);
	}
	
	// Getters / setters
	
	public Tile getTile() {
		return this.tile;
	}
	
	public void setTile(Tile tile) {
		this.tile = tile;
		this.repaint();
	}
	
	public boolean isDraggable() {
		return this.draggable;
	}
	
	public void setDraggable(boolean draggable) {
		this.draggable = draggable;
	}
	
	public boolean isEditable() {
		return this.editable;
	}
	
	public void setEditable(boolean editable) {
		this.editable = editable;
	}
	
	// Drawings
	
	protected void paintComponent(Graphics g) {
		super.paintComponent(g);

		if (this.tile == null) {
			// add image when no tile
		} else {
			Graphics2D g2d = (Graphics2D)g;
			int min = Math.min(this.getWidth(), this.getHeight());
			int x = (this.getWidth() - min) / 2;
			int y = (this.getHeight() - min) / 2;
			TileImage.drawTile(g2d, this.tile, x, y, min);
		}
	}
	
	// MouseListener
	
	public void mouseReleased(MouseEvent e) {
		if (this.isDragging) {
			this.isDragging = false;
			return;
		}
		
		Direction newDirection = null;
		if (this.tile != null && this.editable != false) {
			if(e.getButton() == MouseEvent.BUTTON1) {
				newDirection = this.tile.getTileDirection().turnLeft();
		    }	    
		    else if(e.getButton() == MouseEvent.BUTTON3) {
				newDirection = this.tile.getTileDirection().turnRight();		    	
		    }
			
			if (newDirection != null) {
				this.tile.setDirection(newDirection);
				this.repaint();
			}
		}
	}
	
	public void mouseClicked(MouseEvent e) {}
	public void mouseEntered(MouseEvent e) {}
	public void mousePressed(MouseEvent e) {}
	public void mouseExited(MouseEvent e) {}

	// MouseMotionListener
	
	public void mouseDragged(MouseEvent e) {
		if (this.tile == null || this.draggable == false || this.isDragging == true) {
			return;
		}
		
		BufferedImage image = TileImage.getRotatedImage(this.tile);
		Point offset = new Point(-image.getWidth() / 2, -image.getHeight() / 2);
		
		TransferHandler handler = this.getTransferHandler();
		handler.setDragImage(image);
		handler.setDragImageOffset(offset);
		handler.exportAsDrag(this, e, TransferHandler.COPY);
		
		this.isDragging = true;
	}

	public void mouseMoved(MouseEvent e) {}
	

	// Transferable
	
	public Object getTransferData(DataFlavor flavor) {
		if (this.tile == null) return null;
		
		DataFlavor thisflavor = TilePanel.getDragDataFlavor();
		if (thisflavor != null && flavor.equals(thisflavor)) {
			return this.tile;
		} else {
			return null;
		}
	}

	public DataFlavor[] getTransferDataFlavors() {
		DataFlavor[] flavors = {null};
		flavors[0] = TilePanel.getDragDataFlavor();
		return flavors;
	}

	public boolean isDataFlavorSupported(DataFlavor flavor) {
		DataFlavor[] flavors = {null};
		flavors[0] = TilePanel.getDragDataFlavor();
		for (DataFlavor f : flavors) {
			if (f.equals(flavor)) {
				return true;
			}
		}
		return false;
	}
		
	// Data flavor
	
	private static DataFlavor dragDataFlavor = null;
	
	public static DataFlavor getDragDataFlavor() {
		if (dragDataFlavor == null) {
			dragDataFlavor = new DataFlavor(TilePanel.class, "TilePanel");
		}
		return dragDataFlavor;
	}
}

@@ -334,16 +334,49 @@
 edImage;
+%0Aimport java.rmi.RemoteException;
 %0A%0Aimport
@@ -430,24 +430,68 @@
 .data.Tile;%0A
+import main.java.gui.application.StreetCar;%0A
 import main.
@@ -2821,24 +2821,181 @@
 return;%0A%09%09%7D%0A
+%09%09try %7B%0A%09%09%09if (!StreetCar.player.getGameData().hasRemainingAction(StreetCar.player.getPlayerName())) %7B%0A%09%09%09%09return;%0A%09%09%09%7D%0A%09%09%7D catch (RemoteException e1) %7B%0A%09%09%7D%0A
 %09%09%0A%09%09Buffere

Disable drag and drop if no action is allowed