package cn.iocoder.yudao.module.rescue.utils;

import java.util.List;

public class DistanceCalculator {
    private static final double EARTH_RADIUS = 6371; // 地球平均半径，单位为千米

    public static double calculateDistance(double startLat, double startLng, double endLat, double endLng) {
        // 将经纬度转换为弧度
        double radStartLat = Math.toRadians(startLat);
        double radEndLat = Math.toRadians(endLat);
        double radDiffLat = Math.toRadians(endLat - startLat);
        double radDiffLng = Math.toRadians(endLng - startLng);

        // 使用Haversine公式计算距离
        double a = Math.sin(radDiffLat / 2) * Math.sin(radDiffLat / 2) +
                Math.cos(radStartLat) * Math.cos(radEndLat) *
                        Math.sin(radDiffLng / 2) * Math.sin(radDiffLng / 2);
        double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
        double distance = EARTH_RADIUS * c;

        return distance;
    }

    public static double calculateTotalDistance(List<Double> latitudes, List<Double>  longitudes) {
        double totalDistance = 0.0;

        if (latitudes.size() != longitudes.size() || latitudes.size() < 2) {
            return totalDistance; // 若提供的经纬度点数量不足或不匹配，则总距离为0
        }

        for (int i = 1; i < latitudes.size(); i++) {
            double startLat = latitudes.get(i-1);
            double startLng = longitudes.get(i-1);
            double endLat = latitudes.get(i);
            double endLng = longitudes.get(i);

            double segmentDistance = calculateDistance(startLat, startLng, endLat, endLng);
            totalDistance += segmentDistance;
        }

        return totalDistance;
    }

    public static void main(String[] args) {
    }
}