package com.sundot.airport.common.utils;

import java.util.concurrent.atomic.AtomicReference;

/**
 * 几何平均数计算工具类
 * - g1 = ∛(1 × a × b)
 * - g2 = ∛(1/a × 1 × c)
 * - g3 = ∛(1/b × 1/2 × 1/c)
 */
public class GeometricMeanUtils {


    private static final AtomicReference<WeightParams> currentParams = new AtomicReference<>(
            new WeightParams(3.0, 5.0, 2.0) // 默认值 a=3, b=5, c=2
    );

    /**
     * 权重参数内部类
     */
    private static class WeightParams {
        final double a;
        final double b;
        final double c;

        WeightParams(double a, double b, double c) {
            this.a = a;
            this.b = b;
            this.c = c;
        }
    }

    /**
     * 设置权重参数（线程安全）
     *
     * @param a 参数 a
     * @param b 参数 b
     * @param c 参数 c
     */
    public static void setParams(double a, double b, double c) {
        currentParams.set(new WeightParams(a, b, c));
    }

    /**
     * 获取当前参数 a
     *
     * @return 参数 a
     */
    public static double getParamA() {
        return currentParams.get().a;
    }

    /**
     * 获取当前参数 b
     *
     * @return 参数 b
     */
    public static double getParamB() {
        return currentParams.get().b;
    }

    /**
     * 获取当前参数 c
     *
     * @return 参数 c
     */
    public static double getParamC() {
        return currentParams.get().c;
    }

    /**
     * 计算几何平均数总和（使用当前配置的参数）
     *
     * @return 总和值
     */
    public static double calculateSum() {
        WeightParams params = currentParams.get();
        return calculateSum(params.a, params.b, params.c);
    }

    /**
     * 计算几何平均数总和
     *
     * @param a 变量 a
     * @param b 变量 b
     * @param c 变量 c
     * @return 总和值
     */
    @Deprecated
    public static double calculateSum(double a, double b, double c) {
        double g1 = Math.cbrt(1.0 * a * b);
        double g2 = Math.cbrt((1.0 / a) * 1.0 * c);
        double g3 = Math.cbrt((1.0 / b) * (1.0 / c) * 1.0);

        return g1 + g2 + g3;
    }

    /**
     * 计算几何平均数及其权重（使用当前配置的参数）
     *
     * @return 包含总和和各权重的计算结果
     */
    public static CalculationResult calculateWithWeights() {
        WeightParams params = currentParams.get();
        return calculateWithWeights(params.a, params.b, params.c);
    }

    /**
     * 计算几何平均数及其权重
     *
     * @param a 变量 a
     * @param b 变量 b
     * @param c 变量 c
     * @return 包含总和和各权重的计算结果
     */
    @Deprecated
    public static CalculationResult calculateWithWeights(double a, double b, double c) {
        double g1 = Math.cbrt(1.0 * a * b);
        double g2 = Math.cbrt((1.0 / a) * 1.0 * c);
        double g3 = Math.cbrt((1.0 / b) * (1.0 / c) * 1.0);

        double total = g1 + g2 + g3;
        double w1 = total > 0 ? g1 / total : 0;
        double w2 = total > 0 ? g2 / total : 0;
        double w3 = total > 0 ? g3 / total : 0;

        return new CalculationResult(total, g1, g2, g3, w1, w2, w3);
    }

    /**
     * 计算结果封装类
     */
    public static class CalculationResult {
        private final double total;
        private final double g1;
        private final double g2;
        private final double g3;
        private final double w1;
        private final double w2;
        private final double w3;

        public CalculationResult(double total, double g1, double g2, double g3, double w1, double w2, double w3) {
            this.total = total;
            this.g1 = g1;
            this.g2 = g2;
            this.g3 = g3;
            this.w1 = w1;
            this.w2 = w2;
            this.w3 = w3;
        }

        public double getTotal() {
            return total;
        }

        public double getG1() {
            return g1;
        }

        public double getG2() {
            return g2;
        }

        public double getG3() {
            return g3;
        }

        public double getW1() {
            return w1;
        }

        public double getW2() {
            return w2;
        }

        public double getW3() {
            return w3;
        }

        @Override
        public String toString() {
            return String.format("总和: %.6f, g1: %.6f, g2: %.6f, g3: %.6f, w1: %.6f, w2: %.6f, w3: %.6f",
                    total, g1, g2, g3, w1, w2, w3);
        }
    }
}