using Unity.Entities;
using Unity.Mathematics;
using Unity.Transforms;

namespace TheBlindEye.MovementSystem
{
    public readonly partial struct MovementAspect : IAspect
    {
        public const float CLOSE_SQ_DISTANCE_THRESHOLD = 1f;
        
        private readonly RefRW<LocalTransform> localTransform;
        private readonly RefRO<Movable> movable;
        
        private float3 MoveDirection => movable.ValueRO.TargetPosition - localTransform.ValueRO.Position;
        
        public float3 MoveVelocity => math.normalize(MoveDirection) * movable.ValueRO.MoveSpeed;
        public bool IsCloseToTarget => math.lengthsq(MoveDirection) < CLOSE_SQ_DISTANCE_THRESHOLD;

        public void RotateToDirection(float3 direction, float deltaTime)
        {
            var endRotation = quaternion.LookRotation(direction, math.up());
                
            localTransform.ValueRW.Rotation = math.slerp(localTransform.ValueRO.Rotation, endRotation,
                movable.ValueRO.RotationSpeed * deltaTime);
        }
    }
}