import 'package:equatable/equatable.dart';

/// 用途：设备控制面板整体数据
/// 涉及页面：设备控制页面
class DeviceControlBO extends Equatable {
  // 设备唯一sn
  final String? sn;

  /// 总运行时长（设备累计运行时间）
  final String totalRunTime;

  /// 环境温度（当前环境温度值，从MCU获取）
  final String environmentTemp;

  /// 总制氧时长（设备累计制氧时间）
  final String totalOxygenTime;

  /// 舱内温度控制
  final TemperatureControlBO cabinTemp;

  /// 舱内湿度控制
  final HumidityControlBO cabinHumidity;

  /// 氧气浓度控制
  final OxygenConcentrationBO oxygenConcentration;

  /// 二氧化碳浓度控制
  final CO2ConcentrationBO co2Concentration;

  /// 护理等级控制
  final CareLevelControlBO careLevel;

  /// 新风进化控制
  final FreshAirControlBO freshAir;

  /// 风速调节模式
  final WindSpeedControlBO windSpeed;

  /// 雾化时间设置
  final NebulizationTimeBO nebulizationTime;

  /// 消毒时间设置
  final DisinfectionTimeBO disinfectionTime;

  /// 开放供氧控制
  final OpenOxygenControlBO openOxygen;

  /// 供氧计时
  final OxygenTimerBO oxygenTimer;

  /// 循环模式控制
  final CirculationModeBO circulationMode;

  /// 灯光控制
  final LightControlBO lightControl;

  const DeviceControlBO({
    this.sn,
    required this.totalRunTime,
    required this.environmentTemp,
    required this.totalOxygenTime,
    required this.cabinTemp,
    required this.cabinHumidity,
    required this.oxygenConcentration,
    required this.co2Concentration,
    required this.careLevel,
    required this.freshAir,
    required this.windSpeed,
    required this.nebulizationTime,
    required this.disinfectionTime,
    required this.openOxygen,
    required this.oxygenTimer,
    required this.circulationMode,
    required this.lightControl,
  });

  DeviceControlBO copyWith({
    String? sn,
    String? totalRunTime,
    String? environmentTemp,
    String? totalOxygenTime,
    TemperatureControlBO? cabinTemp,
    HumidityControlBO? cabinHumidity,
    OxygenConcentrationBO? oxygenConcentration,
    CO2ConcentrationBO? co2Concentration,
    CareLevelControlBO? careLevel,
    FreshAirControlBO? freshAir,
    WindSpeedControlBO? windSpeed,
    NebulizationTimeBO? nebulizationTime,
    DisinfectionTimeBO? disinfectionTime,
    OpenOxygenControlBO? openOxygen,
    OxygenTimerBO? oxygenTimer,
    CirculationModeBO? circulationMode,
    LightControlBO? lightControl,
  }) {
    return DeviceControlBO(
      sn: sn ?? this.sn,
      totalRunTime: totalRunTime ?? this.totalRunTime,
      environmentTemp: environmentTemp ?? this.environmentTemp,
      totalOxygenTime: totalOxygenTime ?? this.totalOxygenTime,
      cabinTemp: cabinTemp ?? this.cabinTemp,
      cabinHumidity: cabinHumidity ?? this.cabinHumidity,
      oxygenConcentration: oxygenConcentration ?? this.oxygenConcentration,
      co2Concentration: co2Concentration ?? this.co2Concentration,
      careLevel: careLevel ?? this.careLevel,
      freshAir: freshAir ?? this.freshAir,
      windSpeed: windSpeed ?? this.windSpeed,
      nebulizationTime: nebulizationTime ?? this.nebulizationTime,
      disinfectionTime: disinfectionTime ?? this.disinfectionTime,
      openOxygen: openOxygen ?? this.openOxygen,
      oxygenTimer: oxygenTimer ?? this.oxygenTimer,
      circulationMode: circulationMode ?? this.circulationMode,
      lightControl: lightControl ?? this.lightControl,
    );
  }

