package com.usky.vpp.util;

import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

/**
 * 虚拟电厂 RegisterReport 量测点定义（DL/T 1867—2024 表31 MetricName）。
 */
public final class VppUnRegisterMetricDefinitions {

    private static final String MULTIPLIER_K = "k";

    public static final class RegisterMetric {
        private final String metricName;
        /** SI 前缀倍数，无倍数时为 null（JSON 中省略 multiplier）。 */
        private final String multiplier;
        private final String symbol;

        public RegisterMetric(String metricName, String multiplier, String symbol) {
            this.metricName = metricName;
            this.multiplier = multiplier;
            this.symbol = symbol;
        }

        public String getMetricName() {
            return metricName;
        }

        public String getMultiplier() {
            return multiplier;
        }

        public String getSymbol() {
            return symbol;
        }
    }

    /** 站点/资源点注册的有功功率量测点（kW）。 */
    public static final RegisterMetric ACTIVE_POWER = new RegisterMetric("AP", MULTIPLIER_K, "W");

    /** 虚拟电厂负荷预测（kW）。 */
    public static final RegisterMetric LOAD_FORECAST = new RegisterMetric("LOAD_FORECAST_XNDC", MULTIPLIER_K, "W");

    /** 虚拟电厂需注册的量测点（含负荷预测），resourceID 统一为 dnID。 */
    public static final List<RegisterMetric> VPP_REGISTER_METRICS = Collections.unmodifiableList(Arrays.asList(
            ACTIVE_POWER,
            new RegisterMetric("AP_E", MULTIPLIER_K, "Wh"),
            new RegisterMetric("AP_PE", MULTIPLIER_K, "Wh"),
            new RegisterMetric("REGULATE_UP", MULTIPLIER_K, "W"),
            new RegisterMetric("REGULATE_DOWN", MULTIPLIER_K, "W"),
            new RegisterMetric("RESPONSE_TIME", null, "s"),
            new RegisterMetric("CONTROL_RATE", MULTIPLIER_K, "W/min"),
            new RegisterMetric("DURATION", null, "min"),
            new RegisterMetric("F_REGULATE_UP", MULTIPLIER_K, "W"),
            new RegisterMetric("F_REGULATE_DOWN", MULTIPLIER_K, "W"),
            new RegisterMetric("F_RESPONSE_TIME", null, "s"),
            new RegisterMetric("F_CONTROL_RATE", MULTIPLIER_K, "W/min"),
            new RegisterMetric("F_DURATION", null, "min"),
            LOAD_FORECAST
    ));

    /** MomentData 虚拟电厂维度：实时类 8 项（不含日前申报与负荷预测）。 */
    public static final Set<String> VPP_MOMENT_METRIC_NAMES = Collections.unmodifiableSet(new LinkedHashSet<>(Arrays.asList(
            "AP", "AP_E", "AP_PE",
            "REGULATE_UP", "REGULATE_DOWN", "RESPONSE_TIME", "CONTROL_RATE", "DURATION"
    )));

    /** IntervalData 虚拟电厂维度：基础 3 项 + 日前申报 5 项 + 负荷预测。 */
    public static final Set<String> VPP_INTERVAL_METRIC_NAMES = Collections.unmodifiableSet(new LinkedHashSet<>(Arrays.asList(
            "AP", "AP_E", "AP_PE",
            "F_REGULATE_UP", "F_REGULATE_DOWN", "F_RESPONSE_TIME", "F_CONTROL_RATE", "F_DURATION",
            "LOAD_FORECAST_XNDC"
    )));

    public static boolean isLoadForecast(RegisterMetric metric) {
        return metric != null && LOAD_FORECAST.getMetricName().equals(metric.getMetricName());
    }

    public static boolean isMomentMetric(RegisterMetric metric) {
        return metric != null && VPP_MOMENT_METRIC_NAMES.contains(metric.getMetricName());
    }

    public static boolean isIntervalMetric(RegisterMetric metric) {
        return metric != null && VPP_INTERVAL_METRIC_NAMES.contains(metric.getMetricName());
    }

    private VppUnRegisterMetricDefinitions() {
    }
}
