import 'package:hmg_patient_app/config/config.dart';
import 'package:hmg_patient_app/core/enum/viewstate.dart';
import 'package:hmg_patient_app/core/model/ask_doctor/AskDoctorReqTypes.dart';
import 'package:hmg_patient_app/core/model/ask_doctor/DoctorResponse.dart';
import 'package:hmg_patient_app/core/service/medical/ask_doctor_services.dart';
import 'package:hmg_patient_app/core/service/medical/my_doctor_service.dart';
import 'package:hmg_patient_app/core/viewModels/base_view_model.dart';
import 'package:hmg_patient_app/locator.dart';
import 'package:hmg_patient_app/models/Appointments/DoctorListResponse.dart';
import 'package:hmg_patient_app/uitl/app_toast.dart';
import 'package:hmg_patient_app/uitl/translations_delegate_base.dart';

class AskDoctorViewModel extends BaseViewModel {
  AskDoctorService _askDoctorService = locator<AskDoctorService>();
  MyDoctorService _myDoctorService = locator<MyDoctorService>();

  List<PatientDoctorAppointmentList> patientDoctorAppointmentListHospital = [];

  List<AskDoctorReqTypes> get askDoctorReqTypes => _askDoctorService.askDoctorReqTypes;

  List<DoctorResponse> get doctorResponseList => _askDoctorService.doctorResponseList;

  Future getMyDoctor() async {
    setState(ViewState.Busy);
    await _myDoctorService.getPatientDoctorAppointmentList(top: 25, beforeDays: 15);
    if (_myDoctorService.hasError) {
      error = _myDoctorService.error!;
      setState(ViewState.Error);
    } else if (_myDoctorService.patientDoctorAppointmentList.length != 0) {
      _myDoctorService.patientDoctorAppointmentList.forEach((element) {
        if (!element.isLiveCareClinic!) {
          List<PatientDoctorAppointmentList> doctorByClinic = patientDoctorAppointmentListHospital.where((elementClinic) => elementClinic.filterName == element.projectName).toList();

          if (doctorByClinic.length != 0) {
            patientDoctorAppointmentListHospital[patientDoctorAppointmentListHospital.indexOf(doctorByClinic[0])].patientDoctorAppointmentList!.add(element);
          } else {
            patientDoctorAppointmentListHospital.add(PatientDoctorAppointmentList(filterName: element.projectName, patientDoctorAppointment: element));
          }
        }
        setState(ViewState.Idle);
      });
    } else {
      setState(ViewState.Idle);
      // AppToast.showErrorToast(message: TranslationBase.of(AppGlobal.context).askDocEmpty);
    }
  }

  Future getCallRequestTypeLOVs() async {
    setState(ViewState.Busy);
    await _askDoctorService.getCallRequestTypeLOV();
    if (_askDoctorService.hasError) {
      error = _askDoctorService.error!;
      setState(ViewState.ErrorLocal);
      AppToast.showErrorToast(message: error);
    } else
      setState(ViewState.Idle);
  }

  Future getQuestionTypes() async {
    setState(ViewState.Busy);
    await _askDoctorService.getQuestionTypes();
    if (_askDoctorService.hasError) {
      error = _askDoctorService.error!;
      setState(ViewState.ErrorLocal);
      AppToast.showErrorToast(message: error);
    } else
      setState(ViewState.Idle);
  }

  Future getCallInfoHoursResult({required int projectId, required int doctorId}) async {
    setState(ViewState.Busy);
    await _askDoctorService.getCallInfoHoursResult(projectId: projectId, doctorId: doctorId);
    if (_askDoctorService.hasError) {
      error = _askDoctorService.error!;
      setState(ViewState.ErrorLocal);
    } else
      setState(ViewState.Idle);
  }

  getDoctorResponse() async {
    setState(ViewState.Busy);
    await _askDoctorService.getDoctorResponse();
    if (_askDoctorService.hasError) {
      error = _askDoctorService.error!;
      setState(ViewState.Error);
    } else
      setState(ViewState.Idle);
  }

  Future updateReadStatus({required int transactionNo}) async {
    setState(ViewState.Busy);
    await _askDoctorService.updateReadStatus(transactionNo: transactionNo);
    if (_askDoctorService.hasError) {
      error = _askDoctorService.error!;
      setState(ViewState.Error);
    } else
      setState(ViewState.Idle);
  }

  Future sendRequestLOV({required DoctorList doctorList, required String requestType, required String remark}) async {
    setState(ViewState.BusyLocal);
    await _askDoctorService.sendRequestLOV(doctorList: doctorList, requestType: requestType, remark: remark);
    if (_askDoctorService.hasError) {
      error = _askDoctorService.error!;
      setState(ViewState.ErrorLocal);
      AppToast.showErrorToast(message: error);
    } else
      setState(ViewState.Idle);
  }
}
