import 'package:hmg_patient_app/core/enum/filter_type.dart';
import 'package:hmg_patient_app/core/enum/viewstate.dart';
import 'package:hmg_patient_app/core/model/doctor/doctor_profile.dart';
import 'package:hmg_patient_app/core/model/doctor/doctor_rating.dart';
import 'package:hmg_patient_app/core/service/medical/my_doctor_service.dart';
import 'package:hmg_patient_app/models/Appointments/DoctorListResponse.dart';

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

class MyDoctorViewModel extends BaseViewModel {
  FilterType filterType = FilterType.Clinic;
  MyDoctorService _myDoctorService = locator<MyDoctorService>();

  List<PatientDoctorAppointmentList> _patientDoctorAppointmentListClinic =
     [];
  List<PatientDoctorAppointmentList> _patientDoctorAppointmentListHospital =
     [];

  List<PatientDoctorAppointmentList> get patientDoctorAppointmentList =>
      filterType == FilterType.Clinic
          ? _patientDoctorAppointmentListClinic
          : _patientDoctorAppointmentListHospital;

  DoctorList get doctorList=> _myDoctorService.doctorList!;


  DoctorProfile get doctorProfile => _myDoctorService.doctorProfile!;
  DoctorRating get doctorRating => _myDoctorService.doctorRating;

  void getMyDoctor() async {
    setState(ViewState.Busy);
    await _myDoctorService.getPatientDoctorAppointmentList();
    if (_myDoctorService.hasError) {
      error = _myDoctorService.error!;
      setState(ViewState.Error);
    } else {
      _myDoctorService.patientDoctorAppointmentList.forEach((element) {
        List<PatientDoctorAppointmentList> doctorByClinic =
            _patientDoctorAppointmentListClinic
                .where((elementClinic) =>
                    elementClinic.filterName == element.clinicName)
                .toList();

        if (doctorByClinic.length != 0) {
          _patientDoctorAppointmentListClinic[
                  _patientDoctorAppointmentListClinic
                          .indexOf(doctorByClinic[0])]
              .patientDoctorAppointmentList!
              .add(element);
        } else {
          _patientDoctorAppointmentListClinic.add(PatientDoctorAppointmentList(
              filterName: element.clinicName,
              patientDoctorAppointment: element));
        }

        //   doctor list sort via project
        List<PatientDoctorAppointmentList> doctorByHospital =
            _patientDoctorAppointmentListHospital
                .where(
                  (elementClinic) =>
                      elementClinic.filterName == element.projectName,
                )
                .toList();

        if (doctorByHospital.length != 0) {
          _patientDoctorAppointmentListHospital[_patientDoctorAppointmentListHospital.indexOf(doctorByHospital[0])]
              .patientDoctorAppointmentList!
              .add(element);
        } else {
          _patientDoctorAppointmentListHospital.add(
              PatientDoctorAppointmentList(
                  filterName: element.projectName,
                  patientDoctorAppointment: element));
        }
      });

      setState(ViewState.Idle);
    }
  }

  setFilterType(FilterType filterType) {
    this.filterType = filterType;
    notifyListeners();
  }

  Future getDoctorProfileAndRating({required int doctorId,required int clinicID,required int projectID }) async {
    setState(ViewState.Busy);
    await _myDoctorService.getDoctorProfileAndRating(doctorId: doctorId,clinicID: clinicID,projectID: projectID);
    if (_myDoctorService.hasError) {
      error = _myDoctorService.error!;
      setState(ViewState.Error);
    } else
    setState(ViewState.Idle);
  }


}
