package zh2015.IP_chat;

import java.util.Random;

import javafx.application.Application;
import javafx.application.Platform;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.ListView;
import javafx.scene.control.TextField;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.ColumnConstraints;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.Priority;
import javafx.stage.Stage;

public class GUI extends Application {
	
	Label label1 = new Label("Saját portom: ");
	Label lbMyPort = new Label();
	Label label2 = new Label("Partner portja: ");
	TextField txtPort = new TextField();
	ListView<String> listMessages = new ListView<>();
	TextField txtNewMessage = new TextField();
	Button btnSend = new Button("Küldés");
	
	String lastPort = null;
	Network network;
	
	enum Who { Me, Partner, Error };
	
	void addToList(String msg, Who who) {
		Platform.runLater(new Runnable() {

			@Override
			public void run() {
				listMessages.getItems().add(who.toString() + ": " + msg);
			}
			
		});
	}
	
	void Send() {
		network.Send();
	}
	
	void CheckPort() {
		if(lastPort == null) {
			lastPort = txtPort.getText();
			return;
		}
		
		if (!lastPort.equals(txtPort)) {
			try {
				int port = Integer.parseInt(txtPort.getText());
				if (port < 16000 || 17000 < port) {
					btnSend.setDisable(false);
					network.StopConnection();
				}
				else {
					btnSend.setDisable(true);
					network.BuildConnection();
				}
			} catch (NumberFormatException e) {
				btnSend.setDisable(false);
				network.StopConnection();
			}
		}
	}
	
	public static void main(String[] args) {
		GUI.launch(args);
	}

	@Override
	public void start(Stage primaryStage) throws Exception {
		network = new Network(this);
		Random rand = new Random();
		
		lbMyPort.setText(String.valueOf(rand.nextInt(1000) + 16000));
		txtPort.setOnKeyPressed(event -> CheckPort());
		btnSend.setDisable(true);
		btnSend.setOnAction(event -> Send());
		
		GridPane top = new GridPane();
		top.add(label1, 0, 0);
		top.add(lbMyPort, 1, 0);
		top.add(label2, 0, 1);
		top.add(txtPort, 1, 1);
		top.setPadding(new Insets(5d));
		
		GridPane bottom = new GridPane();
		bottom.add(txtNewMessage, 0, 0);
		bottom.add(btnSend, 1, 0);
		bottom.setPadding(new Insets(5d));
		ColumnConstraints column1 = new ColumnConstraints();
		column1.setHgrow(Priority.ALWAYS);
		bottom.getColumnConstraints().add(column1);

		BorderPane root = new BorderPane();
		root.setTop(top);
		root.setCenter(listMessages);
		root.setBottom(bottom);
		root.setPadding(new Insets(5d));
		
		primaryStage.setTitle("IP chat");
		primaryStage.setScene(new Scene(root, 500, 600));
		primaryStage.setOnCloseRequest(exent -> Platform.exit());
		primaryStage.setMinHeight(400d);
		primaryStage.setMinWidth(400d);
		primaryStage.show();
	}

}
