namespace TurnTheGameOn.IKDriver
{
	using UnityEngine;
	using UnityEngine.UI;
	using UnityEditor;
	using System.Collections;

	[CustomEditor(typeof(IKD_IKDriver))]
	public class Editor_IKD_IKDriver : Editor {

		private float minValue = -1.0f;
		private float minLimit = -3.0f;
		private float maxValue = 1.0f;
		private float maxLimit = 3.0f;
		private float minValueSpeed = 0.001f;
		private float minLimitSpeed = 0f;
		private float maxValueSpeed = 5.0f;
		private float maxLimitSpeed = 10.0f;
		private bool showCurrentIKDriverTargets, showCurrentIKTargetObjects, showIKSteeringWheelTargets, showOtherIKTargetObjects;

		private SerializedObject m_Object;
		private SerializedProperty m_Property; 

		void SetEditorDirty ()
		{
			m_Object.ApplyModifiedProperties();
			EditorUtility.SetDirty (target);
		}

		public override void OnInspectorGUI()
		{
			// inspector script reference
			GUI.enabled = false;
			EditorGUILayout.ObjectField("Script:", MonoScript.FromMonoBehaviour((IKD_IKDriver)target), typeof(IKD_IKDriver), false);
			GUI.enabled = true;
			// target reference
			IKD_IKDriver ikd_IKDriver = (IKD_IKDriver)target;
			m_Object = new SerializedObject(target);
			// editor button width
			float buttonWidth = (Screen.width - 30) / 3.0f;

			#region Inspector Menu Buttons
			EditorGUILayout.LabelField ("", GUI.skin.horizontalSlider);
			EditorGUILayout.LabelField ("IK Driver Configuration Options", EditorStyles.centeredGreyMiniLabel);
			// Inspector Buttons - Row 1
			EditorGUILayout.BeginHorizontal ();
			if (GUILayout.Button ("Avatar", GUILayout.Width (buttonWidth) ))
			{
				ikd_IKDriver.editorView = "avatar";					/// Avatar
			}
			if (GUILayout.Button ("Head", GUILayout.Width (buttonWidth) ))
			{
				ikd_IKDriver.editorView = "head";					/// Head
			}
			if (GUILayout.Button ("Input", GUILayout.Width (buttonWidth) ))
			{
				ikd_IKDriver.editorView = "input";					/// Input
			}
			EditorGUILayout.EndHorizontal ();
			// Inspector Buttons - Row 2
			EditorGUILayout.BeginHorizontal ();
			if (GUILayout.Button ("Shifting", GUILayout.Width (buttonWidth) ))
			{
				ikd_IKDriver.editorView = "shifting";				/// Shifting
			}
			if (GUILayout.Button ("Steering", GUILayout.Width (buttonWidth) ))
			{
				ikd_IKDriver.editorView = "steering";				/// Steering
			}
			if (GUILayout.Button ("Targets", GUILayout.Width (buttonWidth) ))
			{
				ikd_IKDriver.editorView = "iktargets";				/// IK Targets
			}
			EditorGUILayout.EndHorizontal ();
			EditorGUILayout.LabelField ("", GUI.skin.horizontalSlider);
			#endregion

			// inspector menus
			EditorGUILayout.BeginVertical("Box");
			#region Avatar
			if (ikd_IKDriver.editorView == "avatar") 
			{
				EditorGUILayout.LabelField ("IK Avatar Settings", EditorStyles.centeredGreyMiniLabel);
				EditorGUILayout.BeginVertical("Box");
				ikd_IKDriver.ikActive = EditorGUILayout.Toggle ("IK Active", ikd_IKDriver.ikActive);
				ikd_IKDriver.avatarPosition = EditorGUILayout.Vector3Field ("Avatar Position", ikd_IKDriver.avatarPosition);
				ikd_IKDriver.shiftHand = (IKD_IKDriver.TargetSide) EditorGUILayout.EnumPopup ("Shift Hand", ikd_IKDriver.shiftHand);
				ikd_IKDriver.clutchFoot = (IKD_IKDriver.TargetSide) EditorGUILayout.EnumPopup ("Clutch Foot", ikd_IKDriver.clutchFoot);
				ikd_IKDriver.brakeFoot = (IKD_IKDriver.TargetSide) EditorGUILayout.EnumPopup ("Brake Foot", ikd_IKDriver.brakeFoot);
				ikd_IKDriver.gasFoot = (IKD_IKDriver.TargetSide) EditorGUILayout.EnumPopup ("Gas Foot", ikd_IKDriver.gasFoot);
				EditorGUILayout.EndVertical();
			}
			#endregion

			#region Head
			if (ikd_IKDriver.editorView == "head") 
			{
				minValue = ikd_IKDriver.maxLookLeft;
				maxValue = ikd_IKDriver.maxLookRight;
				minValueSpeed = ikd_IKDriver.minLookSpeed;
				maxValueSpeed = ikd_IKDriver.maxLookSpeed;

				EditorGUILayout.LabelField ("IK Head Look Settings", EditorStyles.centeredGreyMiniLabel);

				EditorGUILayout.BeginVertical("Box");

				EditorGUILayout.BeginHorizontal();
				EditorGUILayout.LabelField ("Look Range", GUILayout.MaxWidth(Screen.width * 0.3f));

				EditorGUI.BeginChangeCheck();
				EditorGUILayout.MinMaxSlider( ref minValue, ref maxValue, minLimit, maxLimit );
				EditorGUILayout.EndHorizontal();
				minValue = EditorGUILayout.FloatField ("Max Look Left", minValue);
				maxValue = EditorGUILayout.FloatField ("Max Look Right", maxValue);

				ikd_IKDriver.maxLookLeft = minValue;

				ikd_IKDriver.maxLookRight = maxValue;
				if (EditorGUI.EndChangeCheck())
					serializedObject.ApplyModifiedProperties();
				///
				SerializedProperty defaultLookXPos = serializedObject.FindProperty("defaultLookXPos");
				EditorGUI.BeginChangeCheck();
				EditorGUILayout.PropertyField(defaultLookXPos, true);
				if (EditorGUI.EndChangeCheck())
					serializedObject.ApplyModifiedProperties();
				EditorGUILayout.EndVertical();

				EditorGUILayout.BeginVertical("Box");
				EditorGUILayout.BeginHorizontal();
				EditorGUILayout.LabelField ("Look Speed", GUILayout.MaxWidth(Screen.width * 0.3f));
				EditorGUILayout.MinMaxSlider( ref minValueSpeed, ref maxValueSpeed, minLimitSpeed, maxLimitSpeed );
				EditorGUILayout.EndHorizontal();
				minValueSpeed = EditorGUILayout.FloatField ("Steer Look Speed", minValueSpeed);
				maxValueSpeed = EditorGUILayout.FloatField ("Snap Back Speed", maxValueSpeed);
				ikd_IKDriver.minLookSpeed = minValueSpeed;
				ikd_IKDriver.maxLookSpeed = maxValueSpeed;
				EditorGUILayout.EndVertical();
			}
			#endregion

			#region Steering
			if (ikd_IKDriver.editorView == "steering")
			{
				EditorGUILayout.LabelField ("IK Steering Settings", EditorStyles.centeredGreyMiniLabel);
				EditorGUILayout.BeginVertical("Box");
				ikd_IKDriver.steeringTargets = (IKD_IKDriver.SteeringTargets) EditorGUILayout.EnumPopup ("Steering Targets", ikd_IKDriver.steeringTargets);
				ikd_IKDriver.controlSteeringWheel = EditorGUILayout.Toggle ("Control Steering Wheel", ikd_IKDriver.controlSteeringWheel);
				if (ikd_IKDriver.steeringTargets == IKD_IKDriver.SteeringTargets.All) 
				{
					ikd_IKDriver.steeringWheelRotation = EditorGUILayout.Slider ("Steering Wheel Rotation", ikd_IKDriver.steeringWheelRotation, 0, 360);
				} 
				else if(ikd_IKDriver.steeringTargets == IKD_IKDriver.SteeringTargets.Two)
				{
					ikd_IKDriver.steeringWheelRotation = EditorGUILayout.Slider ("Steering Wheel Rotation", ikd_IKDriver.steeringWheelRotation, 0, 90);
				}
				ikd_IKDriver.wheelShake = EditorGUILayout.Slider ("Wheel Shake", ikd_IKDriver.wheelShake, 0, 1);
				ikd_IKDriver.defaultSteering = EditorGUILayout.Vector3Field ("Steering Wheel Rotation", ikd_IKDriver.defaultSteering);
				ikd_IKDriver.steeringWheel = EditorGUILayout.ObjectField("Steering Wheel", ikd_IKDriver.steeringWheel, typeof(Transform), true) as Transform;
//				ikd_IKDriver.wheelCollider = EditorGUILayout.ObjectField("Wheel Collider", ikd_IKDriver.wheelCollider, typeof(WheelCollider), true) as WheelCollider;
				ikd_IKDriver.steeringRotationSpeed = EditorGUILayout.FloatField ("Steering Rotation Speed", ikd_IKDriver.steeringRotationSpeed);
				EditorGUILayout.EndVertical();
			}
			#endregion

			#region Shifting
			if (ikd_IKDriver.editorView == "shifting")
			{
				EditorGUILayout.LabelField ("IK Shift Settings", EditorStyles.centeredGreyMiniLabel);
				EditorGUILayout.BeginVertical("Box");
				ikd_IKDriver.enableShifting = EditorGUILayout.Toggle ("Enable Shifting", ikd_IKDriver.enableShifting);
				ikd_IKDriver.shift = EditorGUILayout.Toggle ("Shift", ikd_IKDriver.shift);
				ikd_IKDriver.shiftAnimSpeed = EditorGUILayout.FloatField ("Shift Anim Speed", ikd_IKDriver.shiftAnimSpeed);
				EditorGUILayout.EndVertical();
			}
			#endregion

			#region Input
			if (ikd_IKDriver.editorView == "input")
			{
				EditorGUILayout.LabelField ("IK Input Settings", EditorStyles.centeredGreyMiniLabel);
				EditorGUILayout.BeginVertical("Box");
				ikd_IKDriver.mobile = EditorGUILayout.Toggle ("Mobile", ikd_IKDriver.mobile);
				ikd_IKDriver.useRCCInput = EditorGUILayout.Toggle ("RCC", ikd_IKDriver.useRCCInput);
				ikd_IKDriver.steeringAxis = EditorGUILayout.TextField ("Steering Axis", ikd_IKDriver.steeringAxis);
				ikd_IKDriver.throttleAxis = EditorGUILayout.TextField ("Throttle Axis", ikd_IKDriver.throttleAxis);

				m_Property = m_Object.FindProperty("vehicleRigidbody");
				EditorGUILayout.PropertyField(m_Property, new GUIContent("Vehicle Rigidbody"), true);

				m_Property = m_Object.FindProperty("gearText");
				EditorGUILayout.PropertyField(m_Property, new GUIContent("Gear Text"), true);

				m_Object.ApplyModifiedProperties();

				EditorGUILayout.EndVertical();
			}
			#endregion

			#region Targets
			if (ikd_IKDriver.editorView == "iktargets") 
			{
				EditorGUILayout.LabelField ("IK Target Transforms", EditorStyles.centeredGreyMiniLabel);
				EditorGUILayout.BeginVertical ("Box");
				showCurrentIKDriverTargets = EditorGUILayout.Foldout (showCurrentIKDriverTargets, "IK Control Points", true);
				if (showCurrentIKDriverTargets) {
					EditorGUILayout.BeginVertical ("Box");
					ikd_IKDriver.headLookIKCP = EditorGUILayout.ObjectField ("Head Look", ikd_IKDriver.headLookIKCP, typeof(Transform), true) as Transform;
					ikd_IKDriver.targetLeftHandIK = EditorGUILayout.ObjectField ("Left Hand", ikd_IKDriver.targetLeftHandIK, typeof(Transform), true) as Transform;
					ikd_IKDriver.targetRightHandIK = EditorGUILayout.ObjectField ("Right Hand", ikd_IKDriver.targetRightHandIK, typeof(Transform), true) as Transform;
					ikd_IKDriver.targetLeftFootIK = EditorGUILayout.ObjectField ("Left Foot", ikd_IKDriver.targetLeftFootIK, typeof(Transform), true) as Transform;
					ikd_IKDriver.targetRightFootIK = EditorGUILayout.ObjectField ("Right Foot", ikd_IKDriver.targetRightFootIK, typeof(Transform), true) as Transform;
					EditorGUILayout.EndVertical ();
				}

				showCurrentIKTargetObjects = EditorGUILayout.Foldout (showCurrentIKTargetObjects, "Current IK Targets", true);
				if (showCurrentIKTargetObjects) {
					EditorGUILayout.BeginVertical ("Box");
					ikd_IKDriver.leftHandObj = EditorGUILayout.ObjectField ("Left Hand", ikd_IKDriver.leftHandObj, typeof(Transform), true) as Transform;
					ikd_IKDriver.rightHandObj = EditorGUILayout.ObjectField ("Right Hand", ikd_IKDriver.rightHandObj, typeof(Transform), true) as Transform;
					ikd_IKDriver.leftFootObj = EditorGUILayout.ObjectField ("Left Foot", ikd_IKDriver.leftFootObj, typeof(Transform), true) as Transform;
					ikd_IKDriver.rightFootObj = EditorGUILayout.ObjectField ("Right Foot", ikd_IKDriver.rightFootObj, typeof(Transform), true) as Transform;	
					EditorGUILayout.EndVertical ();
				}

				showIKSteeringWheelTargets = EditorGUILayout.Foldout (showIKSteeringWheelTargets, "IK Targets", true);
				if (showIKSteeringWheelTargets) {
					EditorGUILayout.BeginVertical ("Box");
					ikd_IKDriver.lhswt_W = EditorGUILayout.ObjectField ("LH W", ikd_IKDriver.lhswt_W, typeof(Transform), true) as Transform;
					ikd_IKDriver.lhswt_NW = EditorGUILayout.ObjectField ("LH NW", ikd_IKDriver.lhswt_NW, typeof(Transform), true) as Transform;
					ikd_IKDriver.lhswt_N = EditorGUILayout.ObjectField ("LH N", ikd_IKDriver.lhswt_N, typeof(Transform), true) as Transform;
					ikd_IKDriver.lhswt_NE = EditorGUILayout.ObjectField ("LH NE", ikd_IKDriver.lhswt_NE, typeof(Transform), true) as Transform;
					ikd_IKDriver.lhswt_E = EditorGUILayout.ObjectField ("LH E", ikd_IKDriver.lhswt_E, typeof(Transform), true) as Transform;
					ikd_IKDriver.lhswt_SE = EditorGUILayout.ObjectField ("LH SE", ikd_IKDriver.lhswt_SE, typeof(Transform), true) as Transform;
					ikd_IKDriver.lhswt_S = EditorGUILayout.ObjectField ("LH S", ikd_IKDriver.lhswt_S, typeof(Transform), true) as Transform;
					ikd_IKDriver.lhswt_SW = EditorGUILayout.ObjectField ("LH SW", ikd_IKDriver.lhswt_SW, typeof(Transform), true) as Transform;
					ikd_IKDriver.rhswt_W = EditorGUILayout.ObjectField ("RH W", ikd_IKDriver.rhswt_W, typeof(Transform), true) as Transform;
					ikd_IKDriver.rhswt_NW = EditorGUILayout.ObjectField ("RH NW", ikd_IKDriver.rhswt_NW, typeof(Transform), true) as Transform;
					ikd_IKDriver.rhswt_N = EditorGUILayout.ObjectField ("RH N", ikd_IKDriver.rhswt_N, typeof(Transform), true) as Transform;
					ikd_IKDriver.rhswt_NE = EditorGUILayout.ObjectField ("RH NE", ikd_IKDriver.rhswt_NE, typeof(Transform), true) as Transform;
					ikd_IKDriver.rhswt_E = EditorGUILayout.ObjectField ("RH E", ikd_IKDriver.rhswt_E, typeof(Transform), true) as Transform;
					ikd_IKDriver.rhswt_SE = EditorGUILayout.ObjectField ("RH SE", ikd_IKDriver.rhswt_SE, typeof(Transform), true) as Transform;
					ikd_IKDriver.rhswt_S = EditorGUILayout.ObjectField ("RH S", ikd_IKDriver.rhswt_S, typeof(Transform), true) as Transform;
					ikd_IKDriver.rhswt_SW = EditorGUILayout.ObjectField ("RH SW", ikd_IKDriver.rhswt_SW, typeof(Transform), true) as Transform;
					ikd_IKDriver.handShift = EditorGUILayout.ObjectField ("Hand Shift", ikd_IKDriver.handShift, typeof(Transform), true) as Transform;
					ikd_IKDriver.footBrake = EditorGUILayout.ObjectField ("Foot Brake", ikd_IKDriver.footBrake, typeof(Transform), true) as Transform;
					ikd_IKDriver.footGas = EditorGUILayout.ObjectField ("Foot Gas", ikd_IKDriver.footGas, typeof(Transform), true) as Transform;
					ikd_IKDriver.leftFootIdle = EditorGUILayout.ObjectField ("Left Foot Idle", ikd_IKDriver.leftFootIdle, typeof(Transform), true) as Transform;
					ikd_IKDriver.footClutch = EditorGUILayout.ObjectField ("Foot Clutch", ikd_IKDriver.footClutch, typeof(Transform), true) as Transform;
					ikd_IKDriver.rightFootIdle = EditorGUILayout.ObjectField ("Foot Idle", ikd_IKDriver.rightFootIdle, typeof(Transform), true) as Transform;
					EditorGUILayout.EndVertical ();
				}
				EditorGUILayout.EndVertical ();
			}
			#endregion
			EditorGUILayout.EndVertical();
		}
	}
}