  factory DeviceControlBO.fromJson(Map<String, dynamic> json) {
    return DeviceControlBO(
      sn: json['sn'] as String?,
      totalRunTime: json['totalRunTime'] as String? ?? '0h',
      environmentTemp: json['environmentTemp'] as String? ?? '25.0℃',
      totalOxygenTime: json['totalOxygenTime'] as String? ?? '0h',
      cabinTemp: json['cabinTemp'] != null
          ? TemperatureControlBO.fromJson(json['cabinTemp'] as Map<String, dynamic>)
          : const TemperatureControlBO(
              currentValue: '24',
              setValue: '24',
              unit: '℃',
              isOn: false,
            ),
      cabinHumidity: json['cabinHumidity'] != null
          ? HumidityControlBO.fromJson(json['cabinHumidity'] as Map<String, dynamic>)
          : const HumidityControlBO(
              currentValue: '24',
              setValue: '50',
              unit: '%',
              isOn: false,
            ),
      oxygenConcentration: json['oxygenConcentration'] != null
          ? OxygenConcentrationBO.fromJson(json['oxygenConcentration'] as Map<String, dynamic>)
          : const OxygenConcentrationBO(
              currentValue: '24',
              setValue: '90',
              unit: '%',
              isOn: true,
            ),
      co2Concentration: json['co2Concentration'] != null
          ? CO2ConcentrationBO.fromJson(json['co2Concentration'] as Map<String, dynamic>)
          : const CO2ConcentrationBO(
              currentValue: '2400',
              alarmThreshold: '4000',
              unit: 'PPM',
            ),
      careLevel: json['careLevel'] != null
          ? CareLevelControlBO.fromJson(json['careLevel'] as Map<String, dynamic>)
          : const CareLevelControlBO(
              currentLevel: CareLevel.special,
            ),
      freshAir: json['freshAir'] != null
          ? FreshAirControlBO.fromJson(json['freshAir'] as Map<String, dynamic>)
          : const FreshAirControlBO(
              mode: FreshAirMode.auto,
              description: 'CO₂超设定值时自动启动外循环净化，当降至2000ppm后，本功能自动关闭',
            ),
      windSpeed: json['windSpeed'] != null
          ? WindSpeedControlBO.fromJson(json['windSpeed'] as Map<String, dynamic>)
          : const WindSpeedControlBO(
              currentMode: WindSpeedMode.sleep,
            ),
      nebulizationTime: json['nebulizationTime'] != null
          ? NebulizationTimeBO.fromJson(json['nebulizationTime'] as Map<String, dynamic>)
          : const NebulizationTimeBO(
              setMinutes: '00',
            ),
      disinfectionTime: json['disinfectionTime'] != null
          ? DisinfectionTimeBO.fromJson(json['disinfectionTime'] as Map<String, dynamic>)
          : const DisinfectionTimeBO(
              setMinutes: '14',
            ),
      openOxygen: json['openOxygen'] != null
          ? OpenOxygenControlBO.fromJson(json['openOxygen'] as Map<String, dynamic>)
          : const OpenOxygenControlBO(
              isOn: false,
              description: '开启后风扇打开，制冷、加热和循环等设备暂停。\n*保持设备周围空气相对禁止，严禁烟火',
            ),
      oxygenTimer: json['oxygenTimer'] != null
          ? OxygenTimerBO.fromJson(json['oxygenTimer'] as Map<String, dynamic>)
          : const OxygenTimerBO(
              hours: '00',
              minutes: '00',
              seconds: '00',
            ),
      circulationMode: json['circulationMode'] != null
          ? CirculationModeBO.fromJson(json['circulationMode'] as Map<String, dynamic>)
          : const CirculationModeBO(
              currentMode: CirculationMode.external,
            ),
      lightControl: json['lightControl'] != null
          ? LightControlBO.fromJson(json['lightControl'] as Map<String, dynamic>)
          : const LightControlBO(
              checkLightOn: false,
              illuminationOn: false,
              blueLightOn: false,
              redLightOn: false,
            ),
    );
  }

  @override
  List<Object?> get props => [
        sn,
        totalRunTime,
        environmentTemp,
        totalOxygenTime,
        cabinTemp,
        cabinHumidity,
        oxygenConcentration,
        co2Concentration,
        careLevel,
        freshAir,
        windSpeed,
        nebulizationTime,
        disinfectionTime,
        openOxygen,
        oxygenTimer,
        circulationMode,
        lightControl,
      ];
}

/// 用途：舱内温度控制数据
/// 涉及页面：设备控制页面
class TemperatureControlBO extends Equatable {
  /// 当前温度值（设备真实数据，从MCU获取）
  final String currentValue;

