package main.java.gui.board;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.dnd.DropTarget;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.LinkedList;

import main.java.data.Data;
import main.java.data.Tile;
import main.java.gui.components.Panel;
import main.java.player.PlayerIHM;


@SuppressWarnings("serial")
public class MapPanel extends Panel implements MouseListener, ComponentListener {

	// Properties

    private int originX;
    private int originY;
    private int mapWidth;
    private int cellWidth;
    public Data data;
    // Constructors
    
	public MapPanel() {
		this.setBackground(Color.WHITE);
		this.addMouseListener(this);
		this.addComponentListener(this);

		MapPanelDropTargetListener dropTarget = new MapPanelDropTargetListener(this);
        this.setDropTarget(new DropTarget(this, dropTarget));
	}
	
	// Cells positions
	
	protected void updateMapGeometry() {
		if (this.data == null) return;
		this.mapWidth = (int) (0.96 * Math.min(this.getWidth(), this.getHeight()));
		this.cellWidth = this.mapWidth / this.data.getWidth();
	    this.originX = (this.getWidth() - Math.round(this.mapWidth)) / 2;
	    this.originY = (this.getHeight() - Math.round(this.mapWidth)) / 2;
	    
		this.repaint();
	}
	
	public Point cellPositionForLocation(Point location) {
		if (location.x >= this.originX &&  location.x <= this.originX + this.mapWidth &&
			location.y >= this.originY &&  location.y <= this.originY + this.mapWidth) {
			Point cellPosition = new Point();
			cellPosition.x = (int)((float)(location.x - this.originX) / this.cellWidth);
			cellPosition.y = (int)((float)(location.y - this.originY) / this.cellWidth);
			return cellPosition;
		} else {
			return null;
		}
	}
	
	// Drawings
	
	protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        
        if (this.data == null) {
        	System.out.println("data is null");
        	return;
        }
        
        int x = this.originX;
        int y = this.originY;
        Graphics2D g2d = (Graphics2D)g; 
    	Tile[][] board = this.data.getBoard();
    	
		for (int j=0; j < this.data.getHeight(); j++) {
			for (int i=0; i < this.data.getWidth(); i++) {
				Tile tile = board[i][j];
				TileImage.drawTile(g2d, tile, x, y, this.cellWidth);
				x += this.cellWidth;
			}
			x = this.originX;
			y += this.cellWidth;
		}
    }
	
	// Mouse Listener
	
	public void mouseClicked(MouseEvent e) {
		Point p = this.cellPositionForLocation(e.getPoint());
		if (p != null) {
			System.out.println(p);
		}
	}

	public void mousePressed(MouseEvent e) {}
	public void mouseReleased(MouseEvent e) {}   
	public void mouseEntered(MouseEvent e) {}
	public void mouseExited(MouseEvent e) {}
	
	// ComponentListener
	
	public void componentResized(ComponentEvent e) {
		this.updateMapGeometry();
	}

	public void componentMoved(ComponentEvent e) {}
	public void componentShown(ComponentEvent e) {}
	public void componentHidden(ComponentEvent e) {}
	
	// Refresh game

	public void refreshGame(PlayerIHM player, Data data) {
		this.data = data;
		this.updateMapGeometry();
	}

	public void setData(Data d) {
		data = d;
	}

	public void startMaidenVoyage(Point point) {
		// TODO Auto-generated method stub
		
	}

	public void moveTram(LinkedList<Point> tramPath) {
		// TODO Auto-generated method stub
		
	}
}

@@ -391,16 +391,60 @@
 a.Tile;%0A
+import main.java.gui.application.StreetCar;%0A
 import m
@@ -745,30 +745,8 @@
 th;%0A
-    public Data data;%0A
     
@@ -1100,19 +1100,66 @@
 ) %7B%0A
+        Data data = StreetCar.player.getGameData();%0A
 %09%09if (
-this.
 data
@@ -1176,16 +1176,19 @@
 return;%0A
+%09%09%0A
 %09%09this.m
@@ -1292,21 +1292,16 @@
 Width / 
-this.
 data.get
@@ -2071,17 +2071,64 @@
     
-if (this.
+Data data = StreetCar.player.getGameData();%0A        if (
 data
@@ -2343,21 +2343,16 @@
 board = 
-this.
 data.get
@@ -2386,21 +2386,16 @@
 =0; j %3C 
-this.
 data.get
@@ -2432,21 +2432,16 @@
 =0; i %3C 
-this.
 data.get
@@ -3319,94 +3319,27 @@
 his.
-data = data;%0A%09%09this.updateMapGeometry();%0A%09%7D%0A%0A%09public void setData(Data d) %7B%0A%09%09data = d
+updateMapGeometry()
 ;%0A%09%7D

Remove local reference to the data in map panel