import java.applet.Applet;

import java.awt.*;

import java.io.ByteArrayOutputStream;
import java.io.ByteArrayInputStream;
import java.io.StringBufferInputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.IOException;
import java.io.StreamCorruptedException;

import java.net.URL;
import java.net.MalformedURLException;

import java.util.Vector;
import java.util.Enumeration;
import java.util.Hashtable;

import com.extropia.net.JavaCGIBridge;
import com.extropia.net.JavaCGIBridgeExtension;
import com.extropia.net.JavaCGIBridgeTimeOutException;

public class Example5Applet extends Applet {
    private TextArea _cgiResults = new TextArea("Test String From Applet!");
    private Button _callCGIButton = new Button("Call CGI Script");

    private String _cgiURL = null;

    public void init() {

        this.setBackground(Color.white);
        setLayout(new BorderLayout());

        add("North", _cgiResults);
        add("South", _callCGIButton);
       
        getSetupFile();        

    } // end of init method

    public boolean handleEvent(Event e) {
        if (e.id == Event.ACTION_EVENT && e.target == _callCGIButton) {
            clicked_cgiButton();
        }
        return super.handleEvent(e);
    }

    private void clicked_cgiButton() {
        JavaCGIBridgeExtension jcbe = new JavaCGIBridgeExtension();
        byte [] returnedData = null;
        URL u = null;

        ObjectInputStream ois = null;

        try {
            if (_cgiURL.startsWith("http")) {
                 u = new URL(_cgiURL); 
            } else {
                 u = new URL(getDocumentBase(), _cgiURL);
            }
            
            Example5SerialObject so = new Example5SerialObject();
            so.setTestInteger(5);
            so.setTestString(_cgiResults.getText());

            try {
                ObjectOutputStream oos = jcbe.getObjectOutputStream();
                oos.writeObject(so);
            } catch (IOException e) {
                System.out.println("IOException adding Object");
            }
            ois = jcbe.getObjectData(u);

        } catch (MalformedURLException e) {
            System.out.println("Malformed URL Exception:" + e);
        } catch (JavaCGIBridgeTimeOutException e) {
            System.out.println("JavaCGIBridge Timed Out:" + e);
        } catch (StreamCorruptedException e) { 
            System.out.println("Stream Corrupted Getting Object" + e);
        } catch (IOException e) {
            System.out.println("IOException Getting Object" + e);
        }

        try {

            Example5SerialObject so = (Example5SerialObject)ois.readObject();
            _cgiResults.setText(so.getTestString());

            so = (Example5SerialObject)ois.readObject();
            _cgiResults.appendText(so.getTestString());

            _cgiResults.appendText("\n");

            so = (Example5SerialObject)ois.readObject();
            _cgiResults.appendText(so.getTestString());

        } catch (ClassNotFoundException e) {
            System.out.println("Class Not Found Exception Getting Object" + e);
        } catch (StreamCorruptedException e) { 
            System.out.println("Stream Corrupted Getting Object" + e);
        } catch (IOException e) {
            System.out.println("IOException Getting Object" + e);
        }

    }


    private void getSetupFile() {

        JavaCGIBridge jcb = new JavaCGIBridge();
        // Since we are reading a setup file, we just want
        // to make it into a variable=value format instead of the
        // funky ~|~ seperator that is the default. This makes the
        // setup file easier to read at the risk of making sure a 
        // particular variable does not have an equal symbol in its
        // value.
        jcb.setFieldSeparator("=");
        jcb.setRecordSeparator("\n");

        Vector returnedDataSet = null;
        Hashtable keyValuePairs = null;

        try {
            URL u = new URL(getDocumentBase(), "setup.html");
            returnedDataSet = jcb.getParsedData(u);

            if (returnedDataSet == null) {
                System.out.println("returned data set is null for some reason, probably a missing setup file");
            }

            // We call the function below since the vectors that 
            // are returned contain setup information in the form of
            // the first field being a key (variable) and the second
            // field being a value to the key

            keyValuePairs = jcb.getKeyValuePairs(returnedDataSet);

        } catch (MalformedURLException e) {
            System.out.println("Malformed URL Exception:" + e);
        } catch (JavaCGIBridgeTimeOutException e) {
            System.out.println("JavaCGIBridge Timed Out:" + e);
        }
 
        _cgiURL = (String)keyValuePairs.get("cgiURL");

    } // end of getSetupFile

} // end of ExampleApplet
