import 'package:doctor_app_flutter/core/enum/view_state.dart';
import 'package:doctor_app_flutter/core/service/patient_medical_file/medical_report/PatientMedicalReportService.dart';
import 'package:doctor_app_flutter/core/viewModel/base_view_model.dart';
import 'package:doctor_app_flutter/core/model/patient/MedicalReport/MedicalReportTemplate.dart';
import 'package:doctor_app_flutter/core/model/patient/MedicalReport/MeidcalReportModel.dart';
import 'package:doctor_app_flutter/core/model/patient/patiant_info_model.dart';

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

class PatientMedicalReportViewModel extends BaseViewModel {
  PatientMedicalReportService _service = locator<PatientMedicalReportService>();

  List<MedicalReportModel> get medicalReportList => _service.medicalReportList;

  List<MedicalReportTemplate> get medicalReportTemplate =>
      _service.medicalReportTemplate;

  Future getMedicalReportList(PatiantInformtion patient,{bool isVidaPlusProject = false}) async {
    setState(ViewState.Busy);
    await _service.getMedicalReportList(patient, isVidaPlusProject);
    if (_service.hasError) {
      error = _service.error;
      setState(ViewState.ErrorLocal); // ViewState.Error
    } else
      setState(ViewState.Idle);
  }

  Future getMedicalReportTemplate() async {
    setState(ViewState.Busy);
    await _service.getMedicalReportTemplate();
    if (_service.hasError) {
      error = _service.error;
      setState(ViewState.ErrorLocal);
    } else
      setState(ViewState.Idle);
  }

  Future insertMedicalReport(PatiantInformtion patient, String htmlText) async {
    setState(ViewState.Busy);
    await _service.insertMedicalReport(patient, htmlText);
    if (_service.hasError) {
      error = _service.error;
      setState(ViewState.ErrorLocal);
    } else
      setState(ViewState.Idle);
  }

  Future verifyMedicalReport(
      PatiantInformtion patient, MedicalReportModel medicalReport, bool? isVidaPlusProject) async {
    setState(ViewState.Busy);
    print("the isVidaPlusProject flag in VM is $isVidaPlusProject");
    await _service.verifyMedicalReport(patient, medicalReport, isVidaPlusProject);
    if (_service.hasError) {
      error = _service.error;
      setState(ViewState.ErrorLocal);
    } else
      await getMedicalReportList(patient, isVidaPlusProject: isVidaPlusProject == true);
    setState(ViewState.Idle);
  }

  Future addMedicalReport(PatiantInformtion patient, String htmlText, bool? isVidaPlusProject) async {
    setState(ViewState.Busy);
    await _service.addMedicalReport(patient, htmlText, isVidaPlusProject);
    if (_service.hasError) {
      error = _service.error;
      await getMedicalReportList(patient, isVidaPlusProject: isVidaPlusProject == true);
      setState(ViewState.ErrorLocal);
    } else {
      await getMedicalReportList(patient,isVidaPlusProject: isVidaPlusProject == true);
      setState(ViewState.Idle);
    }
  }

  Future updateMedicalReport(PatiantInformtion patient, String htmlText,
      int limitNumber, String invoiceNumber, bool? isVidaPlusProject, int? summaryId, MedicalReportModel medicalReportModel) async {
    setState(ViewState.Busy);
    await _service.updateMedicalReport(
        patient, htmlText, limitNumber, invoiceNumber, summaryId, medicalReportModel);
    if (_service.hasError) {
      error = _service.error;
      await getMedicalReportList(patient, isVidaPlusProject:  isVidaPlusProject == true);
      setState(ViewState.ErrorLocal);
    } else {
      await getMedicalReportList(patient,isVidaPlusProject:  isVidaPlusProject == true);
      setState(ViewState.Idle);
    }
  }
}
