using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class SkeletonUtilityKinematicShadow : MonoBehaviour {

	public bool hideShadow = true;

	Dictionary<Transform, Transform> shadowTable;
	GameObject shadowRoot;

	void Start(){
		shadowRoot = (GameObject)Instantiate(gameObject);
		if(hideShadow)
			shadowRoot.hideFlags = HideFlags.HideInHierarchy;

		shadowRoot.transform.parent = transform.root;

		shadowTable = new Dictionary<Transform, Transform>();

		Destroy(shadowRoot.GetComponent<SkeletonUtilityKinematicShadow>());

		shadowRoot.transform.position = transform.position;
		shadowRoot.transform.rotation = transform.rotation;

		Vector3 scaleRef = transform.TransformPoint(Vector3.right);
		float scale = Vector3.Distance(transform.position, scaleRef);
		shadowRoot.transform.localScale = Vector3.one;

		var shadowJoints = shadowRoot.GetComponentsInChildren<Joint>();
		foreach(Joint j in shadowJoints){
			j.connectedAnchor *= scale;
		}

		var joints = GetComponentsInChildren<Joint>();
		foreach(var j in joints)
			Destroy(j);

		var rbs = GetComponentsInChildren<Rigidbody>();
		foreach(var rb in rbs)
			Destroy(rb);

		var colliders = GetComponentsInChildren<Collider>();
		foreach(var c in colliders)
			Destroy(c);


		//match by bone name
		var shadowBones = shadowRoot.GetComponentsInChildren<SkeletonUtilityBone>();
		var bones = GetComponentsInChildren<SkeletonUtilityBone>();

		//build bone lookup
		foreach(var b in bones){
			if(b.gameObject == gameObject)
				continue;

			foreach(var sb in shadowBones){
				if(sb.rigidbody == null)
					continue;

				if(sb.boneName == b.boneName){
					shadowTable.Add( sb.transform, b.transform );
					break;
				}
			}
		}

		foreach(var b in shadowBones)
			Destroy(b);
	}

	void FixedUpdate(){
		shadowRoot.rigidbody.MovePosition( transform.position );
		shadowRoot.rigidbody.MoveRotation( transform.rotation );

		foreach(var pair in shadowTable){
			pair.Value.localPosition = pair.Key.localPosition;
			pair.Value.localRotation = pair.Key.localRotation;
		}
	}
}