  /// 设置温度值（用户配置的期望值）
  final String setValue;

  /// 温度单位
  final String unit;

  /// 开关状态
  final bool isOn;

  const TemperatureControlBO({
    required this.currentValue,
    required this.setValue,
    required this.unit,
    required this.isOn,
  });

  TemperatureControlBO copyWith({
    String? currentValue,
    String? setValue,
    String? unit,
    bool? isOn,
  }) {
    return TemperatureControlBO(
      currentValue: currentValue ?? this.currentValue,
      setValue: setValue ?? this.setValue,
      unit: unit ?? this.unit,
      isOn: isOn ?? this.isOn,
    );
  }

  factory TemperatureControlBO.fromJson(Map<String, dynamic> json) {
    return TemperatureControlBO(
      currentValue: json['currentValue'] as String? ?? '24',
      setValue: json['setValue'] as String? ?? '24',
      unit: json['unit'] as String? ?? '℃',
      isOn: json['isOn'] as bool? ?? false,
    );
  }

  @override
  List<Object?> get props => [currentValue, setValue, unit, isOn];
}

/// 用途：舱内湿度控制数据
/// 涉及页面：设备控制页面
class HumidityControlBO extends Equatable {
  /// 当前湿度值（设备真实数据，从MCU获取）
  final String currentValue;

  /// 设置湿度值（用户配置的期望值）
  final String setValue;

  /// 湿度单位
  final String unit;

  /// 开关状态
  final bool isOn;

  const HumidityControlBO({
    required this.currentValue,
    required this.setValue,
    required this.unit,
    required this.isOn,
  });

  HumidityControlBO copyWith({
    String? currentValue,
    String? setValue,
    String? unit,
    bool? isOn,
  }) {
    return HumidityControlBO(
      currentValue: currentValue ?? this.currentValue,
      setValue: setValue ?? this.setValue,
      unit: unit ?? this.unit,
      isOn: isOn ?? this.isOn,
    );
  }

  factory HumidityControlBO.fromJson(Map<String, dynamic> json) {
    return HumidityControlBO(
      currentValue: json['currentValue'] as String? ?? '24',
      setValue: json['setValue'] as String? ?? '50',
      unit: json['unit'] as String? ?? '%',
      isOn: json['isOn'] as bool? ?? false,
    );
  }

  @override
  List<Object?> get props => [currentValue, setValue, unit, isOn];
}

/// 用途：氧气浓度控制数据
/// 涉及页面：设备控制页面
class OxygenConcentrationBO extends Equatable {
  /// 当前浓度值（设备真实数据，从MCU获取）
  final String currentValue;

  /// 设置浓度值（用户配置的期望值）
  final String setValue;

  /// 浓度单位
  final String unit;

  /// 开关状态
  final bool isOn;

  const OxygenConcentrationBO({
    required this.currentValue,
    required this.setValue,
    required this.unit,
    required this.isOn,
  });

  OxygenConcentrationBO copyWith({
    String? currentValue,
    String? setValue,
    String? unit,
    bool? isOn,
  }) {
    return OxygenConcentrationBO(
      currentValue: currentValue ?? this.currentValue,
      setValue: setValue ?? this.setValue,
      unit: unit ?? this.unit,
      isOn: isOn ?? this.isOn,
    );
  }

  factory OxygenConcentrationBO.fromJson(Map<String, dynamic> json) {
    return OxygenConcentrationBO(
      currentValue: json['currentValue'] as String? ?? '24',
      setValue: json['setValue'] as String? ?? '90',
      unit: json['unit'] as String? ?? '%',
      isOn: json['isOn'] as bool? ?? true,
    );
  }

  @override
  List<Object?> get props => [currentValue, setValue, unit, isOn];
}

/// 用途：二氧化碳浓度控制数据
/// 涉及页面：设备控制页面
class CO2ConcentrationBO extends Equatable {
  /// 当前浓度值（设备真实数据，从MCU获取）
  final String currentValue;

  /// 告警阈值（用户配置的告警触发值）
  final String alarmThreshold;

  /// 浓度单位
  final String unit;

  const CO2ConcentrationBO({
    required this.currentValue,
    required this.alarmThreshold,
    required this.unit,
  });

