import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.URL;
import java.net.MalformedURLException;
import java.applet.Applet;
import java.applet.AudioClip;

public class TankSimulator extends JPanel implements ActionListener {
	/*@non_null@*/ GridDisplay display;
	/*@non_null@*/ SimObjectChooser simObjChooser;
	/*@non_null@*/ UserTank ut;
	/*@non_null@*/ private JButton startButton, stopButton;
	Status status;
	ItemStatus is;
	String stageName = "";

	private int width;
	private int height;
	
	
	final static String soundweb = "http://www.cs.virginia.edu/cs201j/problem-sets/ps5-submit/tankhunt/sounds/";
	
	static URL codebase;
	static URL base2;
	static URL base3;
	static URL base4;
	static URL base5;
	static URL base6;
	static AudioClip gun;
	static AudioClip death;
	static AudioClip swallow;
	static AudioClip multi;
	static AudioClip demolish;
	static AudioClip magneto;
	static AudioClip bgmusic;
	

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

		/**********MODIFIED**********/
		try {
			codebase =
				new URL(soundweb+"gun.wav");
			base2 =
				new URL(soundweb+"explosion.au");
			base3 =
				new URL(soundweb+"swallow.wav");
			base4 =
				new URL(soundweb+"multi.wav");
			base5 =
				new URL(soundweb+"demolish.wav");
			base6 =
				new URL(soundweb+"magneto.wav");
		} catch (MalformedURLException e) {
			System.err.println(e.getMessage());
		}
		gun = Applet.newAudioClip(codebase);
		death = Applet.newAudioClip(base2);
		swallow = Applet.newAudioClip(base3);
		multi = Applet.newAudioClip(base4);
		demolish = Applet.newAudioClip(base5);
		magneto = Applet.newAudioClip(base6);
		/*********************************/
		
		setLayout (new BoxLayout (this, BoxLayout.Y_AXIS));

		simObjChooser = new SimObjectChooser ();
		ut = new UserTank();
		status = ut.getStatus();
		is = ut.getItemStatus();
		
		
		display = new GridDisplay (grid, simObjChooser);

		stageLoader(display,br);

	
		display.setObjectAt(0,7,ut);
		display.setPreferredSize (new Dimension (width, height));
		display.setMinimumSize (new Dimension (width, height));
		display.addKeyListener(ut);
		add (display);
        

		JPanel panel1 = new JPanel();

		startButton = new JButton ("Start");
		// 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 ("Stop");
		// 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);

		panel1.add(status);
		panel1.add(is);

		add (panel1);
	}

	public void addClass (String s) {
		try {
			simObjChooser.addClass (s);
		} catch (ClassNotFoundException e) {
			System.err.println ("Error loading simulated object class " + s + ": " + e);
		}
	}
        
	public void actionPerformed (ActionEvent e) 
	{
	//@assume e != null;
	String command = e.getActionCommand ();

	if (command != null) {
		if (command.equals ("start")) {
		startButton.setEnabled (false);
		stopButton.setEnabled (true);
		display.startObjects();
		} else if (command.equals ("stop")) { 
		startButton.setEnabled (true);
		stopButton.setEnabled (false);
		display.pauseObjects();
		}
	} else {
		System.err.println ("Null command: " + e);
	}
	display.requestFocus();
	}

	public void stageLoader(GridDisplay gd, BufferedReader br){
		try{
			String line;
			String tmp[] = new String[4];
			line = br.readLine();
			while(line!=null){
				tmp = line.split(" ");
				if(tmp[0].equals("Stage")){
					stageName = " Stage " + tmp[1] + " : " + tmp[2].replace('_',' ');
				}else if(tmp[0].equals("NumberEnemies")){
					ut.setNumEnemies(Integer.decode(tmp[1]).intValue());
				}else if(tmp[3].equals("-")){
					Class newClass = Class.forName(tmp[0]);
					display.setObjectAt(Integer.decode(tmp[1]).intValue(),
									Integer.decode(tmp[2]).intValue(),
									(SimObject)newClass.newInstance());
				}else if(!tmp[3].equals("-")){
					//put Item on
					Class newClass = Class.forName(tmp[3]);
					Item newit = (Item)newClass.newInstance();
					display.setObjectAt(Integer.decode(tmp[1]).intValue(),
									Integer.decode(tmp[2]).intValue(),
									newit);
					//remove Item
					newit.getGrid().removeObjectAt(newit.getX(),newit.getY());
					//put Tank on and let it take the Item
					newClass = Class.forName(tmp[0]);
					Tank newtank = (Tank)newClass.newInstance();
					newtank.setItem(newit,0);
					display.setObjectAt(Integer.decode(tmp[1]).intValue(),
									Integer.decode(tmp[2]).intValue(),
									newtank);
					
				}
				line = br.readLine();
			}
		}catch(FileNotFoundException fnfe){
			System.out.println(fnfe.toString());
		}catch(IOException ioe){
			System.out.println(ioe.toString());
		}catch(ClassNotFoundException cnfe){
			System.out.println(cnfe.toString());		
		}catch(InstantiationException ie){
			System.out.println(ie.toString());
		}catch(IllegalAccessException iae){
			System.out.println(iae.toString());
		}
	}
}