package examples.extractor;

import java.util.Map;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
public class ExtractorUtils {

    public static Double division(String s1, String s2, Map<String, String> maps){
        double rate = 0.0;
        if (maps.containsKey(s1) && maps.containsKey(s2)){
            Double d1 = Double.valueOf(maps.get(s1));
            if (isDoubleEqualToZero(d1)){
                return rate;
            }
            Double d2 = Double.valueOf(maps.get(s2));
            rate = d2 / d1;
        }
        return rate;
    }
    public static Double divisionDouble(Double d1, Double d2){
        double rate = 0.0;
        if (isDoubleEqualToZero(d1)){
            return rate;
        }
        rate = d2 / d1;
        return rate;
    }

    public static boolean isDoubleEqualToZero(double value) {
        final double epsilon = 1e-10; // 定义一个很小的误差范围
        // 判断value是否在误差范围内
        return Math.abs(value) < epsilon;
    }



    public static double calculateVariance(List<Double> numbers) {
        double average = numbers.stream()
                .mapToDouble(Double::doubleValue)
                .average()
                .orElse(0.0);

        double squaredDiffSum = numbers.stream()
                .mapToDouble(Double::doubleValue)
                .map(x -> Math.pow(x - average, 2))
                .average()
                .orElse(0.0);

        return squaredDiffSum;
    }

    public static double calculateAverage(List<Double> numbers) {
        if (numbers == null || numbers.isEmpty()) {
            return 0.0;
        }
        return numbers.stream()
                .mapToDouble(Number::doubleValue)
                .average()
                .orElse(0.0);
    }

    public static List<Double> calculateDifferences(List<Double> numbers) {
        List<Double> differences = new ArrayList<>();

        for (int i = 0; i < numbers.size() - 1; i++) {
            Double diff = 0.0;
            if (!isDoubleEqualToZero(numbers.get(i))){
                diff = (numbers.get(i + 1) - numbers.get(i)) / numbers.get(i);
            }
            differences.add(diff);
        }

        return differences;
    }

    public static List<String> generateHourStrings(String timeString, int N) {
        LocalDateTime dateTime = LocalDateTime.parse(timeString, DateTimeFormatter.ofPattern("yyyyMMddHH"));
        List<String> hourStrings = new ArrayList<>();
        for (int i = 0; i < N; i++) {
            hourStrings.add(dateTime.minusHours(i).format(DateTimeFormatter.ofPattern("yyyyMMddHH")));
        }

        return hourStrings;
    }

    public static String subtractHours(String inputDateTime, int hoursToSubtract) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMddHH");
        LocalDateTime dateTime = LocalDateTime.parse(inputDateTime, formatter);
        LocalDateTime subtractedDateTime = dateTime.minusHours(hoursToSubtract);
        return subtractedDateTime.format(formatter);
    }

    // 针对0-1的数字，进行分桶。
    public static Integer ceilLogRate(Double key) {
        double bucket = Math.ceil(
                Math.pow(key, 0.2) * 100
        );
        if (bucket > 300) {
            bucket = 300;
        }
        if (bucket < 0) {
            bucket = 0;
        }
        return (int)bucket;
    }

    // 针对大于1的数字，进行分桶。
    public static int bucketCnt(Double key) {
        long bucket = Math.round(Math.log((key * 10 + 1.0)) * 10);
        if (bucket > 300) {
            bucket = 300;
        }
        if (bucket < 0) {
            bucket = 0;
        }
        return (int)bucket;
    }

    public static int findInsertPosition(double[] sortedArray, double target) {
        int low = 0;
        int high = sortedArray.length - 1;

        while (low <= high) {
            int mid = low + (high - low) / 2;
            double midValue = sortedArray[mid];

            if (midValue < target) {
                low = mid + 1;
            } else if (midValue > target) {
                high = mid - 1;
            } else {
                // 找到相等的值，尝试在右侧寻找插入点
                while (mid < sortedArray.length - 1 && sortedArray[mid + 1] == target) {
                    mid++;
                }
                return mid + 1; // 返回当前mid的下一个位置作为插入点
            }
        }

        return low; // 返回low作为插入点
    }

    public static void main(String[] args) {
        double[] sortedArray = {1.0, 2.0, 4.0, 4.0, 6.0};
        double target = 0.0;
        System.out.println(findInsertPosition(sortedArray, target));


//        System.out.println(ceilLogRate(0.0002));
//        System.out.println(ceilLogRate(0.01));
//        System.out.println(ceilLogRate(0.2));
//        System.out.println(ceilLogRate(4.));
//        System.out.println(bucketCnt(1.));
//        System.out.println(bucketCnt(20.));
//        System.out.println(bucketCnt(500.));
//        System.out.println(bucketCnt(50000.));

//        System.out.println(generateHourStrings("2024011603", 5));

    }

}
