import 'package:equatable/equatable.dart';
import 'package:laki_icu_app/enums/video_stream_mode_enum.dart';
import 'package:laki_icu_app/models/bo/monitoring_bo.dart';
import 'package:laki_icu_app/services/webrtc_service.dart';
import 'package:laki_icu_app/utils/bluetooth/index.dart';
import 'package:laki_icu_app/utils/mqtt/index.dart';

enum MonitoringIndexStatus {
  initial,
  loading,
  success,
  failure,
}

class MonitoringIndexState extends Equatable {
  final MonitoringIndexStatus status;
  final bool isLoading;
  final String? error;
  final List<MonitoringMetricBO> metrics;
  final PatientInfoBO? patientInfo;
  final List<AlertInfoBO> alerts;

  /// WebRTC 视频连接状态
  final WebrtcConnectionState videoConnectionState;

  /// 当前视频传输模式
  final VideoStreamMode videoStreamMode;

  /// 是否正在切换传输模式（用于 UI loading 状态）
  final bool isSwitchingMode;

  /// P2P 模式是否已连接
  final bool isP2pConnected;

  /// 蓝牙扫描中
  final bool isBluetoothScanning;

  /// 蓝牙绑定/连接处理中
  final bool isBluetoothBinding;

  /// 正在绑定/连接的蓝牙设备 ID
  final String bindingBluetoothDeviceId;

  /// 扫描到的蓝牙设备
  final List<BluetoothScanDevice> bluetoothDevices;

  /// 已绑定蓝牙设备 ID
  final String? boundBluetoothDeviceId;

  /// 已绑定蓝牙设备名称
  final String? boundBluetoothDeviceName;

  /// 蓝牙连接状态
  final BluetoothConnectionStatus bluetoothConnectionStatus;

  /// 蓝牙状态提示
  final String? bluetoothMessage;

  /// 最近收到的蓝牙原始 Hex 数据
  final String? latestBluetoothRawHex;

  /// 最近解析出的 MCU 上报数据
  final McuReport? latestMcuReport;

  /// MQTT 连接状态
  final LakiMqttConnectionState mqttConnectionState;

  /// MQTT 状态提示
  final String? mqttMessage;

  /// 当前 MQTT 连接使用的设备 SN
  final String? mqttDeviceSn;

  /// 设备摄像头序列号（从舱详情接口 /cabin/detail 获取）
  final String? cabinCameraSn;

  /// 设备 WiFi 密码（从舱详情接口 /cabin/detail 获取）
  final String? cabinWifiPwd;

  /// MQTT 实时推送的摄像头告警列表（来自 camera/{sn}/to）
  final List<AlertInfoBO> cameraAlerts;

  /// 当前选中的告警（用于弹窗显示）
  final AlertInfoBO? selectedAlert;

  const MonitoringIndexState({
    this.status = MonitoringIndexStatus.initial,
    this.isLoading = false,
    this.error,
    this.metrics = const [],
    this.patientInfo,
    this.alerts = const [],
    this.cameraAlerts = const [],
    this.videoConnectionState = WebrtcConnectionState.disconnected,
    this.videoStreamMode = VideoStreamMode.p2p,
    this.isSwitchingMode = false,
    this.isP2pConnected = false,
    this.isBluetoothScanning = false,
    this.isBluetoothBinding = false,
    this.bindingBluetoothDeviceId = '',
    this.bluetoothDevices = const [],
    this.boundBluetoothDeviceId,
    this.boundBluetoothDeviceName,
    this.bluetoothConnectionStatus = BluetoothConnectionStatus.disconnected,
    this.bluetoothMessage,
    this.latestBluetoothRawHex,
    this.latestMcuReport,
    this.mqttConnectionState = LakiMqttConnectionState.disconnected,
    this.mqttMessage,
    this.mqttDeviceSn,
    this.cabinCameraSn,
    this.cabinWifiPwd,
    this.selectedAlert,
  });

