import java.awt.*;
import java.awt.event.*;

public class LabyrinthView extends Frame {

    // model
    private Labyrinth labyrinth;

    // view
    private TextArea output;
    private Label status;

    private Button bClear;

    private Button bSet;
    private Button bSolve;

    private TextField tfSSize;

    private static String newline = System.getProperty("line.separator");
    
    public LabyrinthView(Labyrinth model) {

	super("Labyrinth");
	this.labyrinth = model;

	//  ------------------------------------------------------

	addWindowListener (new WindowAdapter () {
		public void windowClosing (WindowEvent e) {
		    quit ();
		}
	    });

	setLayout(new BorderLayout ());

	//  ------------------------------------------------------ 

	output = new TextArea("", labyrinth.MAX_ROW, labyrinth.MAX_COL,TextArea.SCROLLBARS_NONE);
	output.setFont(new Font("Monospaced", Font.BOLD, 18));
	add ("Center", output);

	//  ------------------------------------------------------

	Panel panel1 = new Panel(new GridLayout(1,2));

	//  ------------------------------------------------------

	bClear = new Button("Clear");
	bClear.addActionListener(new ActionListener() {
		public void actionPerformed(ActionEvent e) {
		    clear();
		}
	    });
	panel1.add(bClear);

	//  ------------------------------------------------------

	bSet = new Button("Set");
	bSet.addActionListener(new ActionListener() {
		public void actionPerformed(ActionEvent e) {
		    set();
		}
	    });
	panel1.add(bSet);

	//  ------------------------------------------------------

	bSolve = new Button("Solve");
	bSolve.setEnabled(false);
	bSolve.addActionListener(new ActionListener () {
		public void actionPerformed (ActionEvent e) {
		    solve();
		}
	    });
	panel1.add(bSolve);

	//  ------------------------------------------------------

	Panel bottom = new Panel(new GridLayout(3,1));
	bottom.add(panel1);

	//  ------------------------------------------------------

	Panel panel2 = new Panel(new GridLayout(1,2));

	panel2.add(new Label("Max solution size:"));
	tfSSize = new TextField("30");
	panel2.add(tfSSize);

	bottom.add(panel2);

	//  ------------------------------------------------------

	status = new Label("paste-in a maze and press Set");
	bottom.add(status);

	//  ------------------------------------------------------

	add ("South", bottom);

	pack();
	setVisible (true);
    }

    private void clear()  {
	output.setText("");
	status.setText("paste-in a maze and press Set");
	bSolve.setEnabled(false);
    }

    private void set()  {

	try {
	    labyrinth.set(output.getText());
	    status.setText("press Solve");
	    bSolve.setEnabled(true);
	} catch (LabyrinthFormatException e) {
	    status.setText("not a valid maze: " + e.getMessage());
	    bSolve.setEnabled(false);
	}
    }

    private void solve() {
	int solutionSize = Integer.parseInt(tfSSize.getText());

	status.setText("");
	if (labyrinth.solve(solutionSize))
	    output.setText(labyrinth.get());
	else
	    status.setText("unsolvable, edit maze, press Set");
    }

    public void quit() {
	System.exit (0);
    }

    public static void main(String[] args) {
	Labyrinth model = new Labyrinth();
	Frame view = new LabyrinthView(model);
    }
}
