import 'package:diplomaticquarterapp/core/enum/viewstate.dart';
import 'package:diplomaticquarterapp/core/model/vital_sign/vital_sign_res_model.dart';
import 'package:diplomaticquarterapp/core/service/medical/vital_sign_service.dart';
import 'package:diplomaticquarterapp/core/viewModels/base_view_model.dart';

import '../../../locator.dart';

class VitalSignViewModel extends BaseViewModel {
  VitalSignService _vitalSignService = locator<VitalSignService>();

  List<VitalSignResModel> get vitalSignResModelList =>
      _vitalSignService.vitalSignResModelList;

  String heightCm = "0";
  String weightKg = "0";
  String bodyMax = "0";
  String temperatureCelcius = "0";
  String hartRat = "0";
  String respirationBeatPerMinute = "0";
  String bloodPressure = "0 / 0";

  getPatientVitalSign({int appointmentNo, int projectID}) async {
    setState(ViewState.Busy);

    if (appointmentNo != null && projectID != null) {
      await _vitalSignService.getPatientRadOrders(
          appointmentNo: appointmentNo, projectID: projectID);
    } else {
      await _vitalSignService.getPatientRadOrders();
    }
    if (_vitalSignService.hasError) {
      error = _vitalSignService.error;
      setState(ViewState.Error);
    } else {
      _vitalSignService.vitalSignResModelList.forEach((element) {
        if (heightCm == "0" || heightCm == null || heightCm == 'null') {
          heightCm = element.heightCm.toString();
        }
        if (weightKg == "0" || weightKg == null || weightKg == 'null') {
          weightKg = element.weightKg.toString();
        }
        if (bodyMax == "0" ||
            bodyMax == null ||
            bodyMax == 'null') {
          bodyMax = element.bodyMassIndex.toString();
        }
        if (temperatureCelcius == "0" ||
            temperatureCelcius == null ||
            temperatureCelcius == 'null') {
          temperatureCelcius = element.temperatureCelcius.toString();
        }
        if (hartRat == "0" || hartRat == null || hartRat == 'null') {
          hartRat = element.pulseBeatPerMinute.toString();
        }
        if (respirationBeatPerMinute == "0" ||
            respirationBeatPerMinute == null ||
            respirationBeatPerMinute == 'null') {
          respirationBeatPerMinute =
              element.respirationBeatPerMinute.toString();
        }
        if (bloodPressure == "0 / 0" ||
            bloodPressure == null ||
            bloodPressure == 'null') {
          bloodPressure = element.bloodPressure.toString();
        }
      });

      setState(ViewState.Idle);
    }
  }
}
