/**
 * JavaLife - Conway's Life Simulation
 * @author Scott Hurring - scott at hurring dot com
 * @version alpha1
 * @license GPL
 * For most recent version, go to this url:
 * http://hurring.com/code/java/javalife/
 */

import javax.swing.SwingUtilities;

/**
 * This class is a big dumb delegator, passing messages 
 * between the different components of the life system.
 * Very little logic should be implemented in here.
 */
public class JavaLife
{
	MainFrame mf;
	LifeField field;
	LifeEngine engine;
	double VERSION = 1.0;
	
	public JavaLife() {
		mf = new MainFrame(this);
		newField(0, 0);
		newEngine();
	}

	/**
	 * Create a new field and tell the frame about it
	 */
	public void newField(int rows, int cols) {
		field = new LifeField(this);
		field.create(rows, cols);
		mf.setField(field.getFieldGUI());
	}
	
	/**
	 * The simulation engine
	 */
	public void newEngine() {
		engine = new LifeEngine(this);
	}
	
	/**
	 * Move forward one generation
	 */
	public void next() {
		engine.next(field);
		updateGenerationText();
	}
	
	/**
	 * Start the simulation
	 */
	public void start() {
		mf.start();
		engine.start();
	}
	
	/**
	 * Stop the simulation 
	 */
	public void stop() {
		mf.stop();
		engine.stop();
	}

	/**
	 * Reset the field
	 */
	public void reset() {
		mf.stop();
		engine.reset();
		field.reset();
		updateGenerationText();
	}
	
	/**
	 * Resize the field
	 */
	public void resize(int rows, int cols) {
		newField(rows, cols);
	}
	
	/**
	 * Update the frame's "generation" display field
	 */
	public void updateGenerationText() {
		mf.setGeneration(engine.getGeneration());
	}

	public void showAboutDialog() {
		new AboutDialog(this).display();
	}

	public void showResizeDialog() {
		new ResizeDialog(this).display();
	}

	public void performExitAction() {
		System.exit(0);
	}
	
	public void debug(String string) {
		mf.text.append(string +"\n");
	}
	
	public void debugClear() {
		mf.text.setText("");
	}
	
	public static void main(String[] args)
	{
		try {
			//UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		}
		catch (Exception e) { }
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				new JavaLife();
			}
		});
	}
}
