import 'package:equatable/equatable.dart';
import 'package:laki_icu_app/utils/bluetooth/mcu_report.dart';

class BluetoothReadModel extends Equatable {
  static const empty = BluetoothReadModel();

  final String? sn;
  final double? mainTemperature;
  final int? humidity;
  final int? oxygenConcentration;
  final int? co2Measurement;
  final DateTime? updatedAt;

  const BluetoothReadModel({
    this.sn,
    this.mainTemperature,
    this.humidity,
    this.oxygenConcentration,
    this.co2Measurement,
    this.updatedAt,
  });

  factory BluetoothReadModel.fromMcuReport(McuReport report) {
    return BluetoothReadModel(
      sn: report.sn.trim().isEmpty ? null : report.sn.trim(),
      mainTemperature:
          report.mainTemperature.isNaN ? null : report.mainTemperature,
      humidity: report.humidity < 0 ? null : report.humidity,
      oxygenConcentration:
          report.oxygenConcentration < 0 ? null : report.oxygenConcentration,
      co2Measurement: report.co2Measurement < 0 ? null : report.co2Measurement,
      updatedAt: DateTime.now(),
    );
  }

  bool get hasSn => sn != null && sn!.isNotEmpty;

  BluetoothReadModel copyWith({
    String? sn,
    double? mainTemperature,
    int? humidity,
    int? oxygenConcentration,
    int? co2Measurement,
    DateTime? updatedAt,
    bool clearSn = false,
  }) {
    return BluetoothReadModel(
      sn: clearSn ? null : sn ?? this.sn,
      mainTemperature: mainTemperature ?? this.mainTemperature,
      humidity: humidity ?? this.humidity,
      oxygenConcentration: oxygenConcentration ?? this.oxygenConcentration,
      co2Measurement: co2Measurement ?? this.co2Measurement,
      updatedAt: updatedAt ?? this.updatedAt,
    );
  }

  @override
  List<Object?> get props => [
        sn,
        mainTemperature,
        humidity,
        oxygenConcentration,
        co2Measurement,
        updatedAt,
      ];
}
