import 'package:hmg_patient_app/core/enum/viewstate.dart';
import 'package:hmg_patient_app/core/model/eye/AppoimentAllHistoryResult.dart';
import 'package:hmg_patient_app/core/model/eye/AppointmentFilter.dart';
import 'package:hmg_patient_app/core/service/medical/EyeService.dart';
import 'package:hmg_patient_app/core/viewModels/base_view_model.dart';
import 'package:hmg_patient_app/locator.dart';
import 'package:hmg_patient_app/uitl/app_toast.dart';

class EyeViewModel extends BaseViewModel {
  EyeService _eyeService = locator<EyeService>();

  List<AppoimentAllHistoryResultList> get appoimentAllHistoryResultList =>
      _eyeService.appoimentAllHistoryResultList;

  List<AppointmentFilter> appointmentFilter =[];

  getEyeMeasurement() async {
    setState(ViewState.Busy);
    await _eyeService.getEyeMeasurement();
    if (_eyeService.hasError) {
      error = _eyeService.error!;
      setState(ViewState.Error);
    } else {
      if (_eyeService.appoimentAllHistoryResultList.length == 0) {
        new AppToast().backWithEmpty();
      } else {
        _eyeService.appoimentAllHistoryResultList.forEach((element) {
          List<AppointmentFilter> _appointmentFilter = appointmentFilter
              .where(
                (elementFilter) =>
                    elementFilter.filterName == element.projectName,
              )
              .toList();

          if (_appointmentFilter.length != 0) {
            appointmentFilter[appointmentFilter.indexOf(_appointmentFilter[0])]
                .appointmentAllHistoryResultList
                .add(element);
          } else {
            appointmentFilter.add(AppointmentFilter(
              element,
              element.projectName!,
            ));
          }
        });
      }
      setState(ViewState.Idle);
    }
  }

  sendGlassesPrescriptionEmail(
      {required int appointmentNo, required String projectName, required int projectID}) async {
    setState(ViewState.Busy);
    await _eyeService.sendGlassesPrescriptionEmail(
        appointmentNo: appointmentNo,
        projectID: projectID,
        projectName: projectName);
    if (_eyeService.hasError) {
      error = _eyeService.error!;
      setState(ViewState.Error);
    } else {
      setState(ViewState.Idle);
    }
  }

  sendContactLensPrescriptionEmail(
      {required int appointmentNo, required String projectName, required int projectID}) async {
    setState(ViewState.Busy);
    await _eyeService.sendContactLensPrescriptionEmail(
        appointmentNo: appointmentNo,
        projectID: projectID,
        projectName: projectName);
    if (_eyeService.hasError) {
      error = _eyeService.error!;
      setState(ViewState.Error);
    } else {
      setState(ViewState.Idle);
    }
  }
}
