//Created by Neodrop
//neodrop@unity3d.ru
using UnityEngine;
//#pragma warning disable

public class CsBipedBone : MonoBehaviour {
    public CsBiped biped;
    public bool drawLine = true, containBip;

    void OnDrawGizmos()
    {
        FindBiped();
        if (!biped)
        {
            DestroyImmediate(this);
            return;
        }
        if (!biped.showBones) return;

        Gizmos.color = biped.colorBone;
        DrawSingleBone(transform);
    }

    void OnDrawGizmosSelected()
    {
        FindBiped();
        if (!biped)
        {
            DestroyImmediate(this);
            return;
        }
        if (!biped.showBones) return;

        Gizmos.color = biped.colorSelected;
        DrawSingleBone(transform);
    }

    void DrawSingleBone(Transform bone)
    {
        if (containBip)
        {
            if (Vector3.Distance(bone.position, bone.parent.position) < .1f)
                Gizmos.DrawSphere(bone.position, .005f);
            else Gizmos.DrawSphere(bone.position, .01f);

            if (drawLine) Gizmos.DrawLine(bone.position, bone.parent.position);
        }
    }

    void DrawSingleBoneWired(Transform bone)
    {
        if (containBip)
        {
            if (Vector3.Distance(bone.position, bone.parent.position) < .1f)
                Gizmos.DrawWireSphere(bone.position, .005f);
            else Gizmos.DrawWireSphere(bone.position, .01f);

            if (drawLine) Gizmos.DrawLine(bone.position, bone.parent.position);
        }
    }

    CsBiped FindBiped()
    {
        if (biped != null) return biped;
        biped = transform.root.GetComponent<CsBiped>();
        drawLine = !gameObject.name.Contains("Footsteps");
        containBip = transform.parent != null && transform.parent.gameObject.name.Contains("Bip");
        if (!containBip) containBip = transform.parent != null && transform.parent.gameObject.name.Contains("Bone");
        return biped;
    }
}
