package edu.govschool.govchat.gui;

// We need to import our socket package
import edu.govschool.govchat.socket.*;
// JavaFX imports
import javafx.application.*;
import javafx.geometry.*;
import javafx.scene.*;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.stage.*;

/**
 * GUI for the GovChat client.
 * Most of the actual work is handled with <code>GCClientSocket</code>, this
 * provides us with a GUI to work with this socket and the entire client in
 * general.
 * @author Mr. Davis
 */
public class GCClient extends Application
{
    // Our GUI initially is very spartan, consisting of a large text area to
    // display messages sent and received, a entry field to enter messages, a
    // connect button and a send button. At first, we just connect using the
    // default host and port, localhost and 2000, so we don't need any elements
    // to input a custom host and port yet.
    private TextArea msgArea;
    private TextField entryField;
    private Button sendBtn;
    private Button connectBtn;
    
    // We need a client socket, since this is a client GUI
    private GCClientSocket clientSocket;
    
    // This flag will tell us if we're connected or not
    private boolean socketClosed = true;
    
    public static void main(String[] args)
    {
        launch(args);
    }
    
    @Override
    public void start(Stage primaryStage)
    {
        // Initialize our GUI elements
        msgArea = new TextArea();
        entryField = new TextField();
        sendBtn = new Button("Send");
        connectBtn = new Button("Connect to localhost");
        
        // Set some options on our elements
        msgArea.setEditable(false); // Our messages should be read-only
        msgArea.setPrefColumnCount(20);
        msgArea.setPrefRowCount(20); // 20rows x 20characters big
        msgArea.setWrapText(true); // Our text will wrap if it's too big
        entryField.setPrefColumnCount(20); // Entry field is 20 characters wide
        entryField.setPromptText("Enter a message");
        
        // Set our event handlers
        sendBtn.setOnAction(e -> sendBtn_click());
        connectBtn.setOnAction(e -> connectBtn_click());
        
        // Organize our GUI
        HBox entryPane = new HBox(10, entryField, sendBtn);
        VBox pane = new VBox(10, msgArea, entryPane, connectBtn);
        pane.setAlignment(Pos.CENTER);
        
        // Display our GUI
        Scene scene = new Scene(pane);
        primaryStage.setScene(scene);
        
        // Set our window options
        primaryStage.setTitle("GovChat Client");
        primaryStage.setOnCloseRequest(e -> {
            if (clientSocket != null) clientSocket.shutdown();
        });
        primaryStage.show();
    }
    
    private void sendBtn_click()
    {
        
    }
    
    private void connectBtn_click()
    {
    
    }
    
    private class ClientSocketListener implements GCSocketListener
    {
        @Override
        public void onMessage(final String line)
        {
            
        }
        
        @Override
        public void onCloseUpdate(final Boolean isClosed)
        {
            
        }
    }
}
    
@@ -2806,77 +2806,1070 @@
     
-%0A    %7D%0A    %0A    private void connectBtn_click()%0A    %7B%0A    %0A    %7D%0A    
+// Create a temporary variable to store our message%0A        final String msg = entryField.getText();%0A        // Only send non-empty messages%0A        if (!msg.equals(%22%22)) %7B%0A            // Send the message over the connection%0A            clientSocket.sendMessage(msg);%0A            // Update our own message area%0A            msgArea.appendText(%22CLIENT: %22 + msg + %22%5Cn%22);%0A            // Set the entry field text to the empty string%0A            entryField.setText(%22%22);%0A        %7D%0A    %7D%0A    %0A    private void connectBtn_click()%0A    %7B%0A        // Only connect if we have no connection%0A        if (socketClosed) %7B%0A            clientSocket = new GCClientSocket(new ClientSocketListener(),%0A                                              GCClientSocket.DEFAULT_HOST,%0A                                              GCClientSocket.DEFAULT_PORT);%0A            clientSocket.connect();%0A        %7D%0A    %7D%0A    %0A    // This private class will be our listener for our client socket. It will%0A    // listen for messages sent from the server and for the connection status of%0A    // the socket.
 %0A   
@@ -3934,24 +3934,70 @@
 tener%0A    %7B%0A
+        // If we've received a server message%0A
         @Ove
@@ -4077,27 +4077,105 @@
     
-%0A        %7D%0A        
+msgArea.appendText(%22SERVER: %22 + line);%0A        %7D%0A        %0A        // Update our connection status
 %0A   
@@ -4261,32 +4261,56 @@
   %7B%0A            
+socketClosed = isClosed;
 %0A        %7D%0A    %7D

Implement GCClient event handlers