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

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

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

  List<AppointmentFilter> appointmentFilter = List();

  getEyeMeasurement() async {
    setState(ViewState.Busy);
    await _eyeService.getEyeMeasurement();
    if (_eyeService.hasError) {
      error = _eyeService.error;
      setState(ViewState.Error);
    } 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);
    }
  }
}
