package application;

import javafx.event.EventHandler;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.AnchorPane;

/**
 * This class is used for a singular cell of the
 * grid.
 * 
 * */

@SuppressWarnings("unused")
public class GridComponent extends AnchorPane {
	
	final int GRID_SIZE = 8;
	Button btnComp = null;
	public String btnID = null;
	public String componentName = null;
	public int player_num = 0;
	public Label imageLabel = null;
	
	//Offset for conversion from Character -> Integer.
	private int charOffset = 48;
	
	ImageView pieceImage = null;
	
	double xStart, yStart;
	double orgTranslateX, orgTranslateY;
	double changeX, changeY;
	
	/**
	 * This constructor method sets up the grid
	 * component. 
	 * 
	 * @param int					Width of stage.
	 * @param int					Height of stage.
	 * @param String				Piece to place on this grid.
	 * @param String				ID of grid.
	 * 
	 * 
	 * */
	public GridComponent(int width, int height, String text, String id){
		
		//If we're adding a piece.
		if(!text.equals("")){
			String path = Main.class.getClassLoader().getResource("application/ChessIcons/"+text+".png").toString();
			pieceImage = new ImageView(new Image(path));
			imageLabel = new Label("", pieceImage);
		}
		
		//If the space is blank.
		else{
			imageLabel  = new Label("");
		}
		
		imageLabel.setLayoutX(width/55);
		imageLabel.setLayoutY(height/40);
		
		//Add button to grid component. 
		btnComp = new Button();
		btnComp.setPrefWidth(width/8);
		btnComp.setPrefHeight(height/8);
		btnComp.setText(text);
		btnComp.setStyle("-fx-font: 25px arial;");
		
		btnID = id;
		componentName = text;
		this.setId(btnID);
		setButtonStyle();
		this.getChildren().add(imageLabel);
		//this.getChildren().add(btnComp);
	}	
	
	
	/**
	 * 
	 * 
	 * */
	
	private void setButtonStyle(){
		char x = btnID.charAt(0);
		char y = btnID.charAt(1);
		int i = (int)(x) - charOffset;
		int j = (int)(y) - charOffset;
		
		//Setting background colour of tile. 
		/*If the addition of the row and column numbers is even then
		 * colour dark. Otherwise colour light.*/
		if((i+j)%2==0)
			this.setStyle("-fx-background-color:#DAA520;");
		else
			this.setStyle("-fx-background-color:#949494;");
	
	}
}
