import 'package:doctor_app_flutter/core/enum/viewstate.dart';
import 'package:doctor_app_flutter/core/service/patient-vital-signs-service.dart';
import 'package:doctor_app_flutter/core/viewModel/base_view_model.dart';
import 'package:doctor_app_flutter/models/patient/PatientArrivalEntity.dart';
import 'package:doctor_app_flutter/models/patient/patiant_info_model.dart';
import 'package:doctor_app_flutter/models/patient/vital_sign/patient-vital-sign-data.dart';
import 'package:doctor_app_flutter/models/patient/vital_sign/patient-vital-sign-history.dart';
import 'package:doctor_app_flutter/models/patient/vital_sign/vital_sign_res_model.dart';
import 'package:doctor_app_flutter/util/date-utils.dart';

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

class VitalSignsViewModel extends BaseViewModel {
  VitalSignsService _vitalSignService = locator<VitalSignsService>();

  VitalSignData get patientVitalSigns => _vitalSignService.patientVitalSigns;

  List<VitalSignHistory> get patientVitalSignsHistory =>
      _vitalSignService.patientVitalSignsHistory;

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

  Future getPatientVitalSign(PatiantInformtion patient) async {
    setState(ViewState.Busy);
    await _vitalSignService.getPatientVitalSign(patient);
    if (_vitalSignService.hasError) {
      error = _vitalSignService.error;
      setState(ViewState.Error);
    } else {
      setState(ViewState.Idle);
    }
  }

  Future getPatientVitalSignHistory(
      PatiantInformtion patient, String from, String to) async {
    setState(ViewState.Busy);
    if (from == null || from == "0") {
      from = DateUtils.convertDateToFormat(DateTime.now(), 'yyyy-MM-dd');
    }
    if (to == null || to == "0") {
      to = DateUtils.convertDateToFormat(DateTime.now(), 'yyyy-MM-dd');
    }

    await _vitalSignService.getPatientVitalSignsHistory(patient, from, to);
    if (_vitalSignService.hasError) {
      error = _vitalSignService.error;
      setState(ViewState.Error);
    } else {
      patientVitalSignsHistory.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);
    }
  }

  String getBMI(var bodyMassIndex) {
    if (bodyMassIndex <= 18.5) {
      return "Underweight";
    } else if (bodyMassIndex <= 25.0) {
      return "Normal";
    } else if (bodyMassIndex <= 30) {
      return "Overweight";
    } else {
      // > 30.0
      return "Obese";
    }
  }

  String getTempratureMethod(int temperatureCelciusMethod) {
    // temperatureCelciusMethod ( vital sign response field )- master 2005
    if (temperatureCelciusMethod == 1) {
      return "Oral";
    } else if (temperatureCelciusMethod == 2) {
      return "Axilla";
    } else if (temperatureCelciusMethod == 3) {
      return "Rectal";
    } else if (temperatureCelciusMethod == 4) {
      return "Tympanic";
    } else if (temperatureCelciusMethod == 5) {
      return "Temporal";
    }
  }
}
