/**CorpseApplet.java*/

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

public class CorpseApplet extends Applet implements ActionListener{

  // Class vars

  String wordArray[][];
  String outputString = "";
  TextArea corpseTextArea = new TextArea("",10,50,1);
  BorderLayout corpseBorder = new BorderLayout();
  Button refreshButton = new Button("Again, Again");
  Button modeButton = new Button("List Mode");

  // Methods assoc. with interface

  public void init() {

  	Color backColor = new Color(255,255,255);
		setBackground(backColor);
		loadCorpseArray();
    outputString = getCorpseString();
    setLayout(corpseBorder);
    Panel buttonPanel= new Panel();
    FlowLayout corpseFlow = new FlowLayout(FlowLayout.CENTER, 5,5);
    buttonPanel.setLayout(corpseFlow);
    addButton(refreshButton, "anotherCorpse", buttonPanel);
    addButton(modeButton, "switchMode", buttonPanel);
    add(buttonPanel, "South");
    corpseTextArea.setEditable(false);
    corpseTextArea.append(outputString + "\n");
  }

  public void paint(Graphics screen) {
    doLayout();
    if (!corpseTextArea.isShowing()) {int n = 22;
      Font corpseFont = new Font("TimesRoman", 3, n);
      FontMetrics corpseFM = getFontMetrics(corpseFont);
      int outputStringWidth = corpseFM.stringWidth(outputString);
      int appletWidth = this.getSize().width;
      while ( outputStringWidth + 20 > appletWidth ){
        --n;
        corpseFont = new Font("TimesRoman", 3, n);
        corpseFM = getFontMetrics(corpseFont);
        outputStringWidth = corpseFM.stringWidth(outputString);
      }
      screen.setFont(corpseFont);
      int textX = appletWidth / 2 - outputStringWidth /2;
      int textY = getSize().height /2;
      screen.drawString(outputString, textX, textY);
    }
  }

  public void addButton(Button b, String s, Container c) {
    c.add(b);
    b.setActionCommand(s);
    b.addActionListener(this);
  }

  //button event handler
  public void actionPerformed(ActionEvent event){
    String command = event.getActionCommand();
    if (command == "anotherCorpse") {
      outputString = getCorpseString();
      corpseTextArea.append(outputString + "\n");
      repaint();
    }
    if (command == "switchMode") {
      if (corpseTextArea.isShowing()){
        remove(corpseTextArea);
        modeButton.setLabel("List Mode");
      }
      else {
        add(corpseTextArea, "Center");
        modeButton.setLabel("View Mode");
      }
    }
  }

  // Methods assoc. with generating the string

  public int paramCount(){
    int paramCount = 0;
    while (true) {
      String stringtemp = getParameter("param" + paramCount);
      if (stringtemp != null) paramCount++;
      else break;
    }
    return paramCount;
  }

  public int maxWords(){
    int maxWordsCount = 1000;
    String maxWords = getParameter("maxwords");
    if (maxWords != null){
  		Integer maxTemp = Integer.valueOf(maxWords);
		  maxWordsCount = maxTemp.intValue();
		}
  	return maxWordsCount;
  }

  //load files/words into 2-dim array
  public void loadCorpseArray(){
    wordArray = new String[paramCount()][];
		int paramCount = 0;
    int corpseWords = wordArray.length;
  	int maxWords = maxWords();
		while (paramCount < corpseWords) {
      int wCount = 0;
    	String[] stringTemp = new String[maxWords];
      stringTemp[0] = "ERR";
      //open stream to a file
 			try {
	  		String stringURL = getCodeBase().toString();
			  String stringFile = getParameter ("param" + paramCount);
 				URL url = new URL(stringURL + "word" + stringFile);
	  		InputStream corpseStream = url.openStream();
			  BufferedReader bufferStream = new BufferedReader(new InputStreamReader(corpseStream));
	  		//read file words into array
			  while ((( stringTemp[wCount] = bufferStream.readLine())!= null ) && (wCount < maxWords)){
 					if (stringTemp[wCount] != "") {
	  				wCount ++;
			  	}
 				}
	  	}
 			catch(IOException e) {
	  		showStatus("Bad Parameter - Error " + e);
 			}
      if(wCount == 0) wCount = 1;
      wordArray[paramCount] = new String[wCount];
      for(int n=0; n < wCount; n++){
        wordArray[paramCount][n] = stringTemp[n];
      }
			paramCount++;
	  }
  }

  // get corpsestring
  public String getCorpseString(){
		String stringTemp;
		String buildCorpse = "";
    int params = wordArray.length - 1;
  	int paramCount = 0;
  	while (paramCount <= params){

		  // generate random number based on no. of words/elements
  		int n = wordArray[paramCount].length;
		 	double dblWord = Math.random() * n;
      int intWord = (int) Math.floor(dblWord);

      //get word at random line and add to buildcorpse
		 	stringTemp = (String) wordArray[paramCount][intWord];
 			buildCorpse += (stringTemp);
	  	if ( paramCount < params ) buildCorpse += (" ");
	  	paramCount++;
	  }
	  return buildCorpse;
 	}
  
}
