using UnityEngine;
using System.Collections;

public class CarControl : MonoBehaviour {

	public WheelCollider wheelFL;
	public WheelCollider wheelFR;
	public WheelCollider wheelRL;
	public WheelCollider wheelRR;
	
	public Transform wheelFLTrans;
	public Transform wheelFRTrans;
	public Transform wheelRLTrans;
	public Transform wheelRRTrans;
	
	public float lowestSteerAtSpeed = 50;
	public float lowSpeedSteerAngel = 10;
	public float highSpeedSteerAngel = 1;
	public float decellarationSpeed = 30;
	public float maxTorque = 50;
	public float currentSpeed;
	public float topSpeed = 150;
	public float maxReverseSpeed = 50;
	private bool  braked = false;
	public float maxBrakeTorque = 100;
	public int[] gearRatio;
	public GameObject spark;
	public GameObject collisionSound;

	private Vector3 tempMass;
	
	public Texture2D speedOMeterDial;
	public Texture2D speedOMeterPointer;
	
	void  Start (){
		tempMass = GetComponent<Rigidbody>().centerOfMass;
		tempMass.y = -0.2f;
		GetComponent<Rigidbody>().centerOfMass=tempMass;
	}
	
	void  FixedUpdate (){
		Controle();
		HandBrake();
		Reset ();
	}
	
	void  Update (){
		EngineSound();
	}
	
	void  Controle (){
		currentSpeed = 2*22/7*wheelRL.radius*wheelRL.rpm*60/1000;
		currentSpeed = Mathf.Round(currentSpeed);
		
		if (currentSpeed <= topSpeed && currentSpeed > -maxReverseSpeed && !braked){
			wheelRR.motorTorque = maxTorque * Input.GetAxis("Vertical");
			wheelRL.motorTorque = maxTorque * Input.GetAxis("Vertical");
		}
		else{
			wheelRR.motorTorque = 0;
			wheelRL.motorTorque = 0;
		}
		
		if (Input.GetButton("Vertical")==false){
			wheelRR.brakeTorque = decellarationSpeed;
			wheelRL.brakeTorque = decellarationSpeed;
		}
		else{
			wheelRR.brakeTorque = 0;
			wheelRL.brakeTorque = 0;
		}
		
		float speedFactor= GetComponent<Rigidbody>().velocity.magnitude/lowestSteerAtSpeed;
		float currentSteerAngel= Mathf.Lerp(lowSpeedSteerAngel,highSpeedSteerAngel,speedFactor);
		currentSteerAngel *= Input.GetAxis("Horizontal");
		
		wheelFL.steerAngle = currentSteerAngel;
		wheelFR.steerAngle = currentSteerAngel;
	}
	
	
	void  HandBrake (){
		if (Input.GetButton("Jump")){
			braked = true;
		} else{
			braked = false;
		}
		
		if (braked){
			wheelRR.brakeTorque = maxBrakeTorque;
			wheelRL.brakeTorque = maxBrakeTorque;
			wheelRR.motorTorque = 0;
			wheelRL.motorTorque = 0;
		}
	}

	void Reset() {
		if (Input.GetKey(KeyCode.R)){
			transform.position = CarCheckpoint.startPos;
			transform.rotation = CarCheckpoint.startRot;
			CarCheckpoint.currentLap = 0;
			CarCheckpoint.currentCheckpoint = 0;
		}
	}
	
	void  EngineSound (){
		int i = 0;
		for (i= 0; i < gearRatio.Length; i++){
			if(gearRatio[i]> currentSpeed){
				break;
			}
		}
		
		float gearMinValue = 0.00f;
		float gearMaxValue = 0.00f;
		
		if (i == 0){
			gearMinValue = 0;
		}
		else {
			gearMinValue = gearRatio[i-1];
		}
		gearMaxValue = gearRatio[i];
		float enginePitch = ((currentSpeed - gearMinValue)/(gearMaxValue - gearMinValue))+1;
		GetComponent<AudioSource>().pitch = enginePitch;
	}
	
	void  OnGUI (){
		GUI.DrawTexture( new Rect(Screen.width - 300,Screen.height-150,300,150),speedOMeterDial);
		float speedFactor = currentSpeed / topSpeed;
		float rotationAngle;
		
		if (currentSpeed >= 0){
			rotationAngle = Mathf.Lerp(0,180,speedFactor);
		}
		else {
			rotationAngle = Mathf.Lerp(0,180,-speedFactor);
		}
		
		GUIUtility.RotateAroundPivot(rotationAngle,new Vector2(Screen.width-150,Screen.height));
		GUI.DrawTexture( new Rect(Screen.width - 300,Screen.height-150,300,300),speedOMeterPointer); 
	}
	
	void  OnCollisionEnter ( Collision other  ){
		if (other.transform != transform && other.contacts.Length != 0){
			for (int i=0; i<other.contacts.Length;i++){
				Instantiate(spark,other.contacts[i].point,Quaternion.identity);
			}
		}
	}
	
}