import java.applet.Applet;

import java.awt.*;

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

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

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

public class Example9Applet extends Applet implements Observer {
    private TextArea _textArea = new TextArea();
    private Button _callCGIButton = new Button("Call CGI Script");
    private TextField _notifyInterval = new TextField("-1");

    private String _cgiURL = null;
    private boolean _wasObserverCalled = false;

    public void init() {

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

        Panel p = new Panel();
        p.setLayout(new BorderLayout());
        p.add("West", new Label("Enter HTML Notify Interval:"));
        p.add("Center", _notifyInterval);
        add("North", p);
        add("Center", _textArea);
        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);
    }

    public void update(Observable o, Object arg) {
        JavaCGIBridgeNotify jcbn = (JavaCGIBridgeNotify)arg;
        if (jcbn.isTimedOut()) {
            System.out.println("A JavaCGIBridgeTimeOutException occurred!");
        } else {
            byte [] b = jcbn.getRawData();
            if (b == null) {
                System.out.println("Nothing was returned, something is wrong");
            } else if (!_wasObserverCalled) {
                _textArea.setText(new String(b,0));
            }
            _wasObserverCalled = true;
        }
    } // end of update -- used in observer interface
 
    private void clicked_cgiButton() {
        JavaCGIBridge jcb = new JavaCGIBridge();
        int notifyInterval = Integer.parseInt(_notifyInterval.getText());
        jcb.setRawNotifyInterval(notifyInterval);
        jcb.addObserver(this);
        URL u = null;

        try {
            if (_cgiURL.startsWith("http")) {
                 u = new URL(_cgiURL); 
            } else {
                 u = new URL(getDocumentBase(), _cgiURL);
            }

            _wasObserverCalled = false;
            jcb.callOneWay(u);

        // Note, since this is an asynchronous call to the
        // JavaCGIBridge, we can't catch JavaCGIBridgeTimeOutExceptions
        } catch (MalformedURLException e) {
            System.out.println("Malformed URL Exception:" + 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
