using UnityEngine;
using System.Collections;
namespace RootMotion.FinalIK {
	/// 
	/// Maps a single bone to a node in %IK Solver
	/// 
	[System.Serializable]
	public class IKMappingBone: IKMapping {
		
		#region Main Interface
		
		/// 
		/// The bone transform.
		/// 
		public Transform bone;
		
		/// 
		/// The weight of maintaining the bone's rotation after solver has finished.
		/// 
		[Range(0f, 1f)]
		public float maintainRotationWeight = 1f;
		
		/// 
		/// Determines whether this IKMappingBone is valid.
		/// 
		public override bool IsValid(IKSolver solver, ref string message) {
			if (!base.IsValid(solver, ref message)) return false;
			
			if (bone == null) {
				message = "IKMappingBone's bone is null.";
				return false;
			}
			return true;
		}
		
		#endregion Main Interface
		
		private BoneMap boneMap = new BoneMap();
		
		public IKMappingBone() {}
		
		public IKMappingBone(Transform bone) {
			this.bone = bone;
		}
		public void StoreDefaultLocalState() {
			boneMap.StoreDefaultLocalState();
		}
		
		public void FixTransforms() {
			boneMap.FixTransform(false);
		}
		
		/*
		 * Initiating and setting defaults
		 * */
		public override void Initiate(IKSolverFullBody solver) {
			if (boneMap == null) boneMap = new BoneMap();
			boneMap.Initiate(bone, solver);
		}
		
		/*
		 * Pre-solving
		 * */
		public void ReadPose() {
			boneMap.MaintainRotation();
		}
		
		public void WritePose(float solverWeight) {
			// Rotating back to the last maintained rotation
			boneMap.RotateToMaintain(solverWeight * maintainRotationWeight);
		}
	}
}