import java.applet.Applet;
import java.awt.event.*;
import java.awt.*;
import java.io.*;
import java.util.*;
import java.net.*;


public class GuestBook extends Applet implements ActionListener{
	private static Vector entries;
	private Panel ppic;
	private static Image img;
	private TextField name,email,hp;
	private TextArea comment;
	private Button send;

	//create the static vector list object
	static{
		entries = new Vector();
	}

	GuestBook(){
		super();
		//first load all entries out of the StorageServlet
		loadEntries();
	}

	//create the graphical user interface GUI
	public void init(){
		img = getImage(this.getDocumentBase(),"guestbook.jpg");

		ppic = new Panel(){
      		public void paint(Graphics g) {
        		if (img != null) g.drawImage(img, 0 ,0, this);
      		}

      		public Dimension getPreferredSize(){
				return new Dimension(250,100);
			}
    	};

		try{
			this.setLayout(new BorderLayout());
			Label l = new Label("Internet GuestBook");
			l.setFont(new Font("Courier",Font.BOLD,25));
			this.add(l,BorderLayout.NORTH);

			//add picture canvas
			Panel inPane = new Panel(new FlowLayout());
			inPane.add(ppic);
			//add input panel
			inPane.add(new Label("Name:"));
			name = new TextField(40);
			inPane.add(name);
			inPane.add(new Label("e-mail:"));
			email = new TextField(40);
			inPane.add(email);
			inPane.add(new Label("Homepage:"));
			hp = new TextField(40);
			inPane.add(hp);
			inPane.add(new Label("Comment:"));
			comment = new TextArea(5,40);
			inPane.add(comment);
			send = new Button("Send entry");
			inPane.add(send);
			send.addActionListener(this);
			this.add(inPane,BorderLayout.CENTER);
			ppic.setBackground(Color.white);
		}catch(Exception e){
			e.printStackTrace();
		}
	}

	//eventhandler for button click
	public void actionPerformed(ActionEvent ev){
		if(checkInput()) storeEntries();
		else System.out.println("Input not ok");
	}

	//check the user input fields
	private boolean checkInput(){
		if((email.getText().indexOf('@')==-1)
		   ||(comment.getText().length()<1 ))
		   		return false;
		else
			return true;
	}

	//load all entries from StorageServlet
	private void loadEntries(){
		String s = null;
		try {
			URL server = new URL("http://127.0.0.1:8080/examples/servlet/StorageServlet");
		    InputStream in = server.openStream();
		    byte[] buf = new byte[3000];
		    int i = 0;
		    int ch = in.read();
		    while (ch >= 0) {buf[i++] = (byte)ch; ch = in.read();}
		    s = new String(buf, 0, i);
		} catch (Exception ex){
			ex.printStackTrace();
		}
		StringTokenizer tokenizer = new StringTokenizer(s, "|");
		int count = tokenizer.countTokens();

		while(count>4){
			String name = tokenizer.nextToken();
			String datum = tokenizer.nextToken();
			String email = tokenizer.nextToken();
			String hp = tokenizer.nextToken();
			String text = tokenizer.nextToken();
			Msg m = new Msg(name,email,hp,text);
			System.out.println(m);
	        entries.addElement(m);
			count-=5;
		}
	}

	//store the data from the user input fields with
	//just opening an URL with parameters
	private void storeEntries(){
		try{
			System.out.println("store one entry");
			URL server = new URL("http://127.0.0.1:8080/examples/servlet/StorageServlet?name="+name.getText()+"&email="+email.getText()+"&homepage="+hp.getText()+"&text="+comment.getText());
			server.openStream();
		}catch(Exception e){
			e.printStackTrace();
		}
	}

	public void start(){}

	public void stop(){}

	public void paint(Graphics g) {
		if(ppic.prepareImage(img, this)){
			ppic.repaint();
		}
	}

	public void destroy(){}

	public boolean imageUpdate(Image img,int infoflags,int x,int y,int width,int height){
		boolean ready = ((infoflags & (ERROR | ALLBITS)) != 0);
		if (ready){
			System.out.println("Image Observer : READY");
			ppic.repaint();
			return ready;
		}
		return !ready;
	}

}


