import 'package:kaer_with_panels/app/utils/kr_log_util.dart';

class KRNodeList {
   final List<KrNodeListItem> list;
  final String subscribeId;
  final String startTime;
  final String expireTime;

  const KRNodeList({
    required this.list,
    this.subscribeId = "0",
    this.startTime = "",
    this.expireTime = "",
  });

  factory KRNodeList.fromJson(Map<String, dynamic> json) {
   
    try {
      final List<dynamic>? jsonList= json['list'] as List<dynamic>?;
      return KRNodeList(
        list: jsonList?.map((e) => KrNodeListItem.fromJson(e as Map<String, dynamic>)).toList() ?? [],
        subscribeId: json['id']?.toString() ?? "0",
        startTime: json['start_time']?.toString() ?? "",
        expireTime: json['expire_time']?.toString() ?? "",
      );
    } catch (err) {
      KRLogUtil.kr_e('KRNodeList解析错误: $err', tag: 'NodeList');
      return const KRNodeList(list: []);
    }
  }
}

class KrNodeListItem {
  final int id;
   String name;
  final String uuid;
  final String protocol;
  final String relayMode;
  final String relayNode;
  final String serverAddr;
  final int speedLimit;
  final List<String> tags;
  final int traffic;
  final double trafficRatio;
  final int upload;
  final String city;
  final String config;
  final String country;
  final int createdAt;
  final int download;
  final String startTime;
  final String expireTime;
  final double latitude;
  final double longitude;

  KrNodeListItem({
    required this.id,
    required this.name,
    required this.uuid,
    required this.protocol,
    this.relayMode = '',
    this.relayNode = '',
    required this.serverAddr,
    required this.speedLimit,
    required this.tags,
    required this.traffic,
    required this.trafficRatio,
    required this.upload,
    required this.city,
    required this.config,
    required this.country,
    this.createdAt = 0,
    required this.download,
    required this.startTime,
    required this.expireTime,
    required this.latitude,
    required this.longitude,
  });

  factory KrNodeListItem.fromJson(Map<String, dynamic> json) {
    try {
      return KrNodeListItem(
        id: _parseIntSafely(json['id']),
        name: json['name']?.toString() ?? '',
        uuid: json['uuid']?.toString() ?? '',
        protocol: json['protocol']?.toString() ?? '',
        relayMode: json['relay_mode']?.toString() ?? '',
        relayNode: json['relay_node']?.toString() ?? '',
        serverAddr: json['server_addr']?.toString() ?? '',
        speedLimit: _parseIntSafely(json['speed_limit']),
        tags: _parseStringList(json['tags']),
        traffic: _parseIntSafely(json['traffic']),
        trafficRatio: _parseDoubleSafely(json['traffic_ratio']),
        upload: _parseIntSafely(json['upload']),
        city: json['city']?.toString() ?? '',
        config: json['config']?.toString() ?? '',
        country: json['country']?.toString() ?? '',
        createdAt: _parseIntSafely(json['created_at']),
        download: _parseIntSafely(json['download']),
        startTime: json['start_time']?.toString() ?? '',
        expireTime: json['expire_time']?.toString() ?? '',
        latitude: _parseDoubleSafely(json['latitude']),
        longitude: _parseDoubleSafely(json['longitude']),
      );
    } catch (err) {
      KRLogUtil.kr_e('KrNodeListItem解析错误: $err', tag: 'NodeList');
      return KrNodeListItem(
        id: 0,
        name: '',
        uuid: '',
        protocol: '',
        serverAddr: '',
        speedLimit: 0,
        tags: [],
        traffic: 0,
        trafficRatio: 0,
        upload: 0,
        city: '',
        config: '',
        country: '',
        download: 0,
        startTime: '',
        expireTime: '',
        latitude: 0.0,
        longitude: 0.0,
      );
    }
  }

  // 添加安全解析工具方法
  static int _parseIntSafely(dynamic value) {
    if (value == null) return 0;
    if (value is int) return value;
    if (value is String) return int.tryParse(value) ?? 0;
    return 0;
  }

  static double _parseDoubleSafely(dynamic value) {
    if (value == null) return 0.0;
    if (value is double) return value;
    if (value is int) return value.toDouble();
    if (value is String) return double.tryParse(value) ?? 0.0;
    return 0.0;
  }

  static List<String> _parseStringList(dynamic value) {
    if (value == null) return [];
    if (value is List) {
      return value.map((e) => e?.toString() ?? '').toList();
    }
    return [];
  }
}
