import java.io.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class vcrJFrame extends JFrame implements ActionListener
{
	int loop = 1;
	vcrJPanel mjp;
	Timer tim;
	int moveL = 0;
	int moveR = 0;
	int moveU = 0;
	int moveD = 0;
	obstacle myObstacle;
	track myTrack;
	car myCar;

	int[] inputL = new int[3333];
	int[] inputR = new int[3333];
	int[] inputD = new int[3333];
	int[] inputU = new int[3333];
	
	public vcrJFrame (obstacle inputObstacle, track inputTrack, car inputCar)
	{
		super ("Road Warrior");
		myObstacle = inputObstacle;
		myTrack = inputTrack;
		myCar = inputCar;

		myxmlImport();

		//myObstacle.generateObstacles(myTrack.trackType);

		mjp = new vcrJPanel(myObstacle, myTrack, myCar);
		
		mjp.mjp2.d.addActionListener(this);
		mjp.mjp2.e.addActionListener(this);
		mjp.mjp2.g.addActionListener(this);

		getContentPane().setLayout(new BorderLayout());
		getContentPane().add(mjp,"Center");
	   //------------------------------------------------------
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		setSize (800, 600);
		
		show();
		requestFocusInWindow();
		
		//------------------------------------------------------
		int delay = 33; //milliseconds
		ActionListener scroller = new ActionListener() 
		{
			public void actionPerformed(ActionEvent event) 
			{
				moveL = inputL[mjp.mjp1.step];
				moveR = inputR[mjp.mjp1.step];
				moveD = inputD[mjp.mjp1.step];
				moveU = inputU[mjp.mjp1.step];
				
				System.out.println(mjp.mjp1.step + " " + moveL + moveR + moveD + moveU);
				
				
				int moveLAllow = 1;
				int moveRAllow = 1;
				int moveUAllow = 1;
				int moveDAllow = 1;
				if (mjp.mjp1.carPosX <= 10)
				{ moveLAllow = 0; }
				if (mjp.mjp1.carPosX >= 545) //345
				{ moveRAllow = 0; }
				if (mjp.mjp1.carPosY <= 0)
				{ moveUAllow = 0; }
				if (mjp.mjp1.carPosY >= 475)
				{ moveDAllow = 0; }
				mjp.mjp1.carPosX = mjp.mjp1.carPosX-myCar.carSpeedX*moveL*moveLAllow+myCar.carSpeedX*moveR*moveRAllow;
				mjp.mjp1.carPosY = mjp.mjp1.carPosY+myCar.carSpeedY*moveD*moveDAllow-myCar.carSpeedY*moveU*moveUAllow;
				DoSomething();
			}
		};
		tim = new Timer(delay, scroller);
		//tim.start();	
	}
//--------------------------------------------------------
//-----Action to Perform during Timer to redraw track-----
//--------------------------------------------------------
   	public void DoSomething()
	{
		myTrack.trackPercent = (mjp.mjp1.step*mjp.mjp1.trackSpeed*100)/(10000-10);

		


		//Health Formula
		
		for (int c=0; c < myObstacle.objectsImage.length; c++)
		{
			int dock = 0;

			if (checkIntersection(mjp.mjp1.carPosX, mjp.mjp1.carPosY, myObstacle.objectsPosX[c], myObstacle.objectsPosY[c]+(mjp.mjp1.step*mjp.mjp1.trackSpeed), myObstacle.objectsSizeX[c], myObstacle.objectsSizeY[c]) == 1)
			{
				dock = 1;	
			}
			else if (checkIntersection(mjp.mjp1.carPosX+myCar.carWidth-1, mjp.mjp1.carPosY, myObstacle.objectsPosX[c], myObstacle.objectsPosY[c]+(mjp.mjp1.step*mjp.mjp1.trackSpeed), myObstacle.objectsSizeX[c], myObstacle.objectsSizeY[c]) == 1)
			{
				dock = 1;
			}
			else if (checkIntersection(mjp.mjp1.carPosX, mjp.mjp1.carPosY+myCar.carHeight-1, myObstacle.objectsPosX[c], myObstacle.objectsPosY[c]+(mjp.mjp1.step*mjp.mjp1.trackSpeed), myObstacle.objectsSizeX[c], myObstacle.objectsSizeY[c]) == 1)
			{
				dock = 1;
			}
			else if (checkIntersection(mjp.mjp1.carPosX+myCar.carWidth-1, mjp.mjp1.carPosY+myCar.carHeight-1, myObstacle.objectsPosX[c], myObstacle.objectsPosY[c]+(mjp.mjp1.step*mjp.mjp1.trackSpeed), myObstacle.objectsSizeX[c], myObstacle.objectsSizeY[c]) == 1)
			{
				dock = 1;
			}
			else {}

			if (dock == 1)
			{
				System.out.print("HIT ");
				myCar.carHealth--;
			}
			else {}
			
		}
		System.out.print(" " + myCar.carHealth + " ");

		//System.out.print(" Test-" + checkIntersection(15, 15, 10, 10, 10, 10) + " ");
		
		mjp.mjp2.repaint();

		if (myCar.carHealth <= 0)
		{
			finish(0);
		}
		else {}

		if (((mjp.mjp1.step*mjp.mjp1.trackSpeed)-mjp.mjp1.trackSpeed)>=10000-10)
		{
			finish(1);
		} else {}

		//End Health
		mjp.mjp1.repaint();
		mjp.mjp1.step = mjp.mjp1.step+1;
	}

//--------------------------------------------------------
//------Import----------------------------
//--------------------------------------------------------
	public void myxmlImport()
	{
		//
		myXML_Reader xml = new myXML_Reader();

  		try
		{
    	         	xml.Open("Simulation");
 	            	//while(true)
  	           	//{
  	            		Object o1 = xml.ReadObject();
				myObstacle.objectsImageNumber = (int[]) o1;
				myObstacle.importObjects();			

				Object o2 = xml.ReadObject();
				myObstacle.objectsPosX = (int[]) o2;

				Object o3 = xml.ReadObject();
				myObstacle.objectsPosY = (int[]) o3;

				Object o4 = xml.ReadObject();
				myObstacle.objectsSizeX = (int[]) o4;

				Object o5 = xml.ReadObject();
				myObstacle.objectsSizeY = (int[]) o5;

				Object t1 = xml.ReadObject();
				myTrack.trackNumber = (int[]) t1;
				myTrack.importTrack(myTrack.trackNumber[0]);

				Object c1 = xml.ReadObject();
				myCar.carNumber = (int[]) c1;
				myCar.changeCar(myCar.carNumber[0]);

				Object i1 = xml.ReadObject();
				inputL = (int[]) i1;
				Object i2 = xml.ReadObject();
				inputR = (int[]) i2;
				Object i3 = xml.ReadObject();
				inputD = (int[]) i3;
				Object i4 = xml.ReadObject();
				inputU = (int[]) i4;

  	            		//if (o1 == null) break;
				

  	        	//}
  	           	xml.Close();
  	       	}
        	catch(FileNotFoundException e) {e.printStackTrace();}
        	catch(ArrayIndexOutOfBoundsException e) 
        	{
        		//The exception is caught and the try statement finishes
        	}
	}


//--------------------------------------------------------
//------Reset----------------------------
//--------------------------------------------------------
	public void reset()
	{
		tim.stop();
		mjp.mjp1.step=0;
		mjp.mjp1.carPosX=275;
		mjp.mjp1.carPosY=475;
		myCar.carHealth = myCar.carHealthMax;
		mjp.mjp1.repaint();
		mjp.mjp2.repaint();
	}

//--------------------------------------------------------
//------Finish----------------------------
//--------------------------------------------------------
	public void finish(int input)
	{
		tim.stop();
		vcrFinJFrame sfjf = new vcrFinJFrame(input, myObstacle, myTrack, myCar, inputL, inputR, inputD, inputU);
	}


//--------------------------------------------------------
//------Check for intersection----------------------------
//--------------------------------------------------------
	public int checkIntersection(int inputX, int inputY, int inputObsX, int inputObsY, int inputObsWidth, int inputObsHeight)
	{
		int result = 0;

		

		if ((inputX > inputObsX) && (inputX < inputObsX+inputObsWidth-1))	
		{
			//System.out.print("X Yes ");
			//System.out.println(inputX + " " +inputY + " " +inputObsX + " " +inputObsY + " " +inputObsWidth + " " +inputObsHeight + " ");
			
			if ((inputY > inputObsY) && (inputY < inputObsY+inputObsHeight-1))
			{
				//System.out.print("Y Yes ");
				result = 1;
			}
			else {}
		}
		else {}
		
		return result;
	}
		
//--------------------------------------------------------

	public void actionPerformed(ActionEvent event) 
   	{
  		Object obj = event.getSource();
   	  	if (obj == mjp.mjp2.d)
   	  	{
   	  		System.out.println("Replay");
			reset();
   	  	}
   	  	if (obj == mjp.mjp2.e)
   	  	{
   	  		System.out.println("Start");
			tim.start();
   	  	}
   	  	
		if (obj == mjp.mjp2.g)
		{
			System.out.println("Exit Simulation");
		reset();
		new titleJFrameA();
		this.dispose();
		}
		else{}
		requestFocusInWindow();
   	}

}