package com.github.noxan.aves.demo.chat;

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.UIManager;

import layout.TableLayout;

public class ChatClientGUI {
    private JFrame frame;
    private JPanel cards;

    public ChatClientGUI(ChatClient client) {
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch(Exception e) {
        }

        frame = new JFrame();
        frame.setTitle("aves - chatclient");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        cards = new JPanel(new CardLayout());
        cards.add(new ConnectPanel(client), "connect");
        cards.add(new LoginPanel(client), "login");

        frame.add(cards, BorderLayout.CENTER);

        frame.setSize(320, 680);
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    private class ConnectPanel extends JPanel {
        private static final long serialVersionUID = -8154588851543816456L;

        private ChatClient client;

        private JButton connectButton;

        public ConnectPanel(ChatClient client) {
            this.client = client;
            setLayout(new TableLayout(new double[][] { { TableLayout.FILL, 0.5f, TableLayout.FILL }, { TableLayout.FILL, TableLayout.PREFERRED, 5, TableLayout.PREFERRED, TableLayout.FILL } }));
            initComponents();
        }

        private void initComponents() {
            JLabel offlineLabel = new JLabel("Server currently offline.");
            offlineLabel.setHorizontalAlignment(SwingConstants.CENTER);
            add(offlineLabel, "1,1");

            connectButton = new JButton("Try reconnect");
            connectButton.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    client.connect();
                }
            });
            add(connectButton, "1,3");
        }
    }

    private class LoginPanel extends JPanel implements ActionListener {
        private static final long serialVersionUID = 4260715489603841434L;

        private ChatClient client;

        private JTextField usernameField;
        private JPasswordField passwordField;
        private JButton loginButton;

        public LoginPanel(ChatClient client) {
            this.client = client;
            setLayout(new TableLayout(new double[][] { { TableLayout.FILL, 0.5f, TableLayout.FILL },
                    { TableLayout.FILL, TableLayout.PREFERRED, 5, TableLayout.PREFERRED, 5, TableLayout.PREFERRED, 5, TableLayout.PREFERRED, 5, TableLayout.PREFERRED, TableLayout.FILL } }));

            initComponents();
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            client.login(usernameField.getText(), new String(passwordField.getPassword()));
        }

        private void initComponents() {
            JLabel usernameLabel = new JLabel("Username");
            usernameLabel.setHorizontalAlignment(SwingConstants.CENTER);
            add(usernameLabel, "1,1");

            usernameField = new JTextField();
            add(usernameField, "1,3");

            JLabel passwordLabel = new JLabel("Password");
            passwordLabel.setHorizontalAlignment(SwingConstants.CENTER);
            add(passwordLabel, "1,5");

            passwordField = new JPasswordField();
            add(passwordField, "1,7");

            loginButton = new JButton("Login");
            loginButton.addActionListener(this);
            add(loginButton, "1,9");
        }
    }
}

@@ -165,16 +165,82 @@
 istener;
+%0Aimport java.awt.event.KeyAdapter;%0Aimport java.awt.event.KeyEvent;
 %0A%0Aimport
@@ -373,24 +373,86 @@
 swordField;%0A
+import javax.swing.JScrollPane;%0Aimport javax.swing.JTextArea;%0A
 import javax
@@ -644,16 +644,49 @@
 l cards;
+%0A    private ChatPanel chatPanel;
 %0A%0A    pu
@@ -1094,32 +1094,32 @@
 t), %22connect%22);%0A
-
         cards.ad
@@ -1153,16 +1153,97 @@
 login%22);
+%0A        chatPanel = new ChatPanel(client);%0A        cards.add(chatPanel, %22chat%22);
 %0A%0A      
@@ -4092,11 +4092,1307 @@
  %7D%0A    %7D
+%0A%0A    private class ChatPanel extends JPanel %7B%0A        private static final long serialVersionUID = -848472248464283937L;%0A%0A        private ChatClient client;%0A%0A        private JTextArea chatTextArea;%0A        private JTextField inputField;%0A%0A        public ChatPanel(ChatClient client) %7B%0A            this.client = client;%0A            setLayout(new TableLayout(new double%5B%5D%5B%5D %7B %7B 5, TableLayout.FILL, 5 %7D, %7B 5, TableLayout.FILL, 5, TableLayout.PREFERRED, 5 %7D %7D));%0A            initComponents();%0A        %7D%0A%0A        public void addChatText(String text) %7B%0A            chatTextArea.append(text + System.getProperty(%22line.separator%22));%0A        %7D%0A%0A        public void initComponents() %7B%0A            chatTextArea = new JTextArea(%22aves - chatclient%22);%0A            chatTextArea.setEditable(false);%0A            add(new JScrollPane(chatTextArea), %221,1%22);%0A%0A            inputField = new JTextField();%0A            inputField.addKeyListener(new KeyAdapter() %7B%0A                @Override%0A                public void keyReleased(KeyEvent e) %7B%0A                    if(e.getKeyCode() == KeyEvent.VK_ENTER) %7B%0A                        client.chat(inputField.getText());%0A                        inputField.setText(%22%22);%0A                    %7D%0A                %7D%0A            %7D);%0A            add(inputField, %221,3%22);%0A        %7D%0A    %7D
 %0A%7D%0A

implement chat panel and add it to chat gui