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

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

  List<PatientDoctorAppointmentList> patientDoctorAppointmentListHospital = List();

  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) {
        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 getCallInfoHoursResult({int projectId, 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({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({DoctorList doctorList, String requestType, 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);
  }
}
