package com.aidex.common.utils.dist;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Duration;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;

public class CalcDist {

    private double EARTH_RADIUS = 6378.137;

    public static double calculateDistance(double lat1, double lon1, double lat2, double lon2) {
        double earthRadius = 6378137; // 地球半径，单位为米

        double lat1Rad = Math.toRadians(lat1);
        double lon1Rad = Math.toRadians(lon1);
        double lat2Rad = Math.toRadians(lat2);
        double lon2Rad = Math.toRadians(lon2);

        double deltaLat = lat2Rad - lat1Rad;
        double deltaLon = lon2Rad - lon1Rad;

        double a = Math.sin(deltaLat / 2) * Math.sin(deltaLat / 2) +
                Math.cos(lat1Rad) * Math.cos(lat2Rad) *
                        Math.sin(deltaLon / 2) * Math.sin(deltaLon / 2);
        double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));

        double distance = earthRadius * c;
        return distance;
    }


    public static Long calcStopTime(String stopTime){
        // 当前时间
        LocalDateTime now = LocalDateTime.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        //  停船时间
        LocalDateTime later = LocalDateTime.parse(stopTime,formatter);
        // 计算两个时间之间的差值
        Duration duration = Duration.between(now, later);
        Long minutesBetween = duration.toMinutes();
        return minutesBetween < 0?0:minutesBetween;
    }

    private static double rad(double d)
    {
        return d * Math.PI / 180.0;
    }
    public static double gps2d(double lat_a, double lng_a, double lat_b, double lng_b)
    {
        double d = 0;
        lat_a=lat_a*Math.PI/180;
        lng_a=lng_a*Math.PI/180;
        lat_b=lat_b*Math.PI/180;
        lng_b=lng_b*Math.PI/180;

        d=Math.sin(lat_a)*Math.sin(lat_b)+Math.cos(lat_a)*Math.cos(lat_b)*Math.cos(lng_b-lng_a);
        d=Math.sqrt(1-d*d);
        d=Math.cos(lat_b)*Math.sin(lng_b-lng_a)/d;
        d=Math.asin(d)*180/Math.PI;
        return d;
    }
}
