import '../data/bean/intimacy_config_info.dart';

class IntimacyUtil {
  IntimacyUtil._();

  static IntimacyConfigInfo? _configInfo;

  static List<IntimacyBean>? intimacyList() {
    return _configInfo?.intimacy;
  }

  /// 设置亲密度配置
  static void setConfig(IntimacyConfigInfo? config) {
    _configInfo = config;
  }

  /// 获取亲密度名称
  static String getIntimacyName(int intimacy) {
    final levels = _configInfo?.intimacy;
    if (levels == null || levels.isEmpty) return "?";

    for (final level in levels) {
      if (intimacy >= (level.min ?? 0) && intimacy <= (level.max ?? 0)) {
        return level.name ?? "?";
      }
    }
    return "?";
  }



  /// 获取当前对应的 IntimacyBean
  static IntimacyBean? getIntimacyLevel(int intimacy) {
    final levels = _configInfo?.intimacy;
    if (levels == null || levels.isEmpty) return null;

    for (final level in levels) {
      if (intimacy >= (level.min ?? 0) && intimacy <= (level.max ?? 0)) {
        return level;
      }
    }
    return null;
  }

  /// 根据 index 获取中值
  static int getMedianByIndex(int index) {
    final levels = _configInfo?.intimacy;
    if (levels == null || index < 0 || index >= levels.length) return 0;

    final level = levels[index];
    final min = level.min ?? 0;
    final max = level.max ?? 0;
    return ((min + max) / 2).round();
  }
}
