import 'package:doctor_app_flutter/config/config.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/add_patient_to_doctor_list_request_model.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/base/base_service.dart';

class LiveCarePatientServices extends BaseService {
  List<PatiantInformtion> _patientList = [];

  List<PatiantInformtion> get patientList => _patientList;

  List<AlternativeService> alternativeServicesList = [];

  bool _isFinished = false;

  bool _isLive = true;

  bool get isFinished => _isFinished;

  setFinished(bool isFinished) {
    _isFinished = isFinished;
  }

  var endCallResponse = {};
  var transferToAdminResponse = {};
  var isLoginResponse = {};

  StartCallRes _startCallRes = StartCallRes();

  StartCallRes get startCallRes => _startCallRes;

  Future getPendingPatientERForDoctorApp(PendingPatientERForDoctorAppRequestModel pendingPatientERForDoctorAppRequestModel) async {
    hasError = false;
    await baseAppClient.post(
      GET_PENDING_PATIENT_ER_FOR_DOCTOR_APP,
      onSuccess: (dynamic response, int statusCode) {
        List<PatiantInformtion> localPatientList = [];
        _patientList =[];
        response['List_PendingPatientList'].forEach((v) {
          localPatientList.add(PatiantInformtion.fromJson(v));
        });

        /// add new items.
        localPatientList.forEach((element) {
          if (!_patientList.any((it) => it.patientId == element.patientId)) {
            _patientList.add(element);
          }
        });

        /// remove items.
        // List<PatiantInformtion> removedPatientList = [];
        // _patientList.forEach((element) {
        //   if (localPatientList.any((it) => it.patientId == element.patientId)) {
        //     removedPatientList.add(element);
        //   }
        // });

        // removedPatientList.forEach((element) {
        //   _patientList.remove(element);
        // });
      },
      onFailure: (String error, int statusCode) {
        hasError = true;
        super.error = error;
      },
      body: pendingPatientERForDoctorAppRequestModel.toJson(),
    );
  }

  Future endCall(EndCallReq endCallReq) async {
    hasError = false;
    await baseAppClient.post(END_CALL, onSuccess: (response, statusCode) async {
      endCallResponse = response;
    }, onFailure: (String error, int statusCode) {
      hasError = true;
      super.error = error;
    }, body: endCallReq.toJson(), isLiveCare: _isLive);
  }

  Future startCall(StartCallReq startCallReq) async {
    hasError = false;
    await baseAppClient.post(START_LIVE_CARE_CALL, onSuccess: (response, statusCode) async {
      _startCallRes = StartCallRes.fromJson(response);
    }, onFailure: (String error, int statusCode) {
      hasError = true;
      super.error = error;
    }, body: startCallReq.toJson(), isLiveCare: _isLive);
  }

  Future endCallWithCharge(int vcID, List<int> altServiceList) async {
    hasError = false;
    await baseAppClient.post(END_CALL_WITH_CHARGE, onSuccess: (dynamic response, int statusCode) {
      endCallResponse = response;
    }, onFailure: (String error, int statusCode) {
      hasError = true;
      super.error = error;
    }, body: {"VC_ID": vcID, "AltServiceList": altServiceList, "generalid": GENERAL_ID}, isLiveCare: _isLive);
  }

  Future transferToAdmin(int vcID, String notes) async {
    hasError = false;
    await baseAppClient.post(TRANSFERT_TO_ADMIN, onSuccess: (dynamic response, int statusCode) {
      transferToAdminResponse = response;
    }, onFailure: (String error, int statusCode) {
      hasError = true;
      super.error = error;
    }, body: {
      "VC_ID": vcID,
      "IsOutKsa": false,
      "Notes": notes,
    }, isLiveCare: _isLive);
  }

  Future sendSMSInstruction(int vcID) async {
    hasError = false;
    await baseAppClient.post(SEND_SMS_INSTRUCTIONS, onSuccess: (dynamic response, int statusCode) {
      transferToAdminResponse = response;
    }, onFailure: (String error, int statusCode) {
      hasError = true;
      super.error = error;
    }, body: {"VC_ID": vcID, "generalid": GENERAL_ID}, isLiveCare: _isLive);
  }

  Future isLogin({LiveCareUserLoginRequestModel? isLoginRequestModel, int? loginStatus}) async {
    hasError = false;
    await getDoctorProfile();
    isLoginRequestModel!.doctorId = super.doctorProfile!.doctorID;
    await baseAppClient.post(LIVE_CARE_IS_LOGIN, onSuccess: (response, statusCode) async {
      isLoginResponse = response;
    }, onFailure: (String error, int statusCode) {
      hasError = true;
      super.error = error;
    }, body: isLoginRequestModel.toJson(), isLiveCare: _isLive);
  }

  Future getAlternativeServices(int vcID) async {
    hasError = false;
    alternativeServicesList.clear();

    await baseAppClient.post(GET_ALTERNATIVE_SERVICE, onSuccess: (dynamic response, int statusCode) {
      response['AlternativeServicesList'].forEach((v) {
        alternativeServicesList.add(AlternativeService.fromJson(v));
      });
    }, onFailure: (String error, int statusCode) {
      hasError = true;
      super.error = error;
    }, body: {"VC_ID": vcID, "generalid": GENERAL_ID}, isLiveCare: _isLive);
  }

  Future addPatientToDoctorList({int? vcID}) async {
    hasError = false;
    await getDoctorProfile();
    AddPatientToDoctorListRequestModel addPatientToDoctorListRequestModel = AddPatientToDoctorListRequestModel();

    addPatientToDoctorListRequestModel.doctorId = super.doctorProfile!.doctorID;
    addPatientToDoctorListRequestModel.vCID = vcID;
    addPatientToDoctorListRequestModel.isOutKsa = false;
    addPatientToDoctorListRequestModel.generalid = GENERAL_ID;

    await baseAppClient.post(ADD_PATIENT_TO_DOCTOR, onSuccess: (response, statusCode) async {
      isLoginResponse = response;
    }, onFailure: (String error, int statusCode) {
      hasError = true;
      super.error = error;
    }, body: addPatientToDoctorListRequestModel.toJson(), isLiveCare: _isLive);
  }

  Future removePatientFromDoctorList({int? vcID}) async {
    hasError = false;
    AddPatientToDoctorListRequestModel addPatientToDoctorListRequestModel = AddPatientToDoctorListRequestModel();
    await getDoctorProfile();
    addPatientToDoctorListRequestModel.doctorId = super.doctorProfile!.doctorID;
    addPatientToDoctorListRequestModel.vCID = vcID;
    addPatientToDoctorListRequestModel.isOutKsa = false;
    addPatientToDoctorListRequestModel.generalid = GENERAL_ID;

    await baseAppClient.post(REMOVE_PATIENT_FROM_DOCTOR, onSuccess: (response, statusCode) async {
      isLoginResponse = response;
    }, onFailure: (String error, int statusCode) {
      hasError = true;
      super.error = error;
    }, body: addPatientToDoctorListRequestModel.toJson(), isLiveCare: _isLive);
  }
}
