package main.java.author.view.tabs.terrain;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.imageio.ImageIO;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JPanel;

import main.java.author.controller.MainController;
import main.java.author.view.tabs.EditorTab;
import main.java.author.view.tabs.terrain.types.Grass;
import main.java.author.view.tabs.terrain.types.Ground;
import main.java.author.view.tabs.terrain.types.TileObject;
import main.java.author.view.tabs.terrain.types.Tree;
import main.java.author.view.tabs.terrain.types.Water;

import static main.java.author.controller.ActionListenerUtil.actionListener;

public class TerrainEditorTab extends EditorTab{

	private List<TileObject> availableTiles;
	private static final String TERRAIN_TYPE_PCKG = "main.java.author.view.tabs.terrain.types.";
	private static final String CLEAR = "Clear Tiles";
	private String [] terrainTypes = { "Ground", "Grass", "Water", "Tree" };
	private Canvas myCanvas;
	
	public TerrainEditorTab(MainController controller){
		super(controller);
		initTerrainTypes();
		add(myCanvas = new Canvas(), BorderLayout.CENTER);
		add(getTileList(), BorderLayout.EAST);
		add(initClearButton(), BorderLayout.SOUTH);
	}
	
	private void initTerrainTypes() {
		availableTiles = new ArrayList<TileObject>();		
		for (String terrainType : terrainTypes) {
			try {
				final TileObject tileObj = (TileObject) Class.forName(TERRAIN_TYPE_PCKG + terrainType).newInstance();
				tileObj.addActionListener(new ActionListener() {
					@Override
					public void actionPerformed(ActionEvent e) {
						myCanvas.setSelectedTileObj(tileObj);
					}
				});
				availableTiles.add(tileObj);
			} catch (Exception e) { e.printStackTrace(); } 
		}
	}
	
	private JButton initClearButton() {
		JButton clearButton = new JButton(CLEAR);
		clearButton.addActionListener(actionListener(this, "clearCanvasTiles"));
		return clearButton;
	}
	
	public void clearCanvasTiles(ActionEvent event) {
		myCanvas.clearTiles();
	}

	private JPanel getTileList() {
		GridBagConstraints c = new GridBagConstraints();
		c.gridy = 1;
		JPanel panel = new JPanel();
		for (TileObject tObj : availableTiles) {
			try {
				Image img = ImageIO.read(new File(Tile.DEFAULT_IMAGE_PACKAGE + tObj.getImage()));
				tObj.setIcon(new ImageIcon(img));
				tObj.setBackground(tObj.getBGColor());
				tObj.setOpaque(true);
				tObj.setPreferredSize(new Dimension(35,35));
			} catch (IOException ex) {}
			panel.add(tObj, c);
			c.gridy++;
		}
		panel.setPreferredSize(new Dimension(50,200)); // important for maintaining size of JPanel
		return panel;
	}

}

@@ -1305,16 +1305,52 @@
 ree%22 %7D;%0A
+%09private TileObject mySelectedTile;%0A
 %09private
@@ -1367,16 +1367,16 @@
 Canvas;%0A
-
 %09%0A%09publi
@@ -1752,22 +1752,16 @@
 y %7B%0A%09%09%09%09
-final 
 TileObje
@@ -1878,21 +1878,17 @@
 istener(
-new A
+a
 ctionLis
@@ -1897,133 +1897,38 @@
 ner(
-) %7B%0A%09%09%09%09%09@Override%0A%09%09%09%09%09public void actionPerformed(ActionEvent e) %7B%0A%09%09%09%09%09%09my
+this, %22update
 Canvas
-.set
 Select
-edTileObj(tileObj);%0A%09%09%09%09%09%7D%0A%09%09%09%09%7D
+ion%22)
 );%0A%09
@@ -2206,88 +2206,8 @@
 %7D%0A%09%0A
-%09public void clearCanvasTiles(ActionEvent event) %7B%0A%09%09myCanvas.clearTiles();%0A%09%7D%0A%0A
 %09pri
@@ -2790,16 +2790,16 @@
  JPanel%0A
-
 %09%09return
@@ -2810,10 +2810,202 @@
 el;%0A%09%7D%0A%0A
+%09public void updateCanvasSelection(ActionEvent e) %7B%0A%09%09myCanvas.setSelectedTileObj((TileObject) e.getSource());%0A%09%7D%0A%09%0A%09public void clearCanvasTiles(ActionEvent e) %7B%0A%09%09myCanvas.clearTiles();%0A%09%7D%0A%0A
 %7D%0A

update use of reflection