using UnityEngine;
using System.Collections;
namespace RootMotion.FinalIK {
	/// 
	/// Contains and manages a set of constraints.
	/// 
	[System.Serializable]
	public class Constraints {
		
		#region Main Interface
		/// 
		/// The transform.
		/// 
		public Transform transform;
		/// 
		/// The target.
		/// 
		public Transform target;
		/// 
		/// The position offset.
		/// 
		public Vector3 positionOffset;
		/// 
		/// The position to lerp to by positionWeight
		/// 
		public Vector3 position;
		/// 
		/// The weight of lerping to position
		/// 
		[Range(0f, 1f)]
		public float positionWeight;
		/// 
		/// The rotation offset.
		/// 
		public Vector3 rotationOffset;
		/// 
		/// The rotation to slerp to by rotationWeight
		/// 
		public Vector3 rotation;
		/// 
		/// The weight of slerping to rotation
		/// 
		[Range(0f, 1f)]
		public float rotationWeight;
        private Vector3 defaultLocalPosition;
        private Quaternion defaultLocalRotation;
		/// 
		/// Determines whether this instance is valid.
		/// 
		public bool IsValid() {
			return transform != null;
		}
		/// 
		/// Initiate to the specified transform.
		/// 
		public void Initiate(Transform transform) {
			this.transform = transform;
			this.position = transform.position;
			this.rotation = transform.eulerAngles;
            defaultLocalPosition = transform.localPosition;
            defaultLocalRotation = transform.localRotation;
		}
        public void FixTransforms()
        {
            transform.localPosition = defaultLocalPosition;
            transform.localRotation = defaultLocalRotation;
        }
		/// 
		/// Updates the constraints.
		/// 
		public void Update() {
			if (!IsValid()) return;
			// Position
			if (target != null) position = target.position;
			transform.position += positionOffset;
			if (positionWeight > 0f) transform.position = Vector3.Lerp(transform.position, position, positionWeight);
			// Rotation
			if (target != null) rotation = target.eulerAngles;
			transform.rotation = Quaternion.Euler(rotationOffset) * transform.rotation;
			if (rotationWeight > 0f) transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.Euler(rotation), rotationWeight);
		}
		
		#endregion Main Interface
	}
}