/*

ComputersAreUseless - Java applet sentence generator

Copyright (c) 2008, Chris Kroells
All rights reserved.

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

    * Redistributions of source code must retain the above copyright notice,
      this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright notice,
      this list of conditions and the following disclaimer in the documentation
      and/or other materials provided with the distribution.
    * Neither the name of the author nor the names of its contributors may be
      used to endorse or promote products derived from this software without
      specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

*/


import java.awt.*;
import java.applet.*;
import java.util.Random;

public class ComputersAreUseless extends Applet implements Runnable
{
	// Delay between sentences in milliseconds 
	private int delay = 3000;
	// Count between the "combinations" text
	private int countdown = 10;
		
	private String[] subject = {"Did you", "Did he", "Did she", "Did it", "Will you", "Will he",
								"Will it", "Will she", "Would it", "Would he", "Would she", "Would you",
								"Does it", "Does he", "Does she", "Do you", "Can he", "Can she", "Can it"};
	private String[] verb = {"eat", "smell", "drink", "touch", "apply", "stir", "wiggle", "shake",
								"bake", "snip", "slice", "stroke", "stretch", "hug", "lick", "snatch",
								"hear", "see", "encounter", "find", "seek", "search", "handle", "assemble",
								"wear", "call", "represent", "wonder about", "help", "repeat", "control",
								"buy", "sell", "truncate", "trade", "throw", "toss", "draw", "write",
								"punch", "buy", "sell", "steal", "share", "view", "scratch", "return",
								"rescue", "type", "auction", "reach for", "whip", "teach", "question",
								"discover", "invent", "invest in", "entertain", "share", "try", "dream of"};
	private String[] object = {"water", "air", "chocolate", "toe nails", "a rainbow", "a cell phone", "a pencil",
								"a pendulum", "spinach", "a carrot", "a pumpkin", "a kangaroo", "a donkey",
								"life", "the ground", "trees", "a cigarette", "a clock", "time and space",
								"CD's", "DVD's", "anteaters", "an octopus", "asbestos", "coffee", "milk",
								"cookies", "a plumber", "a cat", "dogs", "racoons", "a dolphin", "tea",
								"an engine", "a yardstick", "a tuba", "a soccer ball", "deers", "fruits"};
	private Random r;
	private Label sentence;
	private boolean running = false;
		
	
	public ComputersAreUseless()
	{
		// set random seed to current time
		r = new Random(System.currentTimeMillis());
		sentence = new Label("");
		sentence.setFont(new Font("SansSerif", Font.PLAIN, 14));
		this.add(sentence);
	}

	// initialization method
	public void init()
	{
		if (running == false)
		{
			// makes a new thread and runs it
			Thread thread = new Thread(this);
			thread.start();
		}
	}
	
	public void stop()
	{
		running = false;
	}
	
	// run() method for Runnable
	public void run()
	{
		running = true;
		
		while(running)
		{
			update();
			
			try
			{
				Thread.sleep(delay);
			}
			catch (Exception e)
			{
				e.printStackTrace(System.out);
			}
		}
	}
	
	private String makeSentence()
	{
		String msg = subject[r.nextInt(subject.length)] + " " +
					 verb[r.nextInt(verb.length)] + " " +
					 object[r.nextInt(object.length)] + "?";
		
		return msg;
	}
	
	private void update()
	{
		if (countdown == 0)
		{
			int combinations = subject.length * verb.length * object.length;
			sentence.setText("ComputersAreUseless: " + combinations + " combinations strong!");
			countdown = 10;
		}
		else
			sentence.setText(makeSentence());

		this.validate();
		
		countdown--;
	}
}