/*
 * Maze
 * Copyright (C) 2000  Paul Davis, pdavis@lpccomp.bc.ca
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

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

/**
 * The Maze Application.
 * Takes from one to three command line arguments.
 * If one argument, it is used as the X and Y dimensions of a 2D maze.
 * If two arguments, they are used as the X and Y dimensions of a 2D maze.
 * If three arguments, they are used as the X,Y and Z dimensions of a 3D maze.
 */
public class MazeApp extends Frame {

	private static MazeApp app;
	private static Maze3D disp;
	private static Coordinate3D size;
	private static Maze m;

	public static void main(String[] args) {
		int x=10;
		int y=10;
		int z=1;
		try {
			if ( args.length > 0 )
				x = y = Integer.parseInt(args[0]);
			if ( args.length > 1 )
				y = Integer.parseInt(args[1]);
			if ( args.length > 2 )
				z = Integer.parseInt(args[2]);
		}
		catch ( NumberFormatException e ) {
			System.out.println("Usage: MazeApp xsize [ysize [zsize]]");
			System.exit(1);
		}

		size = new Coordinate3D(x,y,z);
		m = new Maze(size);
		m.setRandomFinish();
		disp = new Maze3D(m,400,400);
		app = new MazeApp();

		app.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				System.exit(0);
			}
		});

		app.add(disp,BorderLayout.NORTH);

		Button restartButton = new Button("New Maze");
		restartButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				app.remove(disp);
				Maze m2 = new Maze(size);
				m2.setRandomFinish();
				disp = new Maze3D(m2,400,400);
				app.add(disp,BorderLayout.NORTH);
				app.validate();
				app.repaint();
			}
		});
		Button exitButton = new Button("Exit");
		exitButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				System.exit(1);
			}
		});

		Panel buttonPanel = new Panel();
		buttonPanel.add(restartButton);
		buttonPanel.add(exitButton);

		app.add(buttonPanel,BorderLayout.SOUTH);
		app.pack();
		app.show();
	}

}