  CO2ConcentrationBO copyWith({
    String? currentValue,
    String? alarmThreshold,
    String? unit,
  }) {
    return CO2ConcentrationBO(
      currentValue: currentValue ?? this.currentValue,
      alarmThreshold: alarmThreshold ?? this.alarmThreshold,
      unit: unit ?? this.unit,
    );
  }

  factory CO2ConcentrationBO.fromJson(Map<String, dynamic> json) {
    return CO2ConcentrationBO(
      currentValue: json['currentValue'] as String? ?? '2400',
      alarmThreshold: json['alarmThreshold'] as String? ?? '4000',
      unit: json['unit'] as String? ?? 'PPM',
    );
  }

  @override
  List<Object?> get props => [currentValue, alarmThreshold, unit];
}

/// 护理等级枚举
enum CareLevel { off, level3, level2, level1, special }

/// 用途：护理等级控制数据
/// 涉及页面：设备控制页面
class CareLevelControlBO extends Equatable {
  /// 当前护理等级（用户配置的等级）
  final CareLevel currentLevel;

  const CareLevelControlBO({
    required this.currentLevel,
  });

  CareLevelControlBO copyWith({
    CareLevel? currentLevel,
  }) {
    return CareLevelControlBO(
      currentLevel: currentLevel ?? this.currentLevel,
    );
  }

  factory CareLevelControlBO.fromJson(Map<String, dynamic> json) {
    final levelStr = json['currentLevel'] as String? ?? 'special';
    final level = CareLevel.values.firstWhere(
      (e) => e.name == levelStr,
      orElse: () => CareLevel.special,
    );
    return CareLevelControlBO(currentLevel: level);
  }

  @override
  List<Object?> get props => [currentLevel];
}

/// 新风模式枚举
enum FreshAirMode { auto, on, off }

/// 用途：新风进化控制数据
/// 涉及页面：设备控制页面
class FreshAirControlBO extends Equatable {
  /// 新风模式（用户配置的模式）
  final FreshAirMode mode;

  /// 模式描述
  final String description;

  const FreshAirControlBO({
    required this.mode,
    required this.description,
  });

  FreshAirControlBO copyWith({
    FreshAirMode? mode,
    String? description,
  }) {
    return FreshAirControlBO(
      mode: mode ?? this.mode,
      description: description ?? this.description,
    );
  }

  factory FreshAirControlBO.fromJson(Map<String, dynamic> json) {
    final modeStr = json['mode'] as String? ?? 'auto';
    final mode = FreshAirMode.values.firstWhere(
      (e) => e.name == modeStr,
      orElse: () => FreshAirMode.auto,
    );
    return FreshAirControlBO(
      mode: mode,
      description: json['description'] as String? ?? '',
    );
  }

  @override
  List<Object?> get props => [mode, description];
}

/// 风速模式枚举
enum WindSpeedMode { sleep, comfort, strong }

/// 用途：风速调节控制数据
/// 涉及页面：设备控制页面
class WindSpeedControlBO extends Equatable {
  /// 当前风速模式（用户配置的模式）
  final WindSpeedMode currentMode;

  const WindSpeedControlBO({
    required this.currentMode,
  });

  WindSpeedControlBO copyWith({
    WindSpeedMode? currentMode,
  }) {
    return WindSpeedControlBO(
      currentMode: currentMode ?? this.currentMode,
    );
  }

  factory WindSpeedControlBO.fromJson(Map<String, dynamic> json) {
    final modeStr = json['currentMode'] as String? ?? 'sleep';
    final mode = WindSpeedMode.values.firstWhere(
      (e) => e.name == modeStr,
      orElse: () => WindSpeedMode.sleep,
    );
    return WindSpeedControlBO(currentMode: mode);
  }

  @override
  List<Object?> get props => [currentMode];
}

/// 用途：雾化时间设置数据
/// 涉及页面：设备控制页面
class NebulizationTimeBO extends Equatable {
  /// 设置雾化分钟数（用户配置的时间）
  final String setMinutes;

  const NebulizationTimeBO({
    required this.setMinutes,
  });

  NebulizationTimeBO copyWith({
    String? setMinutes,
  }) {
    return NebulizationTimeBO(
      setMinutes: setMinutes ?? this.setMinutes,
    );
  }

  factory NebulizationTimeBO.fromJson(Map<String, dynamic> json) {
    return NebulizationTimeBO(
      setMinutes: json['setMinutes'] as String? ?? '00',
    );
  }

