import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.scene.text.TextAlignment;
import javafx.stage.Stage;

public class Conversation {
	Stage stage;
	Client client;
	TextArea messageList;
	String username;
	Conversation(String username, Client client)
	{
		this.client = client;
		this.username = username;
		BorderPane border = new BorderPane();
        
        Text title = new Text(username);
        StackPane stack = new StackPane();
        stack.setPadding(new Insets(15, 12, 15, 12));
        title.prefWidth(800);
        title.setTextAlignment(TextAlignment.CENTER);
        title.setFont(new Font(24));
        stack.getChildren().add(title);
        border.setTop(stack);
        
        HBox hbox = new HBox();
        hbox.setPadding(new Insets(15, 12, 15, 12));
        hbox.setSpacing(10);
        //hbox.setStyle("-fx-background-color: #336699;");
        
        TextField messageTextField = new TextField();
        messageTextField.setPrefSize(600,  20);
        Button buttonCurrent = new Button("Send message");
        buttonCurrent.setOnAction(new EventHandler<ActionEvent>() {

            @Override
            public void handle(ActionEvent e) {
            	String message = App.username + ": " + messageTextField.getText();
            	client.privateMessage(message, username);
            	messageTextField.setText("");
            	updateMessages(message);
            }
        });
        buttonCurrent.setPrefSize(200, 20);

        hbox.getChildren().addAll(messageTextField, buttonCurrent);
        
        border.setBottom(hbox);
        
        ScrollPane scroll = new ScrollPane();
        scroll.setFitToHeight(true);
        scroll.setFitToWidth(true);
        scroll.setPadding(new Insets(15, 12, 15, 12));
        messageList = new TextArea();
        String messages = "";
        messageList.setText(messages);
        messageList.setEditable(false);
        messageList.setWrapText(true);
        messageList.setPrefSize(525, 500);
        scroll.setContent(messageList);
        border.setCenter(scroll);
        
        stage = new Stage();
        stage.setResizable(false);
        stage.setTitle("Conversation | " + username);
        stage.setScene(new Scene(border, 800, 600));
        stage.show();
	}
	
	public void updateMessages(String text) {
		messageList.appendText(text + "\n");
	}
	
	public void show() {
		stage.show();
	}
}
