using UnityEngine;
using UnityEditor;
using System.Collections;

namespace TurnTheGameOn.IKDriver{
	[CustomEditor(typeof(IKD_VehicleController))]
	public class Editor_IKD_VehicleController : Editor {

		bool showInput;
		bool showHUD;
		bool showSpeed;
		bool showTransmission;
		bool showNitro;
		bool showHandling;
		bool showWheels;
		bool showAIOverride;

		public override void OnInspectorGUI(){
			IKD_VehicleController vehicleController = (IKD_VehicleController)target;

			EditorGUILayout.BeginVertical ("Box");
			showSpeed = EditorGUI.Foldout (EditorGUILayout.GetControlRect(), showSpeed, "Acceleration, Speed and Braking", true);	
			if (showSpeed) {

				SerializedProperty speedType = serializedObject.FindProperty ("speedType");
				EditorGUI.BeginChangeCheck ();
				EditorGUILayout.PropertyField (speedType, true);
				if (EditorGUI.EndChangeCheck ())
					serializedObject.ApplyModifiedProperties ();

				SerializedProperty topSpeed = serializedObject.FindProperty ("topSpeed");
				EditorGUI.BeginChangeCheck ();
				EditorGUILayout.PropertyField (topSpeed, true);
				if (EditorGUI.EndChangeCheck ())
					serializedObject.ApplyModifiedProperties ();

				SerializedProperty topSpeedReverse = serializedObject.FindProperty ("topSpeedReverse");
				EditorGUI.BeginChangeCheck ();
				EditorGUILayout.PropertyField (topSpeedReverse, true);
				if (EditorGUI.EndChangeCheck ())
					serializedObject.ApplyModifiedProperties ();

				SerializedProperty fullTorqueOverAllWheels = serializedObject.FindProperty ("fullTorqueOverAllWheels");
				EditorGUI.BeginChangeCheck ();
				EditorGUILayout.PropertyField (fullTorqueOverAllWheels, true);
				if (EditorGUI.EndChangeCheck ())
					serializedObject.ApplyModifiedProperties ();

				SerializedProperty reverseTorque = serializedObject.FindProperty ("reverseTorque");
				EditorGUI.BeginChangeCheck ();
				EditorGUILayout.PropertyField (reverseTorque, true);
				if (EditorGUI.EndChangeCheck ())
					serializedObject.ApplyModifiedProperties ();

				SerializedProperty brakeTorque = serializedObject.FindProperty ("brakeTorque");
				EditorGUI.BeginChangeCheck ();
				EditorGUILayout.PropertyField (brakeTorque, true);
				if (EditorGUI.EndChangeCheck ())
					serializedObject.ApplyModifiedProperties ();

				SerializedProperty maxHandbrakeTorque = serializedObject.FindProperty ("maxHandbrakeTorque");
				EditorGUI.BeginChangeCheck ();
				EditorGUILayout.PropertyField (maxHandbrakeTorque, true);
				if (EditorGUI.EndChangeCheck ())
					serializedObject.ApplyModifiedProperties ();

				EditorUtility.SetDirty (vehicleController);

			}
			EditorGUILayout.EndVertical ();

			EditorGUILayout.BeginVertical ("Box");
			showTransmission = EditorGUI.Foldout (EditorGUILayout.GetControlRect(), showTransmission, "Transmission", true);	
			if (showTransmission) {

				SerializedProperty manual = serializedObject.FindProperty ("manual");
				EditorGUI.BeginChangeCheck ();
				EditorGUILayout.PropertyField (manual, true);
				if (EditorGUI.EndChangeCheck ())
					serializedObject.ApplyModifiedProperties ();

				SerializedProperty NoOfGears = serializedObject.FindProperty ("NoOfGears");
				EditorGUI.BeginChangeCheck ();
				EditorGUILayout.PropertyField (NoOfGears, true);
				if (EditorGUI.EndChangeCheck ())
					serializedObject.ApplyModifiedProperties ();

				SerializedProperty minRPM = serializedObject.FindProperty ("minRPM");
				EditorGUI.BeginChangeCheck ();
				EditorGUILayout.PropertyField (minRPM, true);
				if (EditorGUI.EndChangeCheck ())
					serializedObject.ApplyModifiedProperties ();

				SerializedProperty maxRPM = serializedObject.FindProperty ("maxRPM");
				EditorGUI.BeginChangeCheck ();
				EditorGUILayout.PropertyField (maxRPM, true);
				if (EditorGUI.EndChangeCheck ())
					serializedObject.ApplyModifiedProperties ();

				SerializedProperty RPMFallRate = serializedObject.FindProperty ("RPMFallRate");
				EditorGUI.BeginChangeCheck ();
				EditorGUILayout.PropertyField (RPMFallRate, true);
				if (EditorGUI.EndChangeCheck ())
					serializedObject.ApplyModifiedProperties ();

				SerializedProperty torqueCurve = serializedObject.FindProperty ("torqueCurve");
				EditorGUI.BeginChangeCheck ();
				EditorGUILayout.PropertyField (torqueCurve, true);
				if (EditorGUI.EndChangeCheck ())
					serializedObject.ApplyModifiedProperties ();

				SerializedProperty gearSpeedLimitCurve = serializedObject.FindProperty ("gearSpeedLimitCurve");
				EditorGUI.BeginChangeCheck ();
				EditorGUILayout.PropertyField (gearSpeedLimitCurve, true);
				if (EditorGUI.EndChangeCheck ())
					serializedObject.ApplyModifiedProperties ();

				SerializedProperty revRangeBoundary = serializedObject.FindProperty ("revRangeBoundary");
				EditorGUI.BeginChangeCheck ();
				EditorGUILayout.PropertyField (revRangeBoundary, true);
				if (EditorGUI.EndChangeCheck ())
					serializedObject.ApplyModifiedProperties ();

                SerializedProperty multiplierPedalToRPM = serializedObject.FindProperty("multiplierPedalToRPM");
                EditorGUI.BeginChangeCheck();
                EditorGUILayout.PropertyField(multiplierPedalToRPM, true);
                if (EditorGUI.EndChangeCheck())
                    serializedObject.ApplyModifiedProperties();

                SerializedProperty multiplierSpeedToRPM = serializedObject.FindProperty("multiplierSpeedToRPM");
                EditorGUI.BeginChangeCheck();
                EditorGUILayout.PropertyField(multiplierSpeedToRPM, true);
                if (EditorGUI.EndChangeCheck())
                    serializedObject.ApplyModifiedProperties();



                EditorUtility.SetDirty (vehicleController);

			}
			EditorGUILayout.EndVertical ();

			EditorGUILayout.BeginVertical ("Box");
			showNitro = EditorGUI.Foldout (EditorGUILayout.GetControlRect(), showNitro, "Nitro", true);	
			if (showNitro) {

				SerializedProperty nitroTopSpeed = serializedObject.FindProperty ("nitroTopSpeed");
				EditorGUI.BeginChangeCheck ();
				EditorGUILayout.PropertyField (nitroTopSpeed, true);
				if (EditorGUI.EndChangeCheck ())
					serializedObject.ApplyModifiedProperties ();

				SerializedProperty nitroFullTorque = serializedObject.FindProperty ("nitroFullTorque");
				EditorGUI.BeginChangeCheck ();
				EditorGUILayout.PropertyField (nitroFullTorque, true);
				if (EditorGUI.EndChangeCheck ())
					serializedObject.ApplyModifiedProperties ();

				SerializedProperty nitroDuration = serializedObject.FindProperty ("nitroDuration");
				EditorGUI.BeginChangeCheck ();
				EditorGUILayout.PropertyField (nitroDuration, true);
				if (EditorGUI.EndChangeCheck ())
					serializedObject.ApplyModifiedProperties ();

				SerializedProperty nitroSpendRate = serializedObject.FindProperty ("nitroSpendRate");
				EditorGUI.BeginChangeCheck ();
				EditorGUILayout.PropertyField (nitroSpendRate, true);
				if (EditorGUI.EndChangeCheck ())
					serializedObject.ApplyModifiedProperties ();

				SerializedProperty nitroRefillRate = serializedObject.FindProperty ("nitroRefillRate");
				EditorGUI.BeginChangeCheck ();
				EditorGUILayout.PropertyField (nitroRefillRate, true);
				if (EditorGUI.EndChangeCheck ())
					serializedObject.ApplyModifiedProperties ();

				SerializedProperty nitroFX = serializedObject.FindProperty ("nitroFX");
				EditorGUI.BeginChangeCheck ();
				EditorGUILayout.PropertyField (nitroFX, true);
				if (EditorGUI.EndChangeCheck ())
					serializedObject.ApplyModifiedProperties ();

				SerializedProperty nitroOn = serializedObject.FindProperty ("nitroOn");
				EditorGUI.BeginChangeCheck ();
				EditorGUILayout.PropertyField (nitroOn, true);
				if (EditorGUI.EndChangeCheck ())
					serializedObject.ApplyModifiedProperties ();


				EditorUtility.SetDirty (vehicleController);

			}
			EditorGUILayout.EndVertical ();

			EditorGUILayout.BeginVertical ("Box");
			showHandling = EditorGUI.Foldout (EditorGUILayout.GetControlRect(), showHandling, "Physics and Handling", true);	
			if (showHandling) {

				SerializedProperty rbody = serializedObject.FindProperty ("rbody");
				EditorGUI.BeginChangeCheck ();
				EditorGUILayout.PropertyField (rbody, true);
				if (EditorGUI.EndChangeCheck ())
					serializedObject.ApplyModifiedProperties ();

				SerializedProperty centerOfMass = serializedObject.FindProperty ("centerOfMass");
				EditorGUI.BeginChangeCheck ();
				EditorGUILayout.PropertyField (centerOfMass, true);
				if (EditorGUI.EndChangeCheck ())
					serializedObject.ApplyModifiedProperties ();

				SerializedProperty carDriveType = serializedObject.FindProperty ("carDriveType");
				EditorGUI.BeginChangeCheck ();
				EditorGUILayout.PropertyField (carDriveType, true);
				if (EditorGUI.EndChangeCheck ())
					serializedObject.ApplyModifiedProperties ();

				SerializedProperty downforce = serializedObject.FindProperty ("downforce");
				EditorGUI.BeginChangeCheck ();
				EditorGUILayout.PropertyField (downforce, true);
				if (EditorGUI.EndChangeCheck ())
					serializedObject.ApplyModifiedProperties ();

				SerializedProperty slipLimit = serializedObject.FindProperty ("slipLimit");
				EditorGUI.BeginChangeCheck ();
				EditorGUILayout.PropertyField (slipLimit, true);
				if (EditorGUI.EndChangeCheck ())
					serializedObject.ApplyModifiedProperties ();

				SerializedProperty steerSensitivity = serializedObject.FindProperty ("steerSensitivity");
				EditorGUI.BeginChangeCheck ();
				EditorGUILayout.PropertyField (steerSensitivity, true);
				if (EditorGUI.EndChangeCheck ())
					serializedObject.ApplyModifiedProperties ();

				SerializedProperty maxSteerAngle = serializedObject.FindProperty ("maxSteerAngle");
				EditorGUI.BeginChangeCheck ();
				EditorGUILayout.PropertyField (maxSteerAngle, true);
				if (EditorGUI.EndChangeCheck ())
					serializedObject.ApplyModifiedProperties ();

				SerializedProperty steerAngleAtMaxSpeed = serializedObject.FindProperty ("steerAngleAtMaxSpeed");
				EditorGUI.BeginChangeCheck ();
				EditorGUILayout.PropertyField (steerAngleAtMaxSpeed, true);
				if (EditorGUI.EndChangeCheck ())
					serializedObject.ApplyModifiedProperties ();

				SerializedProperty steerHelper = serializedObject.FindProperty ("steerHelper");
				EditorGUI.BeginChangeCheck ();
				EditorGUILayout.PropertyField (steerHelper, true);
				if (EditorGUI.EndChangeCheck ())
					serializedObject.ApplyModifiedProperties ();

				SerializedProperty tractionControl = serializedObject.FindProperty ("tractionControl");
				EditorGUI.BeginChangeCheck ();
				EditorGUILayout.PropertyField (tractionControl, true);
				if (EditorGUI.EndChangeCheck ())
					serializedObject.ApplyModifiedProperties ();

				EditorUtility.SetDirty (vehicleController);

			}
			EditorGUILayout.EndVertical ();

			EditorGUILayout.BeginVertical ("Box");
			showWheels = EditorGUI.Foldout (EditorGUILayout.GetControlRect(), showWheels, "Wheels", true);	
			if (showWheels) {

				SerializedProperty wheelMeshes = serializedObject.FindProperty ("wheelMeshes");
				EditorGUI.BeginChangeCheck ();
				EditorGUILayout.PropertyField (wheelMeshes, true);
				if (EditorGUI.EndChangeCheck ())
					serializedObject.ApplyModifiedProperties ();

				if (GUILayout.Button ("Align Wheel Colliders") ) {
					AlignWheelColliders ();
				}

				SerializedProperty wheelColliders = serializedObject.FindProperty ("wheelColliders");
				EditorGUI.BeginChangeCheck ();
				EditorGUILayout.PropertyField (wheelColliders, true);
				if (EditorGUI.EndChangeCheck ())
					serializedObject.ApplyModifiedProperties ();

				SerializedProperty wheelEffects = serializedObject.FindProperty ("wheelEffects");
				EditorGUI.BeginChangeCheck ();
				EditorGUILayout.PropertyField (wheelEffects, true);
				if (EditorGUI.EndChangeCheck ())
					serializedObject.ApplyModifiedProperties ();


				EditorUtility.SetDirty (vehicleController);

			}
			EditorGUILayout.EndVertical ();

			EditorGUILayout.BeginVertical ("Box");
			showAIOverride = EditorGUI.Foldout (EditorGUILayout.GetControlRect(), showAIOverride, "AI Controller Overrides", true);	
			if (showAIOverride) {

				SerializedProperty overrideBrake = serializedObject.FindProperty ("overrideBrake");
				EditorGUI.BeginChangeCheck ();
				EditorGUILayout.PropertyField (overrideBrake, true);
				if (EditorGUI.EndChangeCheck ())
					serializedObject.ApplyModifiedProperties ();

				SerializedProperty overrideBrakePower = serializedObject.FindProperty ("overrideBrakePower");
				EditorGUI.BeginChangeCheck ();
				EditorGUILayout.PropertyField (overrideBrakePower, true);
				if (EditorGUI.EndChangeCheck ())
					serializedObject.ApplyModifiedProperties ();

				SerializedProperty overrideAcceleration = serializedObject.FindProperty ("overrideAcceleration");
				EditorGUI.BeginChangeCheck ();
				EditorGUILayout.PropertyField (overrideAcceleration, true);
				if (EditorGUI.EndChangeCheck ())
					serializedObject.ApplyModifiedProperties ();

				SerializedProperty overrideAccelerationPower = serializedObject.FindProperty ("overrideAccelerationPower");
				EditorGUI.BeginChangeCheck ();
				EditorGUILayout.PropertyField (overrideAccelerationPower, true);
				if (EditorGUI.EndChangeCheck ())
					serializedObject.ApplyModifiedProperties ();

				SerializedProperty overrideSteering = serializedObject.FindProperty ("overrideSteering");
				EditorGUI.BeginChangeCheck ();
				EditorGUILayout.PropertyField (overrideSteering, true);
				if (EditorGUI.EndChangeCheck ())
					serializedObject.ApplyModifiedProperties ();

				SerializedProperty overrideSteeringPower = serializedObject.FindProperty ("overrideSteeringPower");
				EditorGUI.BeginChangeCheck ();
				EditorGUILayout.PropertyField (overrideSteeringPower, true);
				if (EditorGUI.EndChangeCheck ())
					serializedObject.ApplyModifiedProperties ();


				EditorUtility.SetDirty (vehicleController);

			}
			EditorGUILayout.EndVertical ();

		}


