import javax.swing.JFrame;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Vector;
import java.util.Random;


public class Simulator extends JPanel implements ActionListener {
    /*@non_null@*/ GridDisplay display;
    /*@non_null@*/ private JButton startButton, stopButton;
			 private SimObjectChooser simObjChooser;
                         private JLabel label1, label2;
    private int width;
    private int height;
    public int score = 0;

	static public Random random = new Random();

    //@requires width > 0
    //@requires height > 0
      public Simulator (int width, int height, /*@non_null@*/ Grid grid) {
	this.width = width;
	this.height = height;

        setLayout (new BoxLayout (this, BoxLayout.Y_AXIS));
	  simObjChooser = new SimObjectChooser();
	  display = new GridDisplay (grid, simObjChooser);
        display.setPreferredSize (new Dimension (width, height));
        display.setMinimumSize (new Dimension (width, height));
	
		/*****NEW CODE*****/
		//Adds MouseHandlers to the current GridDisplay	 
	  MouseMotionHandler MX = new MouseMotionHandler(display);
	  display.addMouseMotionListener (MX);
	  MouseClickHandler MC = new MouseClickHandler(display);
	  display.addMouseListener(MC);

        
	  add (display);

        JPanel panel1 = new JPanel();

        startButton = new JButton ("Play");
        // place button at center, left position
        startButton.setVerticalTextPosition (AbstractButton.CENTER);
        startButton.setHorizontalTextPosition (AbstractButton.LEFT);
        startButton.setMnemonic (KeyEvent.VK_D);
        startButton.setActionCommand ("start");


        stopButton = new JButton ("Pause");
        // Use the default button position of CENTER, RIGHT.
        stopButton.setMnemonic (KeyEvent.VK_E);
        stopButton.setActionCommand ("stop");
        stopButton.setEnabled (false);


        // Listen for actions on the Start and Stop buttons.
        startButton.addActionListener (this);
        //stopButton.addActionListener (this);

        // Add the start and stop buttons, and the table, to the
        // container, using the default layout.

        panel1.add (startButton);
        /*panel1.add (stopButton);*/ //I can't make the game stop....

		/*****NEW CODE*****/
		//Starts Labels for Score and Level
        label1 = new JLabel("Score: " + score);
        panel1.add(label1);
        label2 = new JLabel("  Level: " + 1);
        panel1.add(label2);

        add (panel1);
    }

    public void addClass (String s) {
	try {
	    simObjChooser.addClass (s);
	} catch (ClassNotFoundException e) {
	    System.err.println ("Error loading simulated object class " + s + ": " + e);
	}
    }

		/*****NEW CODE*****/
	public void initialize() {
	//EFFECTS: Sets Starting Condition for Game
		for(int i=1; i<=5; i++) {
			display.giveGrid().setObjectAt(random.nextInt(3), random.nextInt(50), new Astro());
		}
	}

		/*****NEW CODE*****/
       public void setScore(int newscore) 
	 //EFFECTS: Sets Score Label to updated score
	 {
            score = newscore;
            label1.setText("Score: " + score);
       }
		/*****NEW CODE*****/
       public void setLevelTo(int level) 
	//EFFECTS: Sets Level Label to updated level
	{
            label2.setText("Level: " + level);
      }
		/*****CHANGED CODE*****/
    public void actionPerformed (ActionEvent e)
    {
	//@assume e != null;
	String command = e.getActionCommand ();

	if (command != null) {
	    if (command.equals ("start")) 
	    //Initializes Game
	    {
		startButton.setEnabled (false);
		stopButton.setEnabled (true);
		initialize();
		display.startObjects();
            display.giveGrid().setSim(this);
	    } /*else if (command.equals ("stop")) {
		startButton.setEnabled (true);
		stopButton.setEnabled (false);
		display.pauseObjects();
		display.setVisible(false);
	    }*/
	} else {
	    System.err.println ("Null command: " + e);
	}
    }
}