using System;
using System.Collections.Generic;
using Sirenix.OdinInspector;
using Unity.AI.Navigation;
using UnityEngine;
using UnityEngine.AI;
using VOID.Generic.Navigation;

namespace VOID.Generic.Objects.Traversable
{
    [Serializable]
    public class TraversableObjectData : ObjectComponent<TraversableObject>
    {
        #region Initialize

        protected override void Initialize()
        {
            navMeshLink = parent.GetComponent<NavMeshLink>();
            
            // Find traversable's FIRST link point
            var startPos = navMeshLink.startTransform.position + navMeshLink.startPoint;
            NavMesh.SamplePosition(startPos, out var hitStart, 0.5f, NavMesh.AllAreas);
            linkStartPosition = hitStart.position;
            
            // Find traversable's SECOND link point
            var endPos = navMeshLink.endTransform.position + navMeshLink.endPoint;
            NavMesh.SamplePosition(endPos, out var hitEnd, 0.5f, NavMesh.AllAreas);
            linkEndPosition = hitEnd.position;
            
            // ..And register it in navigation manager
            NavigationManager.current.RegisterTraversable(parent);
        }

        #endregion

        [Title("Pathing")]
        [RequiredListLength(MinLength = 2)]
        public List<Transform> path;

        // Runtime cache
        [ShowInInspector] [ReadOnly] public NavMeshLink navMeshLink { get; private set; }
        [ShowInInspector] [ReadOnly] public Vector3 linkStartPosition { get; private set; }
        [ShowInInspector] [ReadOnly] public Vector3 linkEndPosition { get; private set; }
    }
}
