package com.muyu.utils;


import com.muyu.demos.model.PositionModel;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Random;

public class VehicleUtils {

    /**
     * 生成VIN
     * @return 返回结果对象
     */
    public static String genVin() {
        String characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        Random random = new Random();
        StringBuilder sb = new StringBuilder(17);
        for (int i = 0; i < 17; i++) {
            int index = (int) (random.nextFloat() * characters.length());
            sb.append(characters.charAt(index));
        }
        return sb.toString();
    }

    private static Random random = new Random();

    /**
     * 生成电池额度
     * @return 电池额度
     */
    public static BigDecimal genBattery(){
        return BigDecimal.valueOf(random.nextInt(60, 80) * 1000L);
    }


    /**
     * 两点之间的距离
     * @param startPositionModel 开始定位点
     * @param endPositionModel 结束定位点
     * @return
     */
    public static BigDecimal distance(PositionModel startPositionModel, PositionModel endPositionModel){
        double lon1 = Double.parseDouble(startPositionModel.getLongitude());
        double lat1 = Double.parseDouble(startPositionModel.getLatitude());
        double lon2 = Double.parseDouble(endPositionModel.getLongitude());
        double lat2 = Double.parseDouble(endPositionModel.getLatitude());
        double lon1Rad = Math.toRadians(lon1);
        double lat1Rad = Math.toRadians(lat1);
        double lon2Rad = Math.toRadians(lon2);
        double lat2Rad = Math.toRadians(lat2);
        double earthRadius = 6371; // 地球半径（以公里为单位）

        double latDiff = lat2Rad - lat1Rad;
        double lonDiff = lon2Rad - lon1Rad;

        double a = Math.sin(latDiff / 2) * Math.sin(latDiff / 2) +
                Math.cos(lat1Rad) * Math.cos(lat2Rad) *
                        Math.sin(lonDiff / 2) * Math.sin(lonDiff / 2);

        double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));

        double distance = earthRadius * c;
        return BigDecimal.valueOf(distance).setScale(2, RoundingMode.HALF_UP);
    }

    /**
     * 生成电池浮动
     * @return 电池浮动值
     */
    public static BigDecimal batteryFloat(){
        Random rand = new Random();
        // 生成0.00-0.31之间的随机数
        double num = rand.nextDouble() * 0.31;
        // 加上0.90，得到0.90-1.21之间的随机数
        num += 0.90;
        // 保留两位小数
        num = (double) Math.round(num * 100) / 100;
        return BigDecimal.valueOf(num);
    }

    /**
     * 给予开始和结束的值生成数据
     * @param start 起始范围
     * @param end 截止范围
     * @return 返回结果
     */
    public static String genValue(int start, int end){
        Random rand = new Random();
        return String.valueOf(rand.nextInt(start, end));
    }
}