  @override
  List<Object?> get props => [setMinutes];
}

/// 用途：消毒时间设置数据
/// 涉及页面：设备控制页面
class DisinfectionTimeBO extends Equatable {
  /// 设置消毒分钟数（用户配置的时间）
  final String setMinutes;

  const DisinfectionTimeBO({
    required this.setMinutes,
  });

  DisinfectionTimeBO copyWith({
    String? setMinutes,
  }) {
    return DisinfectionTimeBO(
      setMinutes: setMinutes ?? this.setMinutes,
    );
  }

  factory DisinfectionTimeBO.fromJson(Map<String, dynamic> json) {
    return DisinfectionTimeBO(
      setMinutes: json['setMinutes'] as String? ?? '14',
    );
  }

  @override
  List<Object?> get props => [setMinutes];
}

/// 用途：开放供氧控制数据
/// 涉及页面：设备控制页面
class OpenOxygenControlBO extends Equatable {
  /// 开关状态（用户配置的状态）
  final bool isOn;

  /// 描述说明
  final String description;

  const OpenOxygenControlBO({
    required this.isOn,
    required this.description,
  });

  OpenOxygenControlBO copyWith({
    bool? isOn,
    String? description,
  }) {
    return OpenOxygenControlBO(
      isOn: isOn ?? this.isOn,
      description: description ?? this.description,
    );
  }

  factory OpenOxygenControlBO.fromJson(Map<String, dynamic> json) {
    return OpenOxygenControlBO(
      isOn: json['isOn'] as bool? ?? false,
      description: json['description'] as String? ?? '',
    );
  }

  @override
  List<Object?> get props => [isOn, description];
}

/// 用途：供氧计时数据
/// 涉及页面：设备控制页面
class OxygenTimerBO extends Equatable {
  /// 小时（当前计时时间）
  final String hours;

  /// 分钟（当前计时时间）
  final String minutes;

  /// 秒（当前计时时间）
  final String seconds;

  const OxygenTimerBO({
    required this.hours,
    required this.minutes,
    required this.seconds,
  });

  OxygenTimerBO copyWith({
    String? hours,
    String? minutes,
    String? seconds,
  }) {
    return OxygenTimerBO(
      hours: hours ?? this.hours,
      minutes: minutes ?? this.minutes,
      seconds: seconds ?? this.seconds,
    );
  }

  factory OxygenTimerBO.fromJson(Map<String, dynamic> json) {
    return OxygenTimerBO(
      hours: json['hours'] as String? ?? '00',
      minutes: json['minutes'] as String? ?? '00',
      seconds: json['seconds'] as String? ?? '00',
    );
  }

  @override
  List<Object?> get props => [hours, minutes, seconds];
}

/// 循环模式枚举
enum CirculationMode { external, internal }

/// 用途：循环模式控制数据
/// 涉及页面：设备控制页面
class CirculationModeBO extends Equatable {
  /// 当前循环模式（用户配置的模式）
  final CirculationMode currentMode;

  const CirculationModeBO({
    required this.currentMode,
  });

  CirculationModeBO copyWith({
    CirculationMode? currentMode,
  }) {
    return CirculationModeBO(
      currentMode: currentMode ?? this.currentMode,
    );
  }

  factory CirculationModeBO.fromJson(Map<String, dynamic> json) {
    final modeStr = json['currentMode'] as String? ?? 'external';
    final mode = CirculationMode.values.firstWhere(
      (e) => e.name == modeStr,
      orElse: () => CirculationMode.external,
    );
    return CirculationModeBO(currentMode: mode);
  }

  @override
  List<Object?> get props => [currentMode];
}

/// 用途：灯光控制数据
/// 涉及页面：设备控制页面
class LightControlBO extends Equatable {
  /// 检查灯开关（用户配置的状态）
  final bool checkLightOn;

  /// 照明灯开关（用户配置的状态）
  final bool illuminationOn;

  /// 蓝光灯开关（用户配置的状态）
  final bool blueLightOn;

  /// 红光灯开关（用户配置的状态）
  final bool redLightOn;

  const LightControlBO({
    required this.checkLightOn,
    required this.illuminationOn,
    required this.blueLightOn,
    required this.redLightOn,
  });

