import 'package:doctor_app_flutter/config/config.dart';
import 'package:doctor_app_flutter/config/shared_pref_kay.dart';
import 'package:doctor_app_flutter/core/model/doctor/doctor_profile_model.dart';
import 'package:doctor_app_flutter/core/model/referral/ReferralRequest.dart';
import 'package:doctor_app_flutter/core/service/base/base_service.dart';
import 'package:doctor_app_flutter/core/viewModel/project_view_model.dart';
import 'package:doctor_app_flutter/utils/date-utils.dart';

class ReferralService extends BaseService {
  Future referralPatient(
      {int? admissionNo,
      String? roomID,
      int? referralClinic,
      int? referralDoctor,
      int? patientID,
      int? patientTypeID,
      int? priority,
      int? frequency,
      String? referringDoctorRemarks,
      String? extension,
      String? diagnosisCode,
      String? diagnosisDescription}) async {
    // await getDoctorProfile();
    print("the  referralClinic is $referralClinic");
    ReferralRequest referralRequest = ReferralRequest();
    Map<String, dynamic>? profile = await sharedPref.getObj(DOCTOR_PROFILE);
    DoctorProfileModel doctorProfile = DoctorProfileModel.fromJson(profile!);
    referralRequest.admissionNo = admissionNo;
    referralRequest.roomID = roomID;
    referralRequest.referralClinic = referralClinic.toString();
    referralRequest.referralDoctor = referralDoctor.toString();
    referralRequest.patientID = patientID;
    referralRequest.patientTypeID = patientTypeID;
    referralRequest.priority = priority.toString();
    referralRequest.frequency = frequency.toString();
    referralRequest.referringDoctorRemarks = referringDoctorRemarks;
    referralRequest.referringClinic = doctorProfile!.clinicID;
    referralRequest.referringDoctor = doctorProfile!.doctorID;
    referralRequest.extension = extension;
    referralRequest.editedBy = doctorProfile!.doctorID;
    referralRequest.createdBy = doctorProfile!.doctorID;
    referralRequest.patientOutSA = false;
    referralRequest.diseaseCode = diagnosisCode;
    referralRequest.diseaseDescription = diagnosisDescription;
    referralRequest.toDate = AppDateUtils.convertDateToServerFormat(DateTime.now());

    Map<String, dynamic> json = {};
    if(ProjectViewModel.getVidaPlusStatus()) {
       json = referralRequest.toJsonForVidaPlus();
      json["Specialty"] = doctorProfile?.specialty?.first;
    }else{
      json = referralRequest.toJsonForVidaThree();
    }

    await baseAppClient.post(
      REFER_TO_DOCTOR,
      onSuccess: (dynamic response, int statusCode) {
        hasError = false;

      },
      onFailure: (String error, int statusCode) {
        hasError = true;
        super.error = error;
      },
      body: json,
    );
  }
}
