package ns.tb.main;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.EOFException;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.ScrollPaneConstants;
import javax.swing.SwingUtilities;

public class Main extends JFrame {
private static final long serialVersionUID = 1L;
public static final int WIDTH = 500, HEIGHT = 500;
public static final String VERSION = "Pre Alpha";
private JPanel panel;
private JTextField ip, outgoing;
private JTextArea incoming;
private ObjectOutputStream output;
private ObjectInputStream input;
private ServerSocket server;
private Socket connection;
private String ipString;

public Main() {
setTitle("Ye Ol' Chat Program - " + VERSION);
panel = new JPanel();
setSize(new Dimension(WIDTH, HEIGHT));
setResizable(false);
setLocationRelativeTo(null);
JLabel ipL = new JLabel("Enter the IP here");
ip = new JTextField(25);
JButton host = new JButton("Host a Chat Room");
host.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// ipString = ip.getText();
setVisible(false);
chatMenu();
setVisible(true);
startRunningHost();
}
});
JButton join = new JButton("Join a Chat Room");
join.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
chatMenu();
}
});
panel.add(ipL);
panel.add(ip);
panel.add(host);
panel.add(join);
getContentPane().add(BorderLayout.CENTER, panel);
setVisible(true);
}

public void chatMenu() {
panel.removeAll();
incoming = new JTextArea(26, 40);
incoming.setLineWrap(true);
incoming.setWrapStyleWord(true);
incoming.setEditable(false);
JScrollPane incomingSP = new JScrollPane(incoming);
incomingSP.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
incomingSP.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
outgoing = new JTextField(30);
JButton send = new JButton("Send");
panel.add(incomingSP);
panel.add(outgoing);
panel.add(send);
}

public void startRunningHost() {
try {
server = new ServerSocket(57671, 10);
while (true) {
try {
waitForConnection();
setUpStreams();
whileChatting();
} catch (EOFException eof) {
showMessage("\nServer ended connection!");
} finally {
closeStreams();
}
}
} catch (IOException ex) {
ex.printStackTrace();
}
}

private void waitForConnection() throws IOException {
showMessage("Waiting for someone to connect...\n");
connection = server.accept();
showMessage("Now connected to " + connection.getInetAddress().getHostName());

}

private void setUpStreams() throws IOException {
output = new ObjectOutputStream(connection.getOutputStream());
output.flush();
input = new ObjectInputStream(connection.getInputStream());
showMessage("Streams are set up!");
}

private void whileChatting() throws IOException {
String message = " You are now connected!";
sendMessage(message);
ableToType(true);
do {
try {
message = (String) input.readObject();
showMessage("\n" + message);
} catch (ClassNotFoundException cnfe) {
showMessage("\nInvalid object.");
}
} while (!message.equals("END!@#"));
}

private void closeStreams() {
showMessage("\nClosing streams...");
ableToType(false);
try {
output.close();
input.close();
connection.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}

private void sendMessage(String message) {
try {
output.writeObject("Host: " + message);
output.flush();
showMessage("\nHost: " + message);
} catch (IOException ex) {
incoming.append("\nCant send that.");
}
}

private void showMessage(final String text) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
incoming.append(text);
}
});
}

private void ableToType(final Boolean tof) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
outgoing.setEditable(tof);
}
});
}
}