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

class IntimacyUtil {
  IntimacyUtil._();

  static IntimacyConfigInfo? _configInfo = IntimacyConfigInfo([
  IntimacyBean(20, 0, '初相识', 'http://cdn.atmob.com/keyboard/icon/stage1.png'),
  IntimacyBean(40, 21, '追爱期ing', 'http://cdn.atmob.com/keyboard/icon/stage2.png'),
  IntimacyBean(60, 41, '暧昧期ing', 'http://cdn.atmob.com/keyboard/icon/stage3.png'),
  IntimacyBean(80, 61, '热恋期', 'http://cdn.atmob.com/keyboard/icon/stage4.png'),
  IntimacyBean(90, 81, '伴侣', 'http://cdn.atmob.com/keyboard/icon/stage5.png'),
  IntimacyBean(100, 91, '灵魂伴侣', 'http://cdn.atmob.com/keyboard/icon/stage6.png'),
  ]);


  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();
  }
}
