package com.usky.cdi.service.util;

import java.time.LocalDate;
import java.time.Month;
import java.util.Random;
/**
 *
 * @author fyc
 * @email yuchuan.fu@chinausky.com
 * @date 2026/4/23
 */

/**
 * 上海地区 地下室温湿度常量 + 四季判断工具
 */
public class BasementClimateUtil {

    private static final Random RANDOM = new Random();

    // ====================== 春季（3-5月）======================
    public static final double SPRING_TEMP_MIN = 16.0;
    public static final double SPRING_TEMP_MAX = 20.0;
    public static final double SPRING_HUMI_MIN = 70.0;
    public static final double SPRING_HUMI_MAX = 74.0;

    // ====================== 夏季（6-8月）======================
    public static final double SUMMER_TEMP_MIN = 22.0;
    public static final double SUMMER_TEMP_MAX = 26.0;
    public static final double SUMMER_HUMI_MIN = 80.0;
    public static final double SUMMER_HUMI_MAX = 84.0;

    // ====================== 秋季（9-11月）=====================
    public static final double AUTUMN_TEMP_MIN = 17.0;
    public static final double AUTUMN_TEMP_MAX = 21.0;
    public static final double AUTUMN_HUMI_MIN = 68.0;
    public static final double AUTUMN_HUMI_MAX = 72.0;

    // ====================== 冬季（12-1-2月）===================
    public static final double WINTER_TEMP_MIN = 12.0;
    public static final double WINTER_TEMP_MAX = 16.0;
    public static final double WINTER_HUMI_MIN = 60.0;
    public static final double WINTER_HUMI_MAX = 64.0;

    /**
     * 获取当前系统时间的季节
     * 0=春 1=夏 2=秋 3=冬
     */
    public static int getSeason() {
        return getSeason(LocalDate.now());
    }

    /**
     * 根据日期判断季节（JDK8 标准写法）
     */
    public static int getSeason(LocalDate date) {
        Month month = date.getMonth();
        int monthValue = month.getValue();

        if (monthValue >= 3 && monthValue <= 5) {
            return 0; // 春
        } else if (monthValue >= 6 && monthValue <= 8) {
            return 1; // 夏
        } else if (monthValue >= 9 && monthValue <= 11) {
            return 2; // 秋
        } else {
            return 3; // 冬（12、1、2月）
        }
    }

    /**
     * 获取当前季节的温度范围 [最小值, 最大值]
     */
    public static double getCurrentTempRange() {
        return randomBetween(getTempRange(getSeason())[0], getTempRange(getSeason())[1]);
    }

    /**
     * 获取当前季节的湿度范围 [最小值, 最大值]
     */
    public static double getCurrentHumiRange() {
        return randomBetween(getHumiRange(getSeason())[0], getHumiRange(getSeason())[1]);
    }

    /**
     * 根据季节获取温度范围
     */
    public static double[] getTempRange(int season) {
        if (season == 0) {
            return new double[]{SPRING_TEMP_MIN, SPRING_TEMP_MAX};
        } else if (season == 1) {
            return new double[]{SUMMER_TEMP_MIN, SUMMER_TEMP_MAX};
        } else if (season == 2) {
            return new double[]{AUTUMN_TEMP_MIN, AUTUMN_TEMP_MAX};
        } else if (season == 3) {
            return new double[]{WINTER_TEMP_MIN, WINTER_TEMP_MAX};
        } else {
            throw new IllegalArgumentException("无效季节：" + season);
        }
    }

    /**
     * 根据季节获取湿度范围
     */
    public static double[] getHumiRange(int season) {
        if (season == 0) {
            return new double[]{SPRING_HUMI_MIN, SPRING_HUMI_MAX};
        } else if (season == 1) {
            return new double[]{SUMMER_HUMI_MIN, SUMMER_HUMI_MAX};
        } else if (season == 2) {
            return new double[]{AUTUMN_HUMI_MIN, AUTUMN_HUMI_MAX};
        } else if (season == 3) {
            return new double[]{WINTER_HUMI_MIN, WINTER_HUMI_MAX};
        } else {
            throw new IllegalArgumentException("无效季节：" + season);
        }
    }

    /**
     * 获取地下室相对室外的温度差值
     * 负=地下室更冷，正=地下室更暖
     */
    public static double getTempDiffWithOutdoor() {
        return getTempDiffWithOutdoor(getSeason());
    }

    /**
     * 根据季节获取温度差值
     */
    public static double getTempDiffWithOutdoor(int season) {
        // 所有区间范围都 < 2.0
        switch (season) {
            case 0: // 春：地下室略低
                return randomBetween(-1.0, -0.5);
            case 1: // 夏：地下室明显低
                return randomBetween(-6.0, -5.5);
            case 2: // 秋：低于室外
                return randomBetween(-3.5, -3.0);
            case 3: // 冬：地下室更高
                return randomBetween(4.5, 5.0);
            default:
                return 0.0;
        }
    }

    /**
     * 获取地下室相对室外的湿度差值
     */
    public static double getHumiDiffWithOutdoor() {
        return getHumiDiffWithOutdoor(getSeason());
    }

    /**
     * 根据季节获取湿度差值
     */
    public static double getHumiDiffWithOutdoor(int season) {
        switch (season) {
            case 0:
                return randomBetween(12.0, 13.0);
            case 1:
                return randomBetween(19.0, 20.0);
            case 2:
                return randomBetween(9.0, 10.0);
            case 3:
                return randomBetween(7.0, 8.0);
            default:
                return 0.0;
        }
    }

    /**
     * 生成 [min, max] 之间的随机小数，保留 2 位
     */
    private static double randomBetween(double min, double max) {
        double val = min + (max - min) * RANDOM.nextDouble();
        return Math.round(val * 100) / 100.0;
    }
}