package rpn.applets;

import java.applet.*;
import java.awt.*;
import java.io.*;
import java.net.*;
import rpn.*;
import rpn.vc.VCInterpreter;
import rpn.vc.VCInstaller;

/** This can serve for extension to be used as exercise applets. 
	This replaces the old vc applet, 
	setting up a calculator with load/save capabilities. */

public class submittorApplet extends Applet {

	public CalculatorPanel vc = null;
	public String initString;
	public String title = null;
	
	public void init() {
		int i = 0;
		setLayout(new BorderLayout(0, 0));
		int r = 10;
		String rows = getParameter("rows");
		if (rows != null) {
			r = Integer.parseInt(rows);
		}
		
		URL CB = getCodeBase();
		initString = new String("");
		String iF = getParameter("inputFile");
		System.out.println("input file = " + iF);
		URL inf = null;
		StringBuffer sb = new StringBuffer("");
		if (iF != null) {
			try {
				inf = new URL(CB, iF);
				System.out.println("input = " + inf);
			} catch (MalformedURLException e) {
				System.out.println(e);				
			}
			InputStream is = null;
			try {
				is = inf.openStream();
			} catch(IOException e) { 
				System.out.println("IO: unable to open " + inf);
			}
			if (is != null) try {
				int c;
				while ((c = is.read()) != -1) {
					if (c == '\n') System.out.print('.');
					sb.append((char) c);
				}
			} catch(IOException e) { 
				System.out.println("IO: unable to read " + inf);
				// System.exit(0); 
			} catch(Exception e) {
				System.out.println(e);					
				System.out.println("Security: unable to open " + inf);
			}
		}
		initString += new String(sb);
		// System.out.println("<" + initString + ">");
		String m = "initialString";
		String x = getParameter(m);
		int k = 1;
		while (x != null) {
			initString += x;
			initString += "\n";
			m = "initialString" + (k++);
			x = getParameter(m); 
		}
		System.out.println("Initial string = " + initString);
		
		title = getParameter("title");
		if (title == null) title = "VC";
		String lab = getParameter("lab");
		if (lab == null) {
			System.out.println("No lab specified!");
			System.exit(1);
		}
		String exercise = getParameter("exercise");
		if (exercise == null) {
			System.out.println("No exercise specified!");
			System.exit(1);
		}
		String p = getParameter("port");
		if (p == null) {
			System.out.println("No port specified!");
			System.exit(1);
		}
		int port = Integer.parseInt(p);
		String name = getParameter("name");
		if (name == null) {
			System.out.println("No name specified!");
			System.exit(1);
		}
		vc = new SubmittorPanel(r, initString, title, 
			(ItemInterpreter) new VCInterpreter(), 
			(ItemInstaller) new VCInstaller(), CB, port,
			lab, exercise, name);
		add("Center", vc);
		show();
		// System.out.println("initializing " + title);
	}
	
	int count = 0;
	public void start() {
		vc.parentStart(initString);
		System.out.println("starting " + title); 
	}
	
	public void stop() {
		initString = vc.parentStop();
		System.out.println("stopping " + title);
	}
	
	public void destroy() {
		initString = vc.parentDestroy();
		System.out.println("killing " + title);
	}
	
	String directory(String path) {
		int n = path.length();
		while (n > 0 && path.charAt(n-1) != '/') {
			n--;
		}
		StringBuffer sb = new StringBuffer();
		for (int k=0;k<n;k++) {
			sb.append(path.charAt(k));
		}
		return(new String(sb));
	}
}
