import 'package:doctor_app_flutter/config/config.dart';
import 'package:doctor_app_flutter/core/model/referral/MyReferralPatientModel.dart';
import 'package:doctor_app_flutter/core/model/referral/MyReferralPatientRequestModel.dart';
import 'package:doctor_app_flutter/core/model/referral/add_referred_remarks_request.dart';
import 'package:doctor_app_flutter/core/service/base/base_service.dart';
import 'package:doctor_app_flutter/core/model/doctor/request_add_referred_doctor_remarks.dart';

class MyReferralInPatientService extends BaseService {
  List<MyReferralPatientModel> myReferralPatients = [];

  Future getMyReferralPatientService() async {
    hasError = false;
    await getDoctorProfile();

    MyReferralPatientRequestModel myReferralPatientRequestModel = MyReferralPatientRequestModel(
        doctorID: doctorProfile!.doctorID,
        firstName: "0",
        middleName: "0",
        lastName: "0",
        patientMobileNumber: "0",
        patientIdentificationID: "0",
        patientID: 0,
        from: "0",
        to: "0",
        stamp: DateTime.now().toIso8601String(),
        isLoginForDoctorApp: true,
        patientTypeID: 1);
    myReferralPatients.clear();
    await baseAppClient.post(
      GET_MY_REFERRAL_INPATIENT,
      onSuccess: (dynamic response, int statusCode) {
        if (response['List_MyReferralPatient'] != null) {
          response['List_MyReferralPatient'].forEach((v) {
            myReferralPatients.add(MyReferralPatientModel.fromJson(v));
          });
        }
      },
      onFailure: (String error, int statusCode) {
        hasError = true;
        super.error = error;
      },
      body: myReferralPatientRequestModel.toJson(),
    );
  }

  Future getMyReferralOutPatientService() async {
    hasError = false;
    await getDoctorProfile();

    MyReferralPatientRequestModel myReferralPatientRequestModel = MyReferralPatientRequestModel(
        doctorID: doctorProfile!.doctorID,
        firstName: "0",
        middleName: "0",
        lastName: "0",
        patientMobileNumber: "0",
        patientIdentificationID: "0",
        patientID: 0,
        from: "0",
        to: "0",
        stamp: DateTime.now().toIso8601String(),
        isLoginForDoctorApp: true,
        patientTypeID: 1);
    myReferralPatients.clear();
    await baseAppClient.post(
      GET_MY_REFERRAL_OUT_PATIENT,
      onSuccess: (dynamic response, int statusCode) {
        if (response['List_MyOutPatientReferral'] != null) {
          response['List_MyOutPatientReferral'].forEach((v) {
            myReferralPatients.add(MyReferralPatientModel.fromJson(v));
          });
        }
      },
      onFailure: (String error, int statusCode) {
        hasError = true;
        super.error = error;
      },
      body: myReferralPatientRequestModel.toJson(),
    );
  }

  Future replay(String referredDoctorRemarks, MyReferralPatientModel referral) async {
    hasError = false;
    await getDoctorProfile();
    RequestAddReferredDoctorRemarks _requestAddReferredDoctorRemarks = RequestAddReferredDoctorRemarks();
    _requestAddReferredDoctorRemarks.projectID = referral.projectID;
    _requestAddReferredDoctorRemarks.admissionNo = referral.admissionNo.toString();
    _requestAddReferredDoctorRemarks.lineItemNo = referral.lineItemNo;
    _requestAddReferredDoctorRemarks.referredDoctorRemarks = referredDoctorRemarks;
    _requestAddReferredDoctorRemarks.editedBy = doctorProfile!.doctorID;
    _requestAddReferredDoctorRemarks.patientID = referral.patientID;
    _requestAddReferredDoctorRemarks.referringDoctor = referral.referringDoctor;
    await baseAppClient.post(
      ADD_REFERRED_DOCTOR_REMARKS,
      body: _requestAddReferredDoctorRemarks.toJson(),
      onSuccess: (dynamic body, int statusCode) {},
      onFailure: (String error, int statusCode) {
        hasError = true;
        super.error = error;
      },
    );
  }

  Future replayReferred(String referredDoctorRemarks, MyReferralPatientModel referral, int referralStatus) async {
    hasError = false;
    await getDoctorProfile();
    AddReferredRemarksRequestModel _requestAddReferredDoctorRemarks =
        AddReferredRemarksRequestModel(editedBy: doctorProfile!.doctorID, projectID: doctorProfile!.projectID, referredDoctorRemarks: referredDoctorRemarks, referalStatus: referralStatus);
    _requestAddReferredDoctorRemarks.projectID = referral.projectID;

    //TODO Check this in case out patient
    _requestAddReferredDoctorRemarks.admissionNo = int.parse(referral.admissionNo!);
    _requestAddReferredDoctorRemarks.lineItemNo = referral.lineItemNo;
    _requestAddReferredDoctorRemarks.referredDoctorRemarks = referredDoctorRemarks;
    _requestAddReferredDoctorRemarks.editedBy = doctorProfile!.doctorID;
    _requestAddReferredDoctorRemarks.referalStatus = referralStatus;

    // _requestAddReferredDoctorRemarks.patientID = referral.patientID;
    // _requestAddReferredDoctorRemarks.referringDoctor = referral.referringDoctor;
    await baseAppClient.post(
      ADD_REFERRED_REMARKS_NEW,
      body: _requestAddReferredDoctorRemarks.toJson(),
      onSuccess: (dynamic body, int statusCode) {},
      onFailure: (String error, int statusCode) {
        hasError = true;
        super.error = error;
      },
    );
  }
}
