import 'package:doctor_app_flutter/config/shared_pref_kay.dart';
import 'package:doctor_app_flutter/core/enum/view_state.dart';
import 'package:doctor_app_flutter/core/model/live_care/AlternativeServicesList.dart';
import 'package:doctor_app_flutter/core/model/live_care/PendingPatientERForDoctorAppRequestModel.dart';
import 'package:doctor_app_flutter/core/model/live_care/live_care_login_reguest_model.dart';
import 'package:doctor_app_flutter/core/model/livecare/end_call_req.dart';
import 'package:doctor_app_flutter/core/model/livecare/start_call_req.dart';
import 'package:doctor_app_flutter/core/model/livecare/start_call_res.dart';
import 'package:doctor_app_flutter/core/model/patient/patiant_info_model.dart';
import 'package:doctor_app_flutter/core/service/home/dasboard_service.dart';
import 'package:doctor_app_flutter/core/service/patient/LiveCarePatientServices.dart';
import 'package:doctor_app_flutter/core/viewModel/base_view_model.dart';

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

class LiveCarePatientViewModel extends BaseViewModel {
  List<PatiantInformtion> filterData = [];

  LiveCarePatientServices _liveCarePatientServices = locator<LiveCarePatientServices>();

  StartCallRes? get startCallRes => _liveCarePatientServices.startCallRes;

  List<AlternativeService> get alternativeServicesList => _liveCarePatientServices.alternativeServicesList;

  DashboardService _dashboardService = locator<DashboardService>();

  getPendingPatientERForDoctorApp({bool isFromTimer = false}) async {
    if (isFromTimer) {
      setState(ViewState.Idle);
    } else {
      setState(ViewState.BusyLocal);
    }

    PendingPatientERForDoctorAppRequestModel pendingPatientERForDoctorAppRequestModel = PendingPatientERForDoctorAppRequestModel(sErServiceID: _dashboardService.sServiceID, outSA: false);
    await _liveCarePatientServices.getPendingPatientERForDoctorApp(pendingPatientERForDoctorAppRequestModel);
    if (_liveCarePatientServices.hasError) {
      error = _liveCarePatientServices.error;

      setState(ViewState.ErrorLocal);
    } else {
      filterData = _liveCarePatientServices.patientList;
      setState(ViewState.Idle);
    }
  }

  Future endCall(int vCID, bool isPatient) async {
    await getDoctorProfile(isGetProfile: true);
    EndCallReq endCallReq = new EndCallReq();
    endCallReq.doctorId = doctorProfile!.doctorID;
    endCallReq.generalid = 'Cs2020@2016\$2958';
    endCallReq.vCID = vCID;
    endCallReq.isDestroy = isPatient;

    setState(ViewState.BusyLocal);
    await _liveCarePatientServices.endCall(endCallReq);
    if (_liveCarePatientServices.hasError) {
      error = _liveCarePatientServices.error;
      setState(ViewState.ErrorLocal);
    } else {
      setState(ViewState.Idle);
    }
  }

  getToken() async {
    String token = await sharedPref.getString(TOKEN);
    return token;
  }

  Future startCall({required int vCID, required bool isReCall}) async {
    StartCallReq startCallReq = new StartCallReq();
    await getDoctorProfile();
    startCallReq.clinicId = super.doctorProfile!.clinicID;
    startCallReq.vCID = vCID; //["VC_ID"];
    startCallReq.isrecall = isReCall;
    startCallReq.doctorId = doctorProfile!.doctorID;
    startCallReq.isOutKsa = false; //["IsOutKSA"];
    startCallReq.projectName = doctorProfile!.projectName;
    startCallReq.docotrName = doctorProfile!.doctorName;
    startCallReq.clincName = doctorProfile!.clinicDescription;
    startCallReq.docSpec = doctorProfile!.doctorTitleForProfile;
    startCallReq.generalid = 'Cs2020@2016\$2958';

    setState(ViewState.BusyLocal);
    await _liveCarePatientServices.startCall(startCallReq);
    if (_liveCarePatientServices.hasError) {
      error = _liveCarePatientServices.error;
      setState(ViewState.ErrorLocal);
    } else {
      setState(ViewState.Idle);
    }
  }

  setSelectedCheckboxValues(AlternativeService service, bool isSelected) {
    int index = alternativeServicesList.indexOf(service);
    if (index != -1) alternativeServicesList[index].isSelected = isSelected;
    notifyListeners();
  }

  Future endCallWithCharge(int vcID, bool isConfirmed) async {
    setState(ViewState.BusyLocal);

    List<int> selectedServices = [];
    if (isConfirmed) {
      selectedServices = getSelectedAlternativeServices();
    }

    await _liveCarePatientServices.endCallWithCharge(vcID, selectedServices);
    if (_liveCarePatientServices.hasError) {
      error = _liveCarePatientServices.error;
      setState(ViewState.ErrorLocal);
    } else {
      await getPendingPatientERForDoctorApp();
      setState(ViewState.Idle);
    }
  }

  List<int> getSelectedAlternativeServices() {
    List<int> selectedServices = [];
    for (AlternativeService service in alternativeServicesList) {
      if (service.isSelected!) {
        selectedServices.add(service.serviceID!);
      }
    }
    return selectedServices;
  }

