import 'package:hmg_patient_app/core/enum/viewstate.dart';
import 'package:hmg_patient_app/core/model/AlHabibMedicalService/HealthCalculator/ClinicCategory.dart';
import 'package:hmg_patient_app/core/model/AlHabibMedicalService/HealthCalculator/DiseasesByClinic.dart';
import 'package:hmg_patient_app/core/model/health_calcolator/get_doctor_by_time_model.dart';
import 'package:hmg_patient_app/core/model/health_calcolator/get_doctor_list_calcolation_model.dart';
import 'package:hmg_patient_app/core/service/AlHabibMedicalService/health-calculator/bariatrics-service.dart';
import 'package:hmg_patient_app/core/viewModels/base_view_model.dart';
import 'package:flutter/cupertino.dart';

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

class BariatricsViewModel extends BaseViewModel {
  BariatricsService _service = locator<BariatricsService>();

  List<ClinicCategory> get clinicCategoryList => _service.clinicCategoryList;

  List<DiseasesByClinic> get diseasesList => _service.diseasesList;
  List<GetDoctorListModel> get doctorList => _service.doctorList;
  List<DoctorListByTimeModel> get doctorListByTime => _service.doctorListByTime;
  List<DoctorsListTime> doctorLists =[];

  void getClinicCategory() async {
    setState(ViewState.Busy);
    await _service.getClinicCategory();
    if (_service.hasError) {
      error = _service.error!;
      setState(ViewState.Error);
    } else {
      getDiseaseByClinicId();
    }
  }

  void getDiseaseByClinicId() async {
    setState(ViewState.Busy);
    await _service.getDiseaseByClinicId();
    if (_service.hasError) {
      error = _service.error!;
      setState(ViewState.Error);
    } else {
      setState(ViewState.Idle);
    }
  }

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

  void getDoctorList({required DiseasesByClinic disease}) async {
    setState(ViewState.Busy);
    await _service.getDoctorList(disease:disease);
    if (_service.hasError) {
      error = _service.error!;
      setState(ViewState.Error);
    } else {
      {
        _service.doctorListByTime.forEach((element) {
          List<DoctorsListTime> patientLabOrdersClinic =
              doctorLists.where((elementClinic) => elementClinic.filterName == element.projectName).toList();

          if (patientLabOrdersClinic.length != 0) {
            doctorLists[doctorLists.indexOf(patientLabOrdersClinic[0])].doctorsByTimeList.add(element);
          } else {
            doctorLists.add(DoctorsListTime(filterName: element.projectName, doctor: element));
          }
        });
        setState(ViewState.Idle);
      }
    }
  }
}
