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

public class simJFrame extends JFrame implements ActionListener, KeyListener
{
	int loop = 1;
	simJPanel 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 simJFrame (obstacle inputObstacle, track inputTrack, car inputCar)
	{
		super ("Road Warrior");
		myObstacle = inputObstacle;
		myTrack = inputTrack;
		myCar = inputCar;

		myObstacle.generateObstacles(myTrack.trackType);

		mjp = new simJPanel(myObstacle, myTrack, myCar);
		
		mjp.mjp2.d.addActionListener(this);
		mjp.mjp2.e.addActionListener(this);
		mjp.mjp2.f.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) 
			{
				System.out.println(mjp.mjp1.step + " " + moveL + moveR + moveD + moveU);
				
				inputL[mjp.mjp1.step] = moveL;
				inputR[mjp.mjp1.step] = moveR;
				inputD[mjp.mjp1.step] = moveD;
				inputU[mjp.mjp1.step] = 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);
		this.addKeyListener(this);
		//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;
	}

//--------------------------------------------------------
//------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();
		simFinJFrame sfjf = new simFinJFrame(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;
	}
		
//--------------------------------------------------------
//------Modify movement commands based on key events------
//--------------------------------------------------------	
	public void keyPressed(KeyEvent e) 
	{
		int x = e.getKeyCode();
		if(x ==  e.VK_LEFT)
		{
			moveL = 1;
		}
		else if(x ==  e.VK_RIGHT)
		{
			moveR = 1;
		}
		else if(x ==  e.VK_UP)
		{
			moveU = 1;
		}
		else if(x ==  e.VK_DOWN)
		{
			moveD = 1;
		}
	}

	public void keyReleased(KeyEvent e)
	{
		int x = e.getKeyCode();
		if(x ==  e.VK_LEFT)
		{
			moveL = 0;
		}
		else if(x ==  e.VK_RIGHT)
		{
			moveR = 0;
		}
		else if(x ==  e.VK_UP)
		{
			moveU = 0;
		}
		else if(x ==  e.VK_DOWN)
		{
			moveD = 0;
		}
	}
	
	public void keyTyped(KeyEvent e) {  }
//--------------------------------------------------------

	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.f)
		{
			System.out.println("Options");
			reset();
			new optionsJFrame(myObstacle, myTrack, myCar);
			this.dispose();
		}
		if (obj == mjp.mjp2.g)
		{
			System.out.println("Exit Simulation");
		reset();
		new titleJFrameA();
		this.dispose();
		}
		else{}
		requestFocusInWindow();
   	}

}