import 'package:equatable/equatable.dart';

// ==================== 设备告警看板 Dashboard ====================

class AlertDashboardBO extends Equatable {
  final int total;
  final AlertDashboardStatisticsBO statistics;
  final List<AlertItemBO> alerts;
  final List<DeviceItemBO> devices;

  const AlertDashboardBO({
    required this.total,
    required this.statistics,
    required this.alerts,
    required this.devices,
  });

  factory AlertDashboardBO.fromJson(Map<String, dynamic> json) {
    return AlertDashboardBO(
      total: json['total'] as int? ?? 0,
      statistics: AlertDashboardStatisticsBO.fromJson(
        json['statistics'] as Map<String, dynamic>? ?? {},
      ),
      alerts: _sortedAlerts(
            (json['alerts'] as List<dynamic>?)
                    ?.map((e) =>
                        AlertItemBO.fromJson(e as Map<String, dynamic>))
                    .toList() ??
                []),
      devices: (json['devices'] as List<dynamic>?)
              ?.map((e) =>
                  DeviceItemBO.fromJson(e as Map<String, dynamic>))
              .toList() ??
          [],
    );
  }

  /// 根据 alertLevelText 排序：置顶 > 高警 > 中警 > 一般
  static List<AlertItemBO> _sortedAlerts(List<AlertItemBO> alerts) {
    const order = {'置顶': 0, '高警': 1, '中警': 2, '一般': 3};
    return List<AlertItemBO>.from(alerts)
      ..sort((a, b) {
        final aOrder = order[a.alertLevelText] ?? 4;
        final bOrder = order[b.alertLevelText] ?? 4;
        return aOrder.compareTo(bOrder);
      });
  }

  @override
  List<Object?> get props => [total, statistics, alerts, devices];
}

// ==================== 统计数据 ====================

class AlertDashboardStatisticsBO extends Equatable {
  final int highAlertCount;
  final int mediumAlertCount;
  final int onlineDeviceCount;

  const AlertDashboardStatisticsBO({
    required this.highAlertCount,
    required this.mediumAlertCount,
    required this.onlineDeviceCount,
  });

  factory AlertDashboardStatisticsBO.fromJson(Map<String, dynamic> json) {
    return AlertDashboardStatisticsBO(
      highAlertCount: json['highAlertCount'] as int? ?? 0,
      mediumAlertCount: json['mediumAlertCount'] as int? ?? 0,
      onlineDeviceCount: json['onlineDeviceCount'] as int? ?? 0,
    );
  }

  @override
  List<Object?> get props =>
      [highAlertCount, mediumAlertCount, onlineDeviceCount];
}

// ==================== 告警信息 ====================

class AlertItemBO extends Equatable {
  final int alertId;
  final String alertNo;
  final String alertTitle;
  final String alertLevel;
  final String alertLevelText;
  final String alertStatus;
  final String roomName;
  final String deviceName;
  final String alertValue;
  final String thresholdValue;
  final String alertContent;
  final String voltage;
  final String current;
  final String temperature;
  final int waitingDuration;
  final String waitingDurationDesc;
  final String alertTime;
  final String alertCategory;
  final String deviceCode;
  final String deviceLocation;

  const AlertItemBO({
    required this.alertId,
    required this.alertNo,
    required this.alertTitle,
    required this.alertLevel,
    required this.alertLevelText,
    required this.alertStatus,
    required this.roomName,
    required this.deviceName,
    required this.alertValue,
    required this.thresholdValue,
    required this.alertContent,
    required this.voltage,
    required this.current,
    required this.temperature,
    required this.waitingDuration,
    required this.waitingDurationDesc,
    required this.alertTime,
    required this.alertCategory,
    required this.deviceCode,
    required this.deviceLocation,
  });

