class Vitals {
  String value;
  final String timestamp;
  final String unitOfMeasure;

  Vitals({
    required this.value,
    required this.timestamp,
    this.unitOfMeasure = "",
  });

  Map<String, dynamic> toJson() {
    return {
      'value': value,
      'timestamp': timestamp,
      'uom': unitOfMeasure,
    };
  }

  factory Vitals.fromMap(Map<dynamic, dynamic> map) {
    String timestamp = map['timestamp'] ?? map['timeStamp'] ?? "";
    if (timestamp.isEmpty) {
      timestamp = DateTime.now().toIso8601String();
    }
    return Vitals(
      value: map['value'] ?? "",
      timestamp: timestamp,
      unitOfMeasure: map['uom'] ?? "",
    );
  }
}

class VitalsWRTType {
  final List<Vitals> heartRate;
  final List<Vitals> sleep;
  final List<Vitals> step;
  final List<Vitals> distance;
  final List<Vitals> activity;
  final List<Vitals> bodyOxygen;
  final List<Vitals> bodyTemperature;
  double maxHeartRate = double.negativeInfinity;
  double maxSleep = double.negativeInfinity;
  double maxStep = double.negativeInfinity;
  double maxActivity = double.negativeInfinity;
  double maxBloodOxygen = double.negativeInfinity;
  double maxBodyTemperature = double.negativeInfinity;

  VitalsWRTType(
      {required this.distance,
      required this.bodyOxygen,
      required this.bodyTemperature,
      required this.heartRate,
      required this.sleep,
      required this.step,
      required this.activity});

  factory VitalsWRTType.fromMap(Map<dynamic, dynamic> map) {
    List<Vitals> activity = [];
    List<Vitals> steps = [];
    List<Vitals> sleeps = [];
    List<Vitals> heartRate = [];
    List<Vitals> bodyOxygen = [];
    List<Vitals> distance = [];
    List<Vitals> bodyTemperature = [];
    map["activity"].forEach((element) {
      element["uom"] = "Kcal";
      var data = Vitals.fromMap(element);
      activity.add(data);
    });
    map["steps"].forEach((element) {
      element["uom"] = "";

      steps.add(Vitals.fromMap(element));
    });
    map["sleep"].forEach((element) {
      element["uom"] = "hr";
      sleeps.add(Vitals.fromMap(element));
    });
    map["heartRate"].forEach((element) {
      element["uom"] = "bpm";

      heartRate.add(Vitals.fromMap(element));
    });
    map["bloodOxygen"].forEach((element) {
      element["uom"] = "";

      bodyOxygen.add(Vitals.fromMap(element));
    });

    map["bodyTemperature"].forEach((element) {
      element["uom"] = "C";
      bodyTemperature.add(Vitals.fromMap(element));
    });

    map["distance"].forEach((element) {
      element["uom"] = "km";
      var data = Vitals.fromMap(element);
      data.value = (double.parse(data.value) / 1000).toStringAsFixed(2);
      distance.add(data);
    });

    return VitalsWRTType(
        bodyTemperature: bodyTemperature,
        bodyOxygen: bodyOxygen,
        heartRate: heartRate,
        sleep: sleeps,
        step: steps,
        activity: activity,
        distance: distance);
  }

  Map<String, List<Vitals>> getVitals() {
    return {
      "heartRate": heartRate,
      "sleep": sleep,
      "steps": step,
      "activity": activity,
      "bodyOxygen": bodyOxygen,
      "bodyTemperature": bodyTemperature,
      "distance": distance,
    };
  }
}
