using UnityEngine;

namespace pdinklag
{
    /// <summary>
    /// A simple direction tester for object setup.
    /// </summary>
    public class DirectionTest : MonoBehaviour
    {
        public enum Direction { Forward, Back, Right, Left, Up, Down }

        public Direction LocalDirection = Direction.Forward;
        public Vector3 TargetPos = Vector3.zero;
        public Transform Target;
        public FieldOfView2D FOV = 90;

        public bool Test()
        {
            var testDir = LocalDirection switch
            {
                Direction.Forward => transform.forward,
                Direction.Back => -transform.forward,
                Direction.Right => transform.right,
                Direction.Left => -transform.right,
                Direction.Up => transform.up,
                Direction.Down => -transform.up,
                _ => Vector3.zero
            };

            var targetPos = Target ? Target.position : TargetPos;
            var targetDir = (targetPos - transform.position).normalized;

            return Vector3.Dot(testDir, targetDir) >= FOV.DotThreshold;
        }

        private void Start()
        {
            if (!Test())
            {
                Debug.LogWarningFormat(gameObject, "local {0} of {1} points in wrong direction!", LocalDirection, gameObject);
            }
        }
    }
}