  LightControlBO copyWith({
    bool? checkLightOn,
    bool? illuminationOn,
    bool? blueLightOn,
    bool? redLightOn,
  }) {
    return LightControlBO(
      checkLightOn: checkLightOn ?? this.checkLightOn,
      illuminationOn: illuminationOn ?? this.illuminationOn,
      blueLightOn: blueLightOn ?? this.blueLightOn,
      redLightOn: redLightOn ?? this.redLightOn,
    );
  }

  factory LightControlBO.fromJson(Map<String, dynamic> json) {
    return LightControlBO(
      checkLightOn: json['checkLightOn'] as bool? ?? false,
      illuminationOn: json['illuminationOn'] as bool? ?? false,
      blueLightOn: json['blueLightOn'] as bool? ?? false,
      redLightOn: json['redLightOn'] as bool? ?? false,
    );
  }

  @override
  List<Object?> get props => [checkLightOn, illuminationOn, blueLightOn, redLightOn];
}

/// 用途：设备控制用户设置数据（仅包含用户可配置的字段，不包含MCU获取的值）
/// 用于 hydrated_bloc 持久化
class DeviceControlUserSettings extends Equatable {
  /// 设备SN
  final String? sn;

  /// 舱内温度设定值
  final String? cabinTempSetValue;

  /// 舱内温度开关
  final bool? cabinTempIsOn;

  /// 舱内湿度设定值
  final String? cabinHumiditySetValue;

  /// 舱内湿度开关
  final bool? cabinHumidityIsOn;

  /// 氧气浓度设定值
  final String? oxygenConcentrationSetValue;

  /// 氧气浓度开关
  final bool? oxygenConcentrationIsOn;

  /// 二氧化碳告警阈值
  final String? co2AlarmThreshold;

  /// 护理等级
  final CareLevel? careLevelValue;

  /// 新风模式
  final FreshAirMode? freshAirModeValue;

  /// 风速模式
  final WindSpeedMode? windSpeedModeValue;

  /// 雾化时间（分钟）
  final String? nebulizationTimeMinutes;

  /// 消毒时间（分钟）
  final String? disinfectionTimeMinutes;

  /// 开放供氧开关
  final bool? openOxygenIsOn;

  /// 循环模式
  final CirculationMode? circulationModeValue;

  /// 检查灯开关
  final bool? checkLightOn;

  /// 照明灯开关
  final bool? illuminationOn;

  /// 蓝光灯开关
  final bool? blueLightOn;

  /// 红光灯开关
  final bool? redLightOn;

  const DeviceControlUserSettings({
    this.sn,
    this.cabinTempSetValue,
    this.cabinTempIsOn,
    this.cabinHumiditySetValue,
    this.cabinHumidityIsOn,
    this.oxygenConcentrationSetValue,
    this.oxygenConcentrationIsOn,
    this.co2AlarmThreshold,
    this.careLevelValue,
    this.freshAirModeValue,
    this.windSpeedModeValue,
    this.nebulizationTimeMinutes,
    this.disinfectionTimeMinutes,
    this.openOxygenIsOn,
    this.circulationModeValue,
    this.checkLightOn,
    this.illuminationOn,
    this.blueLightOn,
    this.redLightOn,
  });

