package main.java.author.view.tabs.terrain;
import javax.swing.*;

import main.java.author.view.tabs.terrain.types.TileObject;

import java.awt.*;
import java.awt.event.InputEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.awt.geom.Line2D;
import java.awt.image.BufferedImage;
import java.util.*;
import java.util.List;

public class Canvas extends JPanel {

	public static final Color DEFAULT_TILE_COLOR = Color.LIGHT_GRAY;
	public static final Color DEFAULT_BORDER_COLOR = Color.BLACK;

	public static final int NUM_ROWS = 10;
	public static final int NUM_COLS = 15;
	public static final int TILE_SIZE = 50; // in pixels

	private final Tile[][] myTiles;
	private TileObject selectedTileObj;

	public Canvas(){
		myTiles = new Tile[NUM_COLS][NUM_ROWS];
		for (int col = 0; col < NUM_COLS; col++) {
			for (int row = 0; row < NUM_ROWS; row++) {
				myTiles[col][row] = new Tile(row, col, DEFAULT_TILE_COLOR);
			}
		}
		setPreferredSize(new Dimension(NUM_COLS*TILE_SIZE, NUM_ROWS*TILE_SIZE)); // important for maintaining size of JPanel
		addMouseListener(new MouseAdapter() {
			@Override
			public void mousePressed(MouseEvent e) {
				updateTile(e);
			}
		});
		addMouseMotionListener(new MouseMotionAdapter()
		{
			@Override
			public void mouseDragged(MouseEvent e) {
				updateTile(e);
			}
		});	
	}

	/**
	 * Updates tiles within the JPanel when the 'repaint()' method is called. Sets the
	 * appropriate background and border of each tile accordingly. 
	 */
	@Override
	public void paintComponent(Graphics g) {
		super.paintComponent(g); // Call to super class is necessary
		g.clearRect(0, 0, getWidth(), getHeight());

		int rectWidth = getWidth() / NUM_COLS;
		int rectHeight = getHeight() / NUM_ROWS;

		int index = 0;
		for (Tile tile : getTiles()) {
			// Upper left corner of the tile
			int x = tile.getCol() * rectWidth;
			int y = tile.getRow() * rectHeight;
			Color tileColor = tile.getColor();
			Image tileImage = tile.getImage();

			if (tileImage == null) {
				g.setColor(tileColor);
				g.fillRect(x, y, rectWidth, rectHeight); // filling appropriate Tile background colors
			} else {
				g.drawImage(tileImage,x,y, rectWidth, rectHeight, tileColor, null);
			}
			g.setColor(DEFAULT_BORDER_COLOR);
			g.drawRect(x, y, rectWidth, rectHeight); // drawing appropriate Tile borders
		}
	}

	/**
	 * Obtains the specified Tile within the JPanel
	 * @param x the x-coordinate of the JPanel
	 * @param y the y-coordinate of the JPanel
	 * @return the Tile for a specific location
	 */
	private Tile getTile(int x, int y) {
		int row = y/TILE_SIZE;
		int col = x/TILE_SIZE;
		boolean validRow = row >= 0 && row <= NUM_ROWS - 1;
		boolean validCol = col >= 0 && col <= NUM_COLS - 1;
		if (validRow && validCol) {
			return myTiles[col][row];
		}
		return null;
	}

	/**
	 * Obtains a list of tiles within the JPanel
	 * @return all tiles within the JPanel
	 */
	private List<Tile> getTiles() {
		List<Tile> tiles = new ArrayList<Tile>();
		for (int i = 0; i < NUM_COLS; i++) {
			for (int j = 0; j < NUM_ROWS; j++) {
				tiles.add(myTiles[i][j]);
			}
		}
		return tiles;
	}

	private void updateTile(MouseEvent e) {
		Tile tile = getTile(e.getX(), e.getY());
		if (tile == null) {
			return;
		}
		tile.setImage((selectedTileObj == null) ? null : selectedTileObj.getImage());
		tile.setColor((selectedTileObj == null) ? DEFAULT_TILE_COLOR : selectedTileObj.getBGColor());
		tile.setPassIndex((selectedTileObj == null) ? 0 : selectedTileObj.getPassabilityIndex());
		repaint(); 
	}
	
	protected void clearTiles() {
		for (Tile tile : getTiles()) {
			tile.setImage(null);
			tile.setColor(DEFAULT_TILE_COLOR);
			repaint();
		}
	}

	public void setSelectedTileObj(TileObject tObj) {
		selectedTileObj = tObj;
	}
}
@@ -817,21 +817,21 @@
 NUM_
-COL
+ROW
 S%5D%5BNUM_
-ROW
+COL
 S%5D;%0A
@@ -845,36 +845,36 @@
 int 
-col
+row
  = 0; 
-col
+row
  %3C NUM_
-COLS; col
+ROWS; row
 ++) 
@@ -891,36 +891,36 @@
 int 
-row
+col
  = 0; 
-row
+col
  %3C NUM_
-ROWS; row
+COLS; col
 ++) 
@@ -933,24 +933,24 @@
 myTiles%5B
-col%5D%5Brow
+row%5D%5Bcol
 %5D = new 
@@ -2833,16 +2833,16 @@
 les%5B
-col%5D%5Brow
+row%5D%5Bcol
 %5D;%0A%09
@@ -3062,19 +3062,19 @@
 i %3C NUM_
-COL
+ROW
 S; i++) 
@@ -3102,19 +3102,19 @@
 j %3C NUM_
-ROW
+COL
 S; j++) 

update row col config