  Future getAlternativeServices(int vcID) async {
    setState(ViewState.BusyLocal);
    await _liveCarePatientServices.getAlternativeServices(vcID);
    if (_liveCarePatientServices.hasError) {
      error = _liveCarePatientServices.error;
      setState(ViewState.ErrorLocal);
    } else {
      setState(ViewState.Idle);
    }
  }

  Future transferToAdmin(int vcID, String notes) async {
    setState(ViewState.BusyLocal);
    await _liveCarePatientServices.transferToAdmin(vcID, notes);
    if (_liveCarePatientServices.hasError) {
      error = _liveCarePatientServices.error;
      setState(ViewState.ErrorLocal);
    } else {
      await getPendingPatientERForDoctorApp();
      setState(ViewState.Idle);
    }
  }

  Future sendSMSInstruction(int vcID) async {
    setState(ViewState.BusyLocal);
    await _liveCarePatientServices.sendSMSInstruction(vcID);
    if (_liveCarePatientServices.hasError) {
      error = _liveCarePatientServices.error;
      setState(ViewState.ErrorLocal);
    } else {
      await getPendingPatientERForDoctorApp();
      setState(ViewState.Idle);
    }
  }

  searchData(String str) {
    var strExist = str.length > 0 ? true : false;
    if (strExist) {
      filterData = [];
      for (var i = 0; i < _liveCarePatientServices.patientList.length; i++) {
        String fullName = _liveCarePatientServices.patientList[i].fullName!.toUpperCase();
        String patientID = _liveCarePatientServices.patientList[i].patientId.toString();
        String mobile = _liveCarePatientServices.patientList[i].mobileNumber!.toUpperCase();

        if (fullName.contains(str.toUpperCase()) || patientID.contains(str) || mobile.contains(str)) {
          filterData.add(_liveCarePatientServices.patientList[i]);
        }
      }
      notifyListeners();
    } else {
      filterData = _liveCarePatientServices.patientList;
      notifyListeners();
    }
  }

  Future isLogin(int loginStatus) async {
    await getDoctorProfile(isGetProfile: true);

    LiveCareUserLoginRequestModel userLoginRequestModel = new LiveCareUserLoginRequestModel();
    userLoginRequestModel.isOutKsa = (doctorProfile!.projectID == 2 || doctorProfile!.projectID == 3) ? 1 : 0;
    userLoginRequestModel.isLogin = loginStatus;
    userLoginRequestModel.generalid = "Cs2020@2016\$2958";

    setState(ViewState.BusyLocal);
    await _liveCarePatientServices.isLogin(loginStatus: loginStatus, isLoginRequestModel: userLoginRequestModel);
    if (_liveCarePatientServices.hasError) {
      error = _liveCarePatientServices.error;
      setState(ViewState.ErrorLocal);
    } else {
      setState(ViewState.Idle);
    }
  }

  setDemoData() {
    alternativeServicesList.clear();
    alternativeServicesList.add(
      AlternativeService(serviceID: 1, serviceName: "Medicine Home Delivery"),
    );
    alternativeServicesList.add(
      AlternativeService(serviceID: 2, serviceName: "LABORATORY"),
    );
    alternativeServicesList.add(
      AlternativeService(serviceID: 3, serviceName: "RADIOLOGY(ULTRASOUND) For pregnant only"),
    );
    alternativeServicesList.add(
      AlternativeService(serviceID: 4, serviceName: "VACCINATIONS"),
    );
    alternativeServicesList.add(
      AlternativeService(serviceID: 5, serviceName: "ER SERVICES"),
    );
    alternativeServicesList.add(
      AlternativeService(serviceID: 6, serviceName: "PHYSIOTHERAPY"),
    );
    alternativeServicesList.add(
      AlternativeService(serviceID: 7, serviceName: "DRESSING"),
    );
    alternativeServicesList.add(
      AlternativeService(serviceID: 8, serviceName: "INJECTION"),
    );
    alternativeServicesList.add(
      AlternativeService(serviceID: 9, serviceName: "FAMILY MEDICIN DR"),
    );
    alternativeServicesList.add(
      AlternativeService(serviceID: 10, serviceName: "FOLYS CATHETER INSERTION"),
    );
    alternativeServicesList.add(
      AlternativeService(serviceID: 11, serviceName: "GASTRIC TUBE CHANGE"),
    );
  }

  updateInCallPatient({PatiantInformtion? patient, appointmentNo}) {
    _liveCarePatientServices.patientList.forEach((e) {
      if (e.patientId == patient!.patientId) {
        e.episodeNo = 0;
        e.appointmentNo = appointmentNo;
        return;
      }
    });

    setState(ViewState.Idle);
  }

  Future addPatientToDoctorList(int vcID) async {
    await getDoctorProfile(isGetProfile: true);
    setState(ViewState.BusyLocal);
    await _liveCarePatientServices.addPatientToDoctorList(vcID: vcID);
    if (_liveCarePatientServices.hasError) {
      error = _liveCarePatientServices.error;
      setState(ViewState.ErrorLocal);
    } else {
      setState(ViewState.Idle);
    }
  }

  Future removePatientFromDoctorList(int vcID) async {
    await getDoctorProfile(isGetProfile: true);
    setState(ViewState.BusyLocal);
    await _liveCarePatientServices.removePatientFromDoctorList(vcID: vcID);
    if (_liveCarePatientServices.hasError) {
      error = _liveCarePatientServices.error;
      setState(ViewState.ErrorLocal);
    } else {
      setState(ViewState.Idle);
    }
  }



}
