package com.xhpc.order.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

/**
 * 充电校验配置类
 *
 * @author yuyang
 * @Date 2026-05-19 10:17
 */
@Component
public class ChargingCheckConfig {
    @Value("${charging.check.number-threshold:9}")
    private int defaultNumberThreshold;

    @Value("${charging.check.soc-threshold:85}")
    private int defaultSocThreshold;

    @Value("${charging.check.order-power:500}")
    private int defaultOrderPower;

    @Value("${charging.check.voltage-threshold:500}")
    private int defaultVoltageThreshold;

    @Value("${charging.check.current-threshold:200}")
    private int defaultCurrentThreshold;

    // 当前使用的阈值（可能被缓存覆盖或动态修改）
    private volatile int currentNumberThreshold;
    private volatile int currentSocThreshold;
    private volatile int currentOrderPower;
    private volatile int currentVoltageThreshold;
    private volatile int currentCurrentThreshold;

    // 标记是否已从缓存加载
    private volatile boolean cacheLoaded = false;

    @PostConstruct
    public void init() {
        // 初始化时使用配置文件的值
        this.currentNumberThreshold = defaultNumberThreshold;
        this.currentSocThreshold = defaultSocThreshold;
        this.currentOrderPower = defaultOrderPower;
        this.currentVoltageThreshold = defaultVoltageThreshold;
        this.currentCurrentThreshold = defaultCurrentThreshold;
        System.out.println("充电校验配置初始化完成 - number阈值: " + currentNumberThreshold +
                ", SOC阈值: " + currentSocThreshold +
                ", 电量阈值: " + currentOrderPower +
                ", 电压阈值: " + currentVoltageThreshold +
                ", 电流阈值: " + currentCurrentThreshold + " (从配置文件读取)");
    }

    // ========== Getters ==========

    public int getNumberThreshold() {
        return currentNumberThreshold;
    }

    public int getSocThreshold() {
        return currentSocThreshold;
    }

    public int getOrderPower() {
        return currentOrderPower;
    }

    public int getVoltageThreshold() {
        return currentVoltageThreshold;
    }

    public int getCurrentThreshold() {
        return currentCurrentThreshold;
    }

    // ========== Setters ==========

    public void setNumberThreshold(int numberThreshold) {
        this.currentNumberThreshold = numberThreshold;
    }

    public void setSocThreshold(int socThreshold) {
        this.currentSocThreshold = socThreshold;
    }

    public void setOrderPower(int orderPower) {
        this.currentOrderPower = orderPower;
    }

    public void setVoltageThreshold(int voltageThreshold) {
        this.currentVoltageThreshold = voltageThreshold;
    }

    public void setCurrentThreshold(int currentThreshold) {
        this.currentCurrentThreshold = currentThreshold;
    }

    // ========== 默认值Getters ==========

    public int getDefaultNumberThreshold() {
        return defaultNumberThreshold;
    }

    public int getDefaultSocThreshold() {
        return defaultSocThreshold;
    }

    public int getDefaultOrderPower() {
        return defaultOrderPower;
    }

    public int getDefaultVoltageThreshold() {
        return defaultVoltageThreshold;
    }

    public int getDefaultCurrentThreshold() {
        return defaultCurrentThreshold;
    }

    // ========== 缓存加载 ==========

    /**
     * 从缓存加载阈值（仅在未加载时生效）
     */
    public void loadFromCache(Integer cachedNumberThreshold, Integer cachedSocThreshold,
                              Integer cachedOrderPower, Integer cachedVoltageThreshold,
                              Integer cachedCurrentThreshold) {
        if (!cacheLoaded) {
            boolean updated = false;
            if (cachedNumberThreshold != null && cachedNumberThreshold > 0) {
                this.currentNumberThreshold = cachedNumberThreshold;
                System.out.println("从缓存加载number阈值: " + cachedNumberThreshold);
                updated = true;
            }
            if (cachedSocThreshold != null && cachedSocThreshold > 0 && cachedSocThreshold <= 100) {
                this.currentSocThreshold = cachedSocThreshold;
                System.out.println("从缓存加载SOC阈值: " + cachedSocThreshold);
                updated = true;
            }
            if (cachedOrderPower != null && cachedOrderPower > 0) {
                this.currentOrderPower = cachedOrderPower;
                System.out.println("从缓存加载电量阈值: " + cachedOrderPower);
                updated = true;
            }
            if (cachedVoltageThreshold != null && cachedVoltageThreshold > 0) {
                this.currentVoltageThreshold = cachedVoltageThreshold;
                System.out.println("从缓存加载电压阈值: " + cachedVoltageThreshold);
                updated = true;
            }
            if (cachedCurrentThreshold != null && cachedCurrentThreshold > 0) {
                this.currentCurrentThreshold = cachedCurrentThreshold;
                System.out.println("从缓存加载电流阈值: " + cachedCurrentThreshold);
                updated = true;
            }
            if (updated) {
                System.out.println("当前使用阈值 - number: " + currentNumberThreshold +
                        ", SOC: " + currentSocThreshold +
                        ", 电量: " + currentOrderPower +
                        ", 电压: " + currentVoltageThreshold +
                        ", 电流: " + currentCurrentThreshold);
            }
            this.cacheLoaded = true;
        }
    }

    /**
     * 重置为配置文件默认值
     */
    public void resetToDefault() {
        this.currentNumberThreshold = defaultNumberThreshold;
        this.currentSocThreshold = defaultSocThreshold;
        this.currentOrderPower = defaultOrderPower;
        this.currentVoltageThreshold = defaultVoltageThreshold;
        this.currentCurrentThreshold = defaultCurrentThreshold;
        System.out.println("重置阈值成功 - number阈值: " + currentNumberThreshold +
                ", SOC阈值: " + currentSocThreshold +
                ", 电量阈值: " + currentOrderPower +
                ", 电压阈值: " + currentVoltageThreshold +
                ", 电流阈值: " + currentCurrentThreshold);
    }

    /**
     * 检查是否已从缓存加载
     */
    public boolean isCacheLoaded() {
        return cacheLoaded;
    }

    /**
     * 设置缓存加载标记（用于测试或特殊场景）
     */
    public void setCacheLoaded(boolean cacheLoaded) {
        this.cacheLoaded = cacheLoaded;
    }
}
