import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JLayeredPane;

/**
 * Reprezentuje heksagonalną tabelę elementów {@link Hex}
 * 
 *
 */

public final class Tablepane extends JLayeredPane{
	/**globalna szerokość jednego Hexa*/
	final static int bs = 51;
	/**globalna długość boku jednego Hexa*/
	final static int s = 30;
	/**szerokość tabeli*/
	int w;
	/**wysokość tabeli*/
	int h;
	
	JLabel focus, set;
	/**współrzędne zaznaczenia*/
	int[] isset = new int[2];
	/**tablica @see Hex*/
	Hex[][] table;
	
	/**
	 * Konstruktor
	 * @param x szerokość tabeli
	 * @param y wysokość tabeli
	 */
	public Tablepane(int x, int y){
		super();
		
		TableListener listener = new TableListener();
		addMouseMotionListener(listener);
		addMouseListener(listener);
		
		w=x;
		h=y;
		table = new Hex[w][h];
		
		//Tworzenie siatki
		painttile();
				
		//zaznacznie
		final ImageIcon iset = createImageIcon("red.png", "podświetlenie");
		set = new JLabel(iset);
		set.setBounds(1000, 1000, bs+4, 2*s+4);
		isset[0] = 1000;
		isset[1] = 1000;
		add(set, new Integer(3), 0);
		
		//focus
		final ImageIcon ifocus = createImageIcon("blue.png", "podświetlenie");
		focus = new JLabel(ifocus);
		focus.setBounds(1000, 1000, bs+4, 2*s+4);
		add(focus, new Integer(3), 0);
		
		//test Hexa
		//addHex(0, 0, new Hex());
		//addHex(0, 1, new Hex());
		//addHex(1, 1, new Hex());
		//addHex(7, 7, new Hex());
	}
	
	//rysowanie sześciokątów
	void painttile(){
		ImageIcon web = createImageIcon("empty.png", "pusty sześciokąt");
		
		for (int i = 0; i < w; i++){
			for (int j = 0; j < h; j++){
				JLabel label = new JLabel(web);
				label.setBounds(i*bs+(j%2)*bs/2, j*s*3/2, bs+4, 2*s+4);
				add(label, new Integer(0), 1);
			}
		}
	}
	/**
	 * Dodawanie elementów {@link Hex}
	 * @param x
	 * @param y
	 * @param hex
	 */
	public void addHex(int x, int y, Hex hex){
		table[x][y] = hex;
		
		for(int i=0; i<hex.elems.length; i++){
			Hexelem hexelem = hex.elems[i];
			if(hexelem == null)
				break;
			hexelem.setBounds(x*bs+(y%2)*bs/2+2, y*s*3/2+2, bs, 2*s);
			add(hexelem, new Integer(1), 0);
		}
	}
	
	/**
	 * Ustawienie czerwonego zaznaczenia
	 * @param x
	 * @param y
	 */
	public void setred(int x,int y){
		if(x>=0 && x<w && y>=0 && y<h){
			set.setLocation(x*bs+(y%2)*bs/2, y*s*3/2);
			isset[0] = x;
			isset[1] = y;
		}
		else{
			isset[0]=1000;
			isset[1]=1000;
			set.setLocation(1000, 1000);
		}
	}
	
	/**
	 * Ustawienie niebieskiego podświetlenia
	 * @param x
	 * @param y
	 */
	public void setblue(int x, int y){
		if(x>=0 && x<w && y>=0 && y<h)
			focus.setLocation(x*bs+(y%2)*bs/2, y*s*3/2);
				
		else
			focus.setLocation(1000, 1000);
	}
		
	/**
	 * Tworzenie ikony z pliku
	 * @param path Ścieżka dostępu
	 * @param description Opis ikony
	 * @return {@link javax.swing.ImageIcon} lub null jeśli ścieżka nieprawidłowa
	 */
	protected ImageIcon createImageIcon(String path,
	                                           String description) {
	    java.net.URL imgURL = getClass().getResource(path);
	    if (imgURL != null) {
	        return new ImageIcon(imgURL, description);
	    } else {
	        System.err.println("Couldn't find file: " + path);
	        return null;
	    }
	}	
}
