import 'package:doctor_app_flutter/core/enum/view_state.dart';
import 'package:doctor_app_flutter/core/model/doctor/list_gt_my_patients_question_model.dart';
import 'package:doctor_app_flutter/core/model/doctor/replay/request_create_doctor_response.dart';
import 'package:doctor_app_flutter/core/model/doctor/replay/request_doctor_reply.dart';
import 'package:doctor_app_flutter/core/service/home/doctor_reply_service.dart';

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

class DoctorReplayViewModel extends BaseViewModel {
  DoctorReplyService _doctorReplyService = locator<DoctorReplyService>();

  List<ListGtMyPatientsQuestions> get listDoctorWorkingHoursTable => _doctorReplyService.listDoctorWorkingHoursTable;

  List<ListGtMyPatientsQuestions> get listDoctorNotRepliedQuestions => _doctorReplyService.listDoctorNotRepliedQuestions;

  Future getDoctorReply({int pageSize = 10, int pageIndex = 1, bool isLocalBusy = true, bool isGettingNotReply = false}) async {
    if (isLocalBusy) {
      setState(ViewState.BusyLocal);
    } else {
      setState(ViewState.Busy);
    }
    await getDoctorProfile();
    RequestDoctorReply _requestDoctorReply = RequestDoctorReply(pageIndex: pageIndex, pageSize: pageSize);
    if (isGettingNotReply) _requestDoctorReply.infoStatus = 99; //to get the not replied only

    await _doctorReplyService.getDoctorReply(_requestDoctorReply, clearData: !isLocalBusy, isGettingNotReply: isGettingNotReply);
    if (_doctorReplyService.hasError) {
      error = _doctorReplyService.error;
      if (isLocalBusy) {
        setState(ViewState.ErrorLocal);
      } else {
        setState(ViewState.Error);
      }
    } else
      setState(ViewState.Idle);
  }

  Future createDoctorResponse(String response, ListGtMyPatientsQuestions model) async {
    await getDoctorProfile();
    CreateDoctorResponseModel createDoctorResponseModel = CreateDoctorResponseModel(
      transactionNo: model.transactionNo.toString(),
      doctorResponse: response,
      infoStatus: 6,
      createdBy: this.doctorProfile!.doctorID,
      infoEnteredBy: this.doctorProfile!.doctorID,
    );
    setState(ViewState.BusyLocal);
    await _doctorReplyService.createDoctorResponse(createDoctorResponseModel);
    if (_doctorReplyService.hasError) {
      error = _doctorReplyService.error;
      setState(ViewState.ErrorLocal);
    } else {
      setState(ViewState.Idle);
      _doctorReplyService.getNotRepliedCount();
    }
  }
}