  factory AlertItemBO.fromJson(Map<String, dynamic> json) {
    return AlertItemBO(
      alertId: json['alertId'] as int? ?? 0,
      alertNo: json['alertNo'] as String? ?? '',
      alertTitle: json['alertTitle'] as String? ?? '',
      alertLevel: json['alertLevel'] as String? ?? '',
      alertLevelText: json['alertLevelText'] as String? ?? '',
      alertStatus: json['alertStatus'] as String? ?? '0',
      roomName: json['roomName'] as String? ?? '',
      deviceName: json['deviceName'] as String? ?? '',
      alertValue: json['alertValue'] as String? ?? '',
      thresholdValue: json['thresholdValue'] as String? ?? '',
      alertContent: json['alertContent'] as String? ?? '',
      voltage: json['voltage'] as String? ?? '',
      current: json['current'] as String? ?? '',
      temperature: json['temperature'] as String? ?? '',
      waitingDuration: json['waitingDuration'] as int? ?? 0,
      waitingDurationDesc: json['waitingDurationDesc'] as String? ?? '',
      alertTime: json['alertTime'] as String? ?? '',
      alertCategory: json['alertCategory'] as String? ?? '',
      deviceCode: json['deviceCode'] as String? ?? '',
      deviceLocation: json['deviceLocation'] as String? ?? '',
    );
  }

  @override
  List<Object?> get props => [
        alertId,
        alertNo,
        alertTitle,
        alertLevel,
        alertLevelText,
        alertStatus,
        roomName,
        deviceName,
        alertValue,
        thresholdValue,
        alertContent,
        voltage,
        current,
        temperature,
        waitingDuration,
        waitingDurationDesc,
        alertTime,
        alertCategory,
    deviceCode,
    deviceLocation,
      ];
}

// ==================== 设备巡检列表 ====================

class DeviceItemBO extends Equatable {
  final int deviceId;
  final String deviceName;
  final String deviceTypeIcon;
  final String deviceTypeName;
  final String runStatus;
  final String runStatusText;
  final String roomName;
  final String temperature;
  final String voltage;
  final String power;
  final String current;
  final String onlineStatus;
  final String lastHeartbeat;
  final String deviceCode;
  final String deviceLocation;

  const DeviceItemBO({
    required this.deviceId,
    required this.deviceName,
    required this.deviceTypeIcon,
    required this.deviceTypeName,
    required this.runStatus,
    required this.runStatusText,
    required this.roomName,
    required this.temperature,
    required this.voltage,
    required this.power,
    required this.current,
    required this.onlineStatus,
    required this.lastHeartbeat,
    required this.deviceCode,
    required this.deviceLocation,
  });

  factory DeviceItemBO.fromJson(Map<String, dynamic> json) {
    return DeviceItemBO(
      deviceId: json['deviceId'] as int? ?? 0,
      deviceName: json['deviceName'] as String? ?? '',
      deviceTypeIcon: json['deviceTypeIcon'] as String? ?? '',
      deviceTypeName: json['deviceTypeName'] as String? ?? '',
      runStatus: json['runStatus'] as String? ?? '',
      runStatusText: json['runStatusText'] as String? ?? '',
      roomName: json['roomName'] as String? ?? '',
      temperature: json['temperature'] as String? ?? '',
      voltage: json['voltage'] as String? ?? '',
      power: json['power'] as String? ?? '',
      current: json['current'] as String? ?? '',
      onlineStatus: json['onlineStatus'] as String? ?? '0',
      lastHeartbeat: json['lastHeartbeat'] as String? ?? '',
      deviceCode: json['deviceCode'] as String? ?? '',
      deviceLocation: json['deviceLocation'] as String? ?? '',
    );
  }

  @override
  List<Object?> get props => [
        deviceId,
        deviceName,
        deviceTypeIcon,
        deviceTypeName,
        runStatus,
        runStatusText,
        roomName,
        temperature,
        voltage,
        power,
        current,
        onlineStatus,
        lastHeartbeat,
    deviceCode,
    deviceLocation,
      ];
}