  DeviceControlUserSettings copyWith({
    String? sn,
    String? cabinTempSetValue,
    bool? cabinTempIsOn,
    String? cabinHumiditySetValue,
    bool? cabinHumidityIsOn,
    String? oxygenConcentrationSetValue,
    bool? oxygenConcentrationIsOn,
    String? co2AlarmThreshold,
    CareLevel? careLevelValue,
    FreshAirMode? freshAirModeValue,
    WindSpeedMode? windSpeedModeValue,
    String? nebulizationTimeMinutes,
    String? disinfectionTimeMinutes,
    bool? openOxygenIsOn,
    CirculationMode? circulationModeValue,
    bool? checkLightOn,
    bool? illuminationOn,
    bool? blueLightOn,
    bool? redLightOn,
  }) {
    return DeviceControlUserSettings(
      sn: sn ?? this.sn,
      cabinTempSetValue: cabinTempSetValue ?? this.cabinTempSetValue,
      cabinTempIsOn: cabinTempIsOn ?? this.cabinTempIsOn,
      cabinHumiditySetValue: cabinHumiditySetValue ?? this.cabinHumiditySetValue,
      cabinHumidityIsOn: cabinHumidityIsOn ?? this.cabinHumidityIsOn,
      oxygenConcentrationSetValue: oxygenConcentrationSetValue ?? this.oxygenConcentrationSetValue,
      oxygenConcentrationIsOn: oxygenConcentrationIsOn ?? this.oxygenConcentrationIsOn,
      co2AlarmThreshold: co2AlarmThreshold ?? this.co2AlarmThreshold,
      careLevelValue: careLevelValue ?? this.careLevelValue,
      freshAirModeValue: freshAirModeValue ?? this.freshAirModeValue,
      windSpeedModeValue: windSpeedModeValue ?? this.windSpeedModeValue,
      nebulizationTimeMinutes: nebulizationTimeMinutes ?? this.nebulizationTimeMinutes,
      disinfectionTimeMinutes: disinfectionTimeMinutes ?? this.disinfectionTimeMinutes,
      openOxygenIsOn: openOxygenIsOn ?? this.openOxygenIsOn,
      circulationModeValue: circulationModeValue ?? this.circulationModeValue,
      checkLightOn: checkLightOn ?? this.checkLightOn,
      illuminationOn: illuminationOn ?? this.illuminationOn,
      blueLightOn: blueLightOn ?? this.blueLightOn,
      redLightOn: redLightOn ?? this.redLightOn,
    );
  }

  /// 将用户设置应用到 DeviceControlBO 上
  /// MCU获取的值不受影响，仅覆盖用户可配置的字段
  DeviceControlBO applyTo(DeviceControlBO bo) {
    return bo.copyWith(
      sn: sn ?? bo.sn,
      cabinTemp: bo.cabinTemp.copyWith(
        setValue: cabinTempSetValue ?? bo.cabinTemp.setValue,
        isOn: cabinTempIsOn ?? bo.cabinTemp.isOn,
      ),
      cabinHumidity: bo.cabinHumidity.copyWith(
        setValue: cabinHumiditySetValue ?? bo.cabinHumidity.setValue,
        isOn: cabinHumidityIsOn ?? bo.cabinHumidity.isOn,
      ),
      oxygenConcentration: bo.oxygenConcentration.copyWith(
        setValue: oxygenConcentrationSetValue ?? bo.oxygenConcentration.setValue,
        isOn: oxygenConcentrationIsOn ?? bo.oxygenConcentration.isOn,
      ),
      co2Concentration: bo.co2Concentration.copyWith(
        alarmThreshold: co2AlarmThreshold ?? bo.co2Concentration.alarmThreshold,
      ),
      careLevel: careLevelValue != null
          ? bo.careLevel.copyWith(currentLevel: careLevelValue)
          : null,
      freshAir: freshAirModeValue != null
          ? bo.freshAir.copyWith(mode: freshAirModeValue)
          : null,
      windSpeed: windSpeedModeValue != null
          ? bo.windSpeed.copyWith(currentMode: windSpeedModeValue)
          : null,
      nebulizationTime: nebulizationTimeMinutes != null
          ? bo.nebulizationTime.copyWith(setMinutes: nebulizationTimeMinutes)
          : null,
      disinfectionTime: disinfectionTimeMinutes != null
          ? bo.disinfectionTime.copyWith(setMinutes: disinfectionTimeMinutes)
          : null,
      openOxygen: openOxygenIsOn != null
          ? bo.openOxygen.copyWith(isOn: openOxygenIsOn)
          : null,
      circulationMode: circulationModeValue != null
          ? bo.circulationMode.copyWith(currentMode: circulationModeValue)
          : null,
      lightControl: bo.lightControl.copyWith(
        checkLightOn: checkLightOn ?? bo.lightControl.checkLightOn,
        illuminationOn: illuminationOn ?? bo.lightControl.illuminationOn,
        blueLightOn: blueLightOn ?? bo.lightControl.blueLightOn,
        redLightOn: redLightOn ?? bo.lightControl.redLightOn,
      ),
    );
  }

