package com.tzld.piaoquan.recommend.server.util;

import org.apache.commons.lang3.StringUtils;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

/**
 * @author sunxy
 */
public class ProbabilityCalculator {

    static class DateRange {
        LocalDateTime start;
        LocalDateTime end;

        DateRange(LocalDateTime start, LocalDateTime end) {
            this.start = start;
            this.end = end;
        }
    }

    static class RateRange {
        double start;
        double end;

        RateRange(double start, double end) {
            this.start = start;
            this.end = end;
        }
    }

    public static double calculateCurrentProbability(String filterRate, String filterDate) {
        if (StringUtils.isBlank(filterRate) || StringUtils.isBlank(filterDate)) {
            return -1;
        }
        // 解析日期区间
        String[] dateRanges = filterDate.split(",");
        List<DateRange> parsedDateRanges = new ArrayList<>();
        for (String range : dateRanges) {
            String[] dates = range.split("-");
            if (dates.length != 2) {
                continue;
            }
            LocalDateTime start = LocalDateTime.parse(dates[0], DateTimeFormatter.ofPattern("yyyyMMddHH"));
            LocalDateTime end = LocalDateTime.parse(dates[1], DateTimeFormatter.ofPattern("yyyyMMddHH"));
            parsedDateRanges.add(new DateRange(start, end));
        }

        // 解析概率区间
        String[] rateRanges = filterRate.split(",");
        List<RateRange> parsedRateRanges = new ArrayList<>();
        for (String range : rateRanges) {
            if (range.split("-").length != 2) {
                continue;
            }
            String[] rates = range.split("-");
            double start = Double.parseDouble(rates[0]);
            double end = Double.parseDouble(rates[1]);
            parsedRateRanges.add(new RateRange(start, end));
        }

        // 获取当前时间
        LocalDateTime now = LocalDateTime.now();

        // 确定当前时间属于哪个日期区间，并计算概率
        for (int i = 0; i < parsedDateRanges.size(); i++) {
            DateRange dateRange = parsedDateRanges.get(i);
            if (now.isEqual(dateRange.start) || (now.isAfter(dateRange.start) && now.isBefore(dateRange.end))) {
                RateRange rateRange = parsedRateRanges.get(i);
                long totalMinutes = dateRange.end.atZone(java.time.ZoneId.systemDefault()).toEpochSecond() - dateRange.start.atZone(java.time.ZoneId.systemDefault()).toEpochSecond();
                long elapsedMinutes = now.atZone(java.time.ZoneId.systemDefault()).toEpochSecond() - dateRange.start.atZone(java.time.ZoneId.systemDefault()).toEpochSecond();
                double elapsedRatio = (double) elapsedMinutes / totalMinutes;
                return rateRange.start + ((rateRange.end - rateRange.start) * elapsedRatio);
            }
        }

        // 如果当前时间不在任何配置的日期区间内，返回-1表示无效
        return -1;
    }

    public static void main(String[] args) {
        double probability = calculateCurrentProbability("0.8-1.0,0.5-0.6", "2024020312-2024020400," +
                "2024020400-2024020702");
        System.out.println("Current Probability: " + probability);
    }

}
