import 'package:doctor_app_flutter/core/enum/view_state.dart';
import 'package:doctor_app_flutter/core/service/patient_medical_file/vital_sign/patient-vital-signs-service.dart';
import 'package:doctor_app_flutter/core/viewModel/base_view_model.dart';
import 'package:doctor_app_flutter/core/model/patient/patiant_info_model.dart';
import 'package:doctor_app_flutter/core/model/patient/vital_sign/patient-vital-sign-data.dart';
import 'package:doctor_app_flutter/core/model/patient/vital_sign/patient-vital-sign-history.dart';
import 'package:doctor_app_flutter/utils/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";
  String oxygenation = "0";
  String painScore = "-";
  String painLocation = "-";
  String painCharacter = "-";
  String painDuration = "-";
  String isPainDone = "-";
  String painFrequency = "-";

  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, bool isInPatient) async {
    setState(ViewState.Busy);
    if (from == null || from == "0") {
      from = AppDateUtils.convertDateToFormat(DateTime.now(), 'yyyy-MM-dd');
    }
    if (to == null || to == "0") {
      to = AppDateUtils.convertDateToFormat(DateTime.now(), 'yyyy-MM-dd');
    }

    if (isInPatient) {
      await _vitalSignService.getPatientVitalSignsHistory(patient, from, to);
    } else {
      await _vitalSignService.getOutPatientVitalSignHistory(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();
        }
        if (oxygenation == "0" || oxygenation == null || oxygenation == 'null') {
          oxygenation = "${element.sAO2.toString()}"; /* - ${element.fIO2.toString()}*/
        }
        if (painScore == null || painScore == "-") {
          painScore = element.painScoreDesc.toString() != 'null' ? element.painScoreDesc.toString() : "-";
          painLocation = element.painLocation.toString() != 'null' ? element.painLocation.toString() : "-";
          painCharacter = element.painCharacter.toString() != 'null' ? element.painCharacter.toString() : "-";
          painDuration = element.painDuration.toString() != 'null' ? element.painDuration.toString() : "-";
          isPainDone = element.isPainManagementDone.toString() != 'null' ? element.isPainManagementDone.toString() : "-";
          painFrequency = element.painFrequency.toString() != 'null' ? element.painFrequency.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";
    }
  }

  int getBMIStatus() {
    var bodyMassIndex = double.parse(bodyMax);
    if (bodyMassIndex < 18.5) {
      return 1;
    } else if (bodyMassIndex >= 18.5 && bodyMassIndex < 24.9) {
      return 2;
    } else if (bodyMassIndex >= 24.9 && bodyMassIndex < 29.9) {
      return 3;
    } else if (bodyMassIndex >= 29.9 && bodyMassIndex < 34.9) {
      return 4;
    } else {
      return 5;
    }
  }

  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";
    }
  }
}
