//questo applet contatta il server ServerGilotalk lanciato sul webserver
//in ascolto alla porta 5528 e apre la connessione per parlare con Gilo

import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import java.util.*;

public class Gilotalk extends Applet implements ActionListener {
	TextArea area;
	TextField field;
	String nl;
    
	Socket outSock;
	OutputStream outStream;
	PrintWriter ps;
	InputStream inStream;
	BufferedReader cis;

	Font font;
	int i;
	boolean done;
	String guest;

	public void init() {
		font = new Font("Helvetica",Font.BOLD,14);
		setFont(font);
		field = new TextField(10);
		area = new TextArea(10,25);
		area.setBackground(Color.cyan);
		area.setEditable(false);
		GridBagLayout gridBag = new GridBagLayout();
		setLayout(gridBag);
		GridBagConstraints c = new GridBagConstraints();
		c.gridwidth = GridBagConstraints.REMAINDER;

		c.fill = GridBagConstraints.HORIZONTAL;
		gridBag.setConstraints(field, c);
		add(field);
	
		c.fill = GridBagConstraints.BOTH;
		c.weightx = 1.0;
		c.weighty = 1.0;
		gridBag.setConstraints(area, c);

		add(area);
	
		field.addActionListener(this);
		nl = System.getProperty("line.separator");
	}//end init

	public void start() {
//		String host = "sviluppo1.atel.it";
		String host = "swpg24";
		int port = 5528;
		field.setEnabled(true);
		i = -1;
		done = false;

		try {
			//create the socket
			area.append("Trying connection ....." + nl);
			try {
				outSock = new Socket(host,port);
			} catch (Exception e0) {
				System.out.println("Exception:\n" + e0.toString());
				area.append("Server Program is not running." + nl + nl +
					"Please, SEND ME A MAIL if you wanna talk with me:"
					+ nl + "ggilardoni@atel.it" + nl);
				field.setEnabled(false);
			}
			if(outSock != null)
			{
				area.append("Socket created to " + host + nl);

				//set up the streams
				outStream = outSock.getOutputStream();
				ps = new PrintWriter(outStream,true);

				inStream = outSock.getInputStream();
				cis = new BufferedReader(new InputStreamReader(inStream));

				//ask the guest name
				area.append(nl + "Welcome to Gilotalk, insert your name in the textfield" + nl);
			}
		} catch (Exception e1){
			area.append("Exception: " + e1.toString() + nl + nl);
			area.append("I'm afraid we're having a problem here!" + nl);
		}
	}//end start

	public void actionPerformed(ActionEvent evt) {
		if (!done) {
			i++;	
			String line = field.getText();
			field.setText("waiting a message from gilo");
			field.selectAll();
			if (i==0)
				guest = line;
			try {	
				if (!line.startsWith("exit")) {
					area.append(guest + "> " + line + nl);
					field.selectAll();
					ps.println(line);
					ps.flush();
					String mine = cis.readLine();
					if(mine.length()==0) {
						area.append(nl + "Net error" + nl);
						done=true;
					}
					else {
						area.append("Gilo> " + mine + nl);
						field.setText("");
					}
				}
				else {
					//stop the talk
					ps.println("~Gilo-Stop~");
					ps.flush();
					field.setText("");
					area.append(nl + "Gilotalk is over" + nl);
					done = true;
				}
			} catch (Exception e2) {
				area.append(nl + "Net error: Gilotalk is over" + nl);
				done = true;
			}
		}
		if(done == true) {
			try {
				//close the socket
				outSock.close();
			}catch (Exception e3){}
			field.setEnabled(false);
		}
	}//end actionPerformed

	public void stop() {
		try {
			field.setText("");
			area.setText("");
			ps.println("~Gilo-Stop~");
			ps.flush();

			//close the socket
			outSock.close();
		}catch (Exception e4){}
	}
}