		public void AlignWheelColliders() {
			IKD_VehicleController vehicleController = (IKD_VehicleController)target;
			// make a reference to the colliders original parent
			Transform defaultColliderParent = vehicleController.wheelColliders [0].transform.parent;
			// move colliders to the reference positions
			vehicleController.wheelColliders [0].transform.parent = vehicleController.wheelMeshes [0].transform;
			vehicleController.wheelColliders [1].transform.parent = vehicleController.wheelMeshes [1].transform;
			vehicleController.wheelColliders [2].transform.parent = vehicleController.wheelMeshes [2].transform;
			vehicleController.wheelColliders [3].transform.parent = vehicleController.wheelMeshes [3].transform;
			//adjust the wheel collider positions on x and z axis to match the new wheel position
			vehicleController.wheelColliders [0].transform.position = new Vector3 (vehicleController.wheelMeshes [0].transform.position.x, 
				vehicleController.wheelColliders [0].transform.position.y, vehicleController.wheelMeshes [0].transform.position.z);
			vehicleController.wheelColliders [1].transform.position = new Vector3 (vehicleController.wheelMeshes [1].transform.position.x, 
				vehicleController.wheelColliders [1].transform.position.y, vehicleController.wheelMeshes [1].transform.position.z);
			vehicleController.wheelColliders [2].transform.position = new Vector3 (vehicleController.wheelMeshes [2].transform.position.x, 
				vehicleController.wheelColliders [2].transform.position.y, vehicleController.wheelMeshes [2].transform.position.z);
			vehicleController.wheelColliders [3].transform.position = new Vector3 (vehicleController.wheelMeshes [3].transform.position.x, 
				vehicleController.wheelColliders [3].transform.position.y, vehicleController.wheelMeshes [3].transform.position.z);
			// move colliders back to the original parent
			vehicleController.wheelColliders [0].transform.parent = defaultColliderParent;
			vehicleController.wheelColliders [1].transform.parent = defaultColliderParent;
			vehicleController.wheelColliders [2].transform.parent = defaultColliderParent;
			vehicleController.wheelColliders [3].transform.parent = defaultColliderParent;
		}


	}
}