  MonitoringIndexState copyWith({
    MonitoringIndexStatus? status,
    bool? isLoading,
    String? error,
    List<MonitoringMetricBO>? metrics,
    PatientInfoBO? patientInfo,
    List<AlertInfoBO>? alerts,
    List<AlertInfoBO>? cameraAlerts,
    WebrtcConnectionState? videoConnectionState,
    VideoStreamMode? videoStreamMode,
    bool? isSwitchingMode,
    bool? isP2pConnected,
    bool? isBluetoothScanning,
    bool? isBluetoothBinding,
    String? bindingBluetoothDeviceId,
    List<BluetoothScanDevice>? bluetoothDevices,
    String? boundBluetoothDeviceId,
    String? boundBluetoothDeviceName,
    BluetoothConnectionStatus? bluetoothConnectionStatus,
    String? bluetoothMessage,
    String? latestBluetoothRawHex,
    McuReport? latestMcuReport,
    LakiMqttConnectionState? mqttConnectionState,
    String? mqttMessage,
    String? mqttDeviceSn,
    String? cabinCameraSn,
    String? cabinWifiPwd,
    AlertInfoBO? selectedAlert,
    bool clearBoundBluetoothDevice = false,
    bool clearLatestMcuReport = false,
    bool clearPatientInfo = false,
    bool clearMqttDeviceSn = false,
    bool clearSelectedAlert = false,
  }) {
    return MonitoringIndexState(
      status: status ?? this.status,
      isLoading: isLoading ?? this.isLoading,
      error: error ?? this.error,
      metrics: metrics ?? this.metrics,
      patientInfo: clearPatientInfo ? null : patientInfo ?? this.patientInfo,
      alerts: alerts ?? this.alerts,
      cameraAlerts: cameraAlerts ?? this.cameraAlerts,
      videoConnectionState: videoConnectionState ?? this.videoConnectionState,
      videoStreamMode: videoStreamMode ?? this.videoStreamMode,
      isSwitchingMode: isSwitchingMode ?? this.isSwitchingMode,
      isP2pConnected: isP2pConnected ?? this.isP2pConnected,
      isBluetoothScanning: isBluetoothScanning ?? this.isBluetoothScanning,
      isBluetoothBinding: isBluetoothBinding ?? this.isBluetoothBinding,
      bindingBluetoothDeviceId:
          bindingBluetoothDeviceId ?? this.bindingBluetoothDeviceId,
      bluetoothDevices: bluetoothDevices ?? this.bluetoothDevices,
      boundBluetoothDeviceId: clearBoundBluetoothDevice
          ? null
          : boundBluetoothDeviceId ?? this.boundBluetoothDeviceId,
      boundBluetoothDeviceName: clearBoundBluetoothDevice
          ? null
          : boundBluetoothDeviceName ?? this.boundBluetoothDeviceName,
      bluetoothConnectionStatus:
          bluetoothConnectionStatus ?? this.bluetoothConnectionStatus,
      bluetoothMessage: bluetoothMessage ?? this.bluetoothMessage,
      latestBluetoothRawHex:
          latestBluetoothRawHex ?? this.latestBluetoothRawHex,
      latestMcuReport:
          clearLatestMcuReport ? null : latestMcuReport ?? this.latestMcuReport,
      mqttConnectionState: mqttConnectionState ?? this.mqttConnectionState,
      mqttMessage: mqttMessage ?? this.mqttMessage,
      mqttDeviceSn:
          clearMqttDeviceSn ? null : mqttDeviceSn ?? this.mqttDeviceSn,
      cabinCameraSn: cabinCameraSn ?? this.cabinCameraSn,
      cabinWifiPwd: cabinWifiPwd ?? this.cabinWifiPwd,
      selectedAlert: clearSelectedAlert
          ? null
          : selectedAlert ?? this.selectedAlert,
    );
  }

  @override
  List<Object?> get props => [
        status,
        isLoading,
        error,
        metrics,
        patientInfo,
        alerts,
        cameraAlerts,
        videoConnectionState,
        videoStreamMode,
        isSwitchingMode,
        isP2pConnected,
        isBluetoothScanning,
        isBluetoothBinding,
        bindingBluetoothDeviceId,
        bluetoothDevices,
        boundBluetoothDeviceId,
        boundBluetoothDeviceName,
        bluetoothConnectionStatus,
        bluetoothMessage,
        latestBluetoothRawHex,
        latestMcuReport,
        mqttConnectionState,
        mqttMessage,
        mqttDeviceSn,
        cabinCameraSn,
        cabinWifiPwd,
        selectedAlert,
      ];
}
