using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Fuel.Core
{
    public class DistanceCalculator
    {
        // 地球半径（单位：公里）
        private const double EarthRadiusKm = 6371.0;

        /// <summary>
        /// 使用Haversine公式计算两点之间的距离。
        /// </summary>
        /// <param name="lat1">第一个点的纬度。</param>
        /// <param name="lon1">第一个点的经度。</param>
        /// <param name="lat2">第二个点的纬度。</param>
        /// <param name="lon2">第二个点的经度。</param>
        /// <returns>两点之间的距离（单位：公里）。</returns>
        public static double CalculateDistance(double lat1, double lon1, double lat2, double lon2)
        {
            var lat1Rad = DegreesToRadians(lat1);
            var lat2Rad = DegreesToRadians(lat2);
            var deltaLat = DegreesToRadians(lat2 - lat1);
            var deltaLon = DegreesToRadians(lon2 - lon1);

            var a = Math.Sin(deltaLat / 2) * Math.Sin(deltaLat / 2) +
                    Math.Cos(lat1Rad) * Math.Cos(lat2Rad) *
                    Math.Sin(deltaLon / 2) * Math.Sin(deltaLon / 2);
            var c = 2 * Math.Atan2(Math.Sqrt(a), Math.Sqrt(1 - a));

            return EarthRadiusKm * c; // 返回结果为公里
        }

        private static double DegreesToRadians(double degrees)
        {
            return degrees * (Math.PI / 180);
        }
    }
}
