import java.awt.*;
import java.awt.event.*;

/**
 * Yagol is the main class of YAGoL, controlling the GUI.
 *
 * <p>
 * Made by Joakim "firetech" Andersson in June 2007.<br>
 * <i>Free to use, abuse and/or modify for non-commercial purposes as long as I'm
 * credited.</i>
 * </p>
 */
public class Yagol extends Frame {
	/** The "Play" button */
	private Button play;
	/** The "Next Generation" button */
	private Button next;
	/** The Generation counter label */
	private Label generations;
	/** The rules field */
	private TextField rules;
	/** The "Clear" button */
	private Button clear;
	/** The "Zoom In" button */
	private Button zoomIn;
	/** The "Zoom Out" button */
	private Button zoomOut;
	/** The canvas on which it all takes place*/
	private GolCanvas canvas;

	/**
	 * Normal static main() method, which just initiates a new, non-static
	 * instance of this class.
	 *
	 * @param args not used.
	 */
	public static void main(String[] args) {
		new Yagol();
	}

	/**
	 * Create the GUI.
	 */
	public Yagol() {
		super("YAGoL - Yet Another Game of Life");
	
		Panel control = new Panel();
		play = new Button("Play/Stop");
		control.add(play);
		next = new Button("Next Generation");
		control.add(next);
		control.add(new Label(""));
		control.add(new Label("Generations:"));
		generations = new Label("0000", Label.RIGHT);
		control.add(generations);
		control.add(new Label(" Rules:"));
		rules = new TextField("23/3",5);
		control.add(rules);
		clear = new Button("Clear");
		control.add(clear);
		control.add(new Label(""));
		zoomIn = new Button("Zoom In");
		control.add(zoomIn);
		zoomOut = new Button("Zoom Out");
		control.add(zoomOut);
		add(control, BorderLayout.NORTH);
		
		canvas = new GolCanvas(800, 600, rules.getText(), generations);
		add(canvas, BorderLayout.CENTER);
		
		addWindowListener(new WindowHandler());

		ControlHandler ctrlHand = new ControlHandler();
		play.addActionListener(ctrlHand);
		next.addActionListener(ctrlHand);
		rules.addKeyListener(ctrlHand);
		rules.addActionListener(ctrlHand);
		clear.addActionListener(ctrlHand);
		zoomIn.addActionListener(ctrlHand);
		zoomOut.addActionListener(ctrlHand);
		
		pack();
		setMinimumSize(getSize());
		
		setVisible(true);
	}
	
	/**
	 * WindowHandler handles the window closing operation.
	 */
	private class WindowHandler extends WindowAdapter {
		/**
		 * Called when the user clicks the close button of the window.
		 */
		public void windowClosing(WindowEvent e) {
			System.exit(0);
		}
	}

	/**
	 * ControlHandler handles the button clicks.
	 */
	private class ControlHandler extends KeyAdapter implements ActionListener {
		/**
		 * Set background of the rules filed to red while changing it.
		 */
		public void keyTyped(KeyEvent e) {
			if(e.getSource() == rules) {
				rules.setBackground(Color.red);
			}
		}

		/**
		 * Handle the button clicks and Enter in the rules field.
		 */
		public void actionPerformed(ActionEvent e) {
			Object b = e.getSource();
			if(b == play) {
				if(canvas.isPlaying())
					canvas.stop();
				else
					canvas.play();
			} else if(b == next) {
				canvas.stop();
				canvas.next();
			} else if(b == rules) {
				try {
					canvas.changeRules(rules.getText());
					rules.setBackground(Color.white);
				} catch(IllegalArgumentException ex) {}
			} else if(b == clear) {
				canvas.clear();
			} else if(b == zoomIn) {
				zoomIn.setEnabled(canvas.zoomIn());
				zoomOut.setEnabled(true);
			} else if(b == zoomOut) {
				zoomOut.setEnabled(canvas.zoomOut());
				zoomIn.setEnabled(true);
			}
		}
	}
}
