using System;
using UnityEngine;

namespace pdinklag.CPW.Common
{
    /// <summary>
    /// Defines a measurement of a body defined as the distance between two POIs.
    /// </summary>
    public class BodyMeasure : MonoBehaviour
    {
        [Header("Definition")]
        [Tooltip("The first reference point.")]
        public BodyPOI Point1;

        [Tooltip("The second reference point.")]
        public BodyPOI Point2;

        [Header("Precomputed Values")]
        [Tooltip("The distance between the reference points on a reference body.")]
        public float Distance;

#if UNITY_EDITOR
        [NonSerialized]
        public BodyDescription RefBody; // nb: used for editing
#endif

        /// <summary>
        /// Computes the distance for the given target body relative to the reference body.
        /// </summary>
        /// <param name="body">the target body</param>
        public float Aspect(BodyDescription body) => ComputeDistance(body) / Distance;

        /// <summary>
        /// Computes the distance for a given body.
        /// </summary>
        /// <param name="body">the body in question</param>
        public float ComputeDistance(BodyDescription body) => Vector3.Distance(body.GetPOI(Point1), body.GetPOI(Point2));

#if UNITY_EDITOR
        private void OnDrawGizmosSelected()
        {
            if (RefBody)
            {
                Gizmos.color = Color.yellow;
                Gizmos.DrawLine(RefBody.GetPOI(Point1), RefBody.GetPOI(Point2));
            }
        }
#endif
    }
}