import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;

/**
 * Wątek wody
 * 
 *
 */
public final class Water extends Thread{
	int x, y, i; //położenie i wlew
	int j;
	/**Ilość zmian klatek na sekundę, ustawiane przez {@link SpeedDialog}*/
	public int speed;
	boolean suspended, finished;
	
	/**
	 * Konstruktor domyślny
	 */
	public Water(){
		x=7;
		y=10;
		i=0;
		j=0;
						
		finished = false;
		pause();
		
	}
	
	/**
	 * Wstrzymuje płynięcie wody
	 */
	public synchronized void pause(){
		suspended = true;
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				Main.window.table.tablepane.setVisible(false);
				Main.window.table.tray.setVisible(false);
				Main.window.table.next.setVisible(false);
				Main.window.table.label.setVisible(true);
				Main.window.button.setText("Start");
				Main.window.button.repaint();
			}
		});
		}
	/**
	 * Kontynuacja płynięcia wody
	 */
	public synchronized void go(){
		suspended = false;
		notify();
		speed = Main.window.speed.slider.getValue();
		
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				Main.window.table.tablepane.setVisible(true);
				Main.window.table.tray.setVisible(true);
				Main.window.table.next.setVisible(true);
				Main.window.table.label.setVisible(false);
				Main.window.button.setText("Pauza");
				Main.window.button.repaint();
			}
		});
	}
	/**
	 * Kończy pracę wątku
	 */
	public synchronized void finish(){
		go();
		finished = true;
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {	
				Main.window.button.setText("Nowa gra");
				Main.window.button.repaint();
			}
		});
	}
	/**
	 * Obsługa płynięcia wody
	 */
	public void run(){
		while(true){
			if(finished)
				return;
			
			try{
				synchronized(this){
					while(suspended)
						wait();
				}
			}catch(Exception e){}
			
			try{
				sleep(1000/speed);
			}catch(Exception e){}
			
			try{
				if(x==6 && y==11)
					throw new Exception();
				j = Main.window.table.tablepane.table[x][y].pour(i);
			}
			catch(Exception e){
				String name = JOptionPane.showInputDialog(Main.window, "<html>Twój wynik: " + Main.window.table.points+"<br>Podaj imię...", "Koniec gry!", JOptionPane.INFORMATION_MESSAGE);
				Main.window.score.addScore(name, Main.window.table.points);
				finish();
				return;
			}
									
			if (j != i){
					
				SwingUtilities.invokeLater(new Runnable() {
					public void run() {
						Main.window.table.points+=(speed+5);
						Main.window.wynik.setText("Wynik: " + Main.window.table.points);
						Main.window.wynik.repaint();
					}
				});
				
				if(j == 0){
					if(y%2 == 0)
						x--;
					y++;
					i = (3+j)%6;
					continue;
				}
				if(j == 1){
					if(y%2 != 0)
						x++;
					y++;
					i = (3+j)%6;
					continue;
				}
				if(j == 2){
					x++;
					i = (3+j)%6;
					continue;
				}
				if(j == 3){
					if(y%2 != 0)
						x++;
					y--;
					i = (3+j)%6;
					continue;
				}
				if(j == 4){
					if(y%2 == 0)
						x--;
					y--;
					i = (3+j)%6;
					continue;
				}
				if(j == 5){
					x--;
					i = (3+j)%6;
					continue;
				}
			}
		}
	}	
}
