import 'package:doctor_app_flutter/core/enum/viewstate.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/models/patient/MedicalReport/MedicalReportTemplate.dart';
import 'package:doctor_app_flutter/models/patient/MedicalReport/MeidcalReportModel.dart';
import 'package:doctor_app_flutter/models/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) async {
    setState(ViewState.Busy);
    await _service.getMedicalReportList(patient);
    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.BusyLocal);
    await _service.insertMedicalReport(patient, htmlText);
    if (_service.hasError) {
      error = _service.error!;
      setState(ViewState.ErrorLocal);
    } else
      setState(ViewState.Idle);
  }

  Future verifyMedicalReport(PatiantInformtion patient, MedicalReportModel medicalReport) async {
    setState(ViewState.Busy);
    await _service.verifyMedicalReport(patient, medicalReport);
    if (_service.hasError) {
      error = _service.error!;
      setState(ViewState.ErrorLocal);
    } else
      await getMedicalReportList(patient);
    setState(ViewState.Idle);
  }

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

  Future updateMedicalReport(PatiantInformtion patient, String htmlText, int limitNumber, String invoiceNumber) async {
    setState(ViewState.Busy);
    await _service.updateMedicalReport(patient, htmlText, limitNumber, invoiceNumber);
    if (_service.hasError) {
      error = _service.error!;
      await getMedicalReportList(patient);
      setState(ViewState.ErrorLocal);
    } else
      {
        await getMedicalReportList(patient);
    setState(ViewState.Idle);}
  }
}