  factory DeviceControlUserSettings.fromJson(Map<String, dynamic> json) {
    return DeviceControlUserSettings(
      sn: json['sn'] as String?,
      cabinTempSetValue: json['cabinTempSetValue'] as String?,
      cabinTempIsOn: json['cabinTempIsOn'] as bool?,
      cabinHumiditySetValue: json['cabinHumiditySetValue'] as String?,
      cabinHumidityIsOn: json['cabinHumidityIsOn'] as bool?,
      oxygenConcentrationSetValue: json['oxygenConcentrationSetValue'] as String?,
      oxygenConcentrationIsOn: json['oxygenConcentrationIsOn'] as bool?,
      co2AlarmThreshold: json['co2AlarmThreshold'] as String?,
      careLevelValue: json['careLevelValue'] != null
          ? CareLevel.values.firstWhere(
              (e) => e.name == json['careLevelValue'] as String,
              orElse: () => CareLevel.special,
            )
          : null,
      freshAirModeValue: json['freshAirModeValue'] != null
          ? FreshAirMode.values.firstWhere(
              (e) => e.name == json['freshAirModeValue'] as String,
              orElse: () => FreshAirMode.auto,
            )
          : null,
      windSpeedModeValue: json['windSpeedModeValue'] != null
          ? WindSpeedMode.values.firstWhere(
              (e) => e.name == json['windSpeedModeValue'] as String,
              orElse: () => WindSpeedMode.sleep,
            )
          : null,
      nebulizationTimeMinutes: json['nebulizationTimeMinutes'] as String?,
      disinfectionTimeMinutes: json['disinfectionTimeMinutes'] as String?,
      openOxygenIsOn: json['openOxygenIsOn'] as bool?,
      circulationModeValue: json['circulationModeValue'] != null
          ? CirculationMode.values.firstWhere(
              (e) => e.name == json['circulationModeValue'] as String,
              orElse: () => CirculationMode.external,
            )
          : null,
      checkLightOn: json['checkLightOn'] as bool?,
      illuminationOn: json['illuminationOn'] as bool?,
      blueLightOn: json['blueLightOn'] as bool?,
      redLightOn: json['redLightOn'] as bool?,
    );
  }

  Map<String, dynamic> toJson() {
    return {
      if (sn != null) 'sn': sn,
      if (cabinTempSetValue != null) 'cabinTempSetValue': cabinTempSetValue,
      if (cabinTempIsOn != null) 'cabinTempIsOn': cabinTempIsOn,
      if (cabinHumiditySetValue != null) 'cabinHumiditySetValue': cabinHumiditySetValue,
      if (cabinHumidityIsOn != null) 'cabinHumidityIsOn': cabinHumidityIsOn,
      if (oxygenConcentrationSetValue != null) 'oxygenConcentrationSetValue': oxygenConcentrationSetValue,
      if (oxygenConcentrationIsOn != null) 'oxygenConcentrationIsOn': oxygenConcentrationIsOn,
      if (co2AlarmThreshold != null) 'co2AlarmThreshold': co2AlarmThreshold,
      if (careLevelValue != null) 'careLevelValue': careLevelValue!.name,
      if (freshAirModeValue != null) 'freshAirModeValue': freshAirModeValue!.name,
      if (windSpeedModeValue != null) 'windSpeedModeValue': windSpeedModeValue!.name,
      if (nebulizationTimeMinutes != null) 'nebulizationTimeMinutes': nebulizationTimeMinutes,
      if (disinfectionTimeMinutes != null) 'disinfectionTimeMinutes': disinfectionTimeMinutes,
      if (openOxygenIsOn != null) 'openOxygenIsOn': openOxygenIsOn,
      if (circulationModeValue != null) 'circulationModeValue': circulationModeValue!.name,
      if (checkLightOn != null) 'checkLightOn': checkLightOn,
      if (illuminationOn != null) 'illuminationOn': illuminationOn,
      if (blueLightOn != null) 'blueLightOn': blueLightOn,
      if (redLightOn != null) 'redLightOn': redLightOn,
    };
  }

  @override
  List<Object?> get props => [
        sn,
        cabinTempSetValue,
        cabinTempIsOn,
        cabinHumiditySetValue,
        cabinHumidityIsOn,
        oxygenConcentrationSetValue,
        oxygenConcentrationIsOn,
        co2AlarmThreshold,
        careLevelValue,
        freshAirModeValue,
        windSpeedModeValue,
        nebulizationTimeMinutes,
        disinfectionTimeMinutes,
        openOxygenIsOn,
        circulationModeValue,
        checkLightOn,
        illuminationOn,
        blueLightOn,
        redLightOn,
      ];
}
