package com.ruoyi.system.util;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class CalculateUtil {

    private static final Pattern SPEED_PATTERN = Pattern.compile("(\\d+\\.?\\d*)\\s*(\\S*)");
    /**
     * 单位换算
     * @param speedWithUnit
     * @return
     */
    public static BigDecimal parseSpeedToMbps(String speedWithUnit, String mode) {
        if (speedWithUnit == null || speedWithUnit.trim().isEmpty()) {
            return BigDecimal.ZERO;
        }

        Matcher matcher = SPEED_PATTERN.matcher(speedWithUnit.trim());
        if (!matcher.find()) {
            throw new IllegalArgumentException("Invalid speed format: " + speedWithUnit);
        }

        BigDecimal value = new BigDecimal(matcher.group(1));
        String unit = matcher.group(2).toUpperCase();

        // 处理纯数字（单位为空）
        if (unit.isEmpty()) {
            unit = "B/S"; // 默认单位: Bit/s
        }
        switch (mode) {
            case "1000":
                return convertWithDecimalCoefficients(value, unit);
            case "1024":
                return convertWithBinaryCoefficients(value, unit);
            default:
                throw new UnsupportedOperationException("Unknown mode: " + mode);
        }
    }
    // 1000进制换算（最小单位为bit）
    private static BigDecimal convertWithDecimalCoefficients(BigDecimal value, String unit) {
        switch (unit) {
            case "B/S":  // bit per second
                return value.divide(new BigDecimal("1000000000"), 2, RoundingMode.HALF_UP);  // 10^9
            case "KB/S": // Kilobit per second
                return value.divide(new BigDecimal("1000000"), 2, RoundingMode.HALF_UP);     // 10^6
            case "MB/S": // Megabit per second
                return value.divide(new BigDecimal("1000"), 2, RoundingMode.HALF_UP);        // 10^3
            case "GB/S": // Gigabit per second
                return value.setScale(2, RoundingMode.HALF_UP);                              // 10^0
            case "TB/S": // Terabit per second
                return value.multiply(new BigDecimal("1000"))                                // 10^3
                        .setScale(2, RoundingMode.HALF_UP);
            default:
                throw new IllegalArgumentException("Unsupported DECIMAL unit: " + unit);
        }
    }
    // 1024进制换算（最小单位为bit）
    private static BigDecimal convertWithBinaryCoefficients(BigDecimal value, String unit) {
        BigDecimal base1024 = new BigDecimal(1024);
        BigDecimal base1048576 = base1024.multiply(base1024); // 1024^2 = 1048576
        BigDecimal base1073741824 = base1048576.multiply(base1024); // 1024^3 = 1073741824

        switch (unit) {
            case "B/S":  // bit per second
                return value.divide(base1073741824, 2, RoundingMode.HALF_UP);  // 1024^3
            case "KB/S": // Kilobit per second
                return value.divide(base1048576, 2, RoundingMode.HALF_UP);     // 1024^2
            case "MB/S": // Megabit per second
                return value.divide(base1024, 2, RoundingMode.HALF_UP);        // 1024^1
            case "GB/S": // Gigabit per second
                return value.setScale(2, RoundingMode.HALF_UP);                // 1024^0
            case "TB/S": // Terabit per second
                return value.multiply(base1024)                                // 1024^1
                        .setScale(2, RoundingMode.HALF_UP);
            default:
                throw new IllegalArgumentException("Unsupported BINARY unit: " + unit);
        }
    }
    // 字节1000进制换算
//    private static BigDecimal convertWithDecimalCoefficients(BigDecimal value, String unit) {
//        switch (unit.toUpperCase()) {
//            case "B/S":
//                return value.multiply(new BigDecimal("0.000008"))  // 8/1000000
//                        .setScale(2, RoundingMode.HALF_UP);
//            case "KB/S":
//                return value.multiply(new BigDecimal("0.008"))     // 8/1000
//                        .setScale(2, RoundingMode.HALF_UP);
//            case "MB/S":
//                return value.setScale(2, RoundingMode.HALF_UP);
//            case "GB/S":
//                return value.multiply(new BigDecimal("8000"))      // 1000 * 8
//                        .setScale(2, RoundingMode.HALF_UP);
//            case "TB/S":
//                return value.multiply(new BigDecimal("8000000"))  // 1000 * 1000 * 8
//                        .setScale(2, RoundingMode.HALF_UP);
//            default:
//                throw new IllegalArgumentException("Unsupported DECIMAL unit: " + unit);
//        }
//    }
//
//    // 字节1024进制换算
//    private static BigDecimal convertWithBinaryCoefficients(BigDecimal value, String unit) {
//        switch (unit.toUpperCase()) {
//            case "B/S":
//                return value.multiply(new BigDecimal("0.00000762939"))  // 8/1048576
//                        .setScale(2, RoundingMode.HALF_UP);
//            case "KB/S":
//                return value.multiply(new BigDecimal("0.0078125"))     // 8/1024
//                        .setScale(2, RoundingMode.HALF_UP);
//            case "MB/S":
//                return value.setScale(2, RoundingMode.HALF_UP);
//            case "GB/S":
//                return value.multiply(new BigDecimal("8192"))          // 1024 * 8
//                        .setScale(2, RoundingMode.HALF_UP);
//            case "TB/S":
//                return value.multiply(new BigDecimal("8388608"))       // 1024 * 1024 * 8
//                        .setScale(2, RoundingMode.HALF_UP);
//            default:
//                throw new IllegalArgumentException("Unsupported BINARY unit: " + unit);
//        }
//    }

    /**
     * 计算百分位值
     */
    public static BigDecimal calculatePercentile(List<BigDecimal> sortedValues, double percentile) {
        int total = sortedValues.size();
        int position = (int) Math.ceil(percentile * total) - 1;
        position = Math.max(0, Math.min(position, total - 1));
        return sortedValues.get(position).setScale(2, RoundingMode.HALF_UP);
    }

    /**
     * 计算95值位置 从大到小
     * @param sortedValues
     * @param percentile
     * @return
     */
    public static int calculate95Index(List<?> sortedValues, double percentile) {
        int total = sortedValues.size();
        int position = (int) Math.ceil(percentile * total) - 1;
        position = Math.max(0, Math.min(position, total - 1));
        return position;
    }

    /**
     * 根据带宽类型设置时间范围（修复T分隔符问题）
     */
    public static String getTypeByBandwidthType(String bandwidthType) {
        switch (bandwidthType) {
            case "1":
            case "3":
            case "5": // 日带宽
                return "1";
            case "2":
            case "4":
            case "6":
            case "7": // 月带宽
                return "2";
            default:
                throw new IllegalArgumentException("不支持的带宽类型: " + bandwidthType);
        }
    }

    /**
     * 判断是否为有效
     * @param list
     * @return
     */
    public static Boolean isEff(List<?> list){
        if(!list.isEmpty()){
            if(list.size()<288){
                return true;
            }
        }
        return false;
    }
}
