package cn.com.taiji.utils;

import cn.com.taiji.entity.Location;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * @author chenfangchao
 * @title: LocationUtils
 * @projectName es-track-analysis
 * @description: TODO
 * @date 2023/2/7 5:40 PM
 */
public class LocationUtils {

    public static List<Location> generateLogLat(String startTime, String endTime, Double startLog, Double startLat, Double endLog, Double endLat) {
        List<Location> list = new ArrayList<>();
        try {
            final int seconds = 1000;
            //经纬度差
            Double difflog = Math.abs(startLog - endLog);
            Double difflat = Math.abs(startLat - endLat);
            //相差秒数
            long diffSeconds = diffSeconds(startTime, endTime);
            //平均经纬度差
            Double avgLog = difflog / diffSeconds;
            Double avgLat = difflat / diffSeconds;
            //开始时间时间戳
            long startTimeStamp = dateToStamp(startTime);
            long endTimeStamp = dateToStamp(endTime);
            Double resLog = startLog;
            Double resLat = startLat;
            long startTimeStampNew = startTimeStamp + seconds;
            //判断时间大小关系
            if (startTimeStampNew < endTimeStamp) {
                for (long i = startTimeStamp + seconds; i < endTimeStamp; i += seconds) {
                    resLog = avgLog + resLog;
                    resLat = avgLat + resLat;
                    double bdLog = doubleDown(resLog);
                    double bdLat = doubleDown(resLat);
                    Location location = new Location(bdLog,bdLat);
                    list.add(location);
                }
            } else {
                for (long i = endTimeStamp + seconds; i < startTimeStampNew; i += seconds) {
                    resLog = avgLog + resLog;
                    resLat = avgLat + resLat;
                    double bdLog = doubleDown(resLog);
                    double bdLat = doubleDown(resLat);
                    Location location = new Location(bdLog,bdLat);
                    list.add(location);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }


    public static double doubleDown(Double data) {
        try {
            BigDecimal bd = new BigDecimal(data);
            double res = bd.setScale(7, BigDecimal.ROUND_DOWN).doubleValue();
            return res;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    public static long dateToStamp(String time) {
        Date date = DateUtil.parse(time);
        long ts = date.getTime();
        return ts;
    }

    public static long diffSeconds(String startTime,String endTime){
        Date startDate= DateUtil.parse(startTime);
        Date endDate=DateUtil.parse(endTime);
        long second=DateUtil.between(startDate,endDate, DateUnit.SECOND);
        return second;
    }
}