package com.app.simplitend.patientprofile.medicalinfo.mvvm;

import android.util.Log;

import androidx.annotation.NonNull;

import com.app.simplitend.apputils.RetrofitHelper;
import com.app.simplitend.patientprofile.PatientProfileAPIService;
import com.app.simplitend.patientprofile.ProfileContracts;
import com.app.simplitend.welcome.welcomepatient.mvvm.models.CallResponse;

import java.util.Map;

import okhttp3.RequestBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class MedicalIntoRepository {

    private static final String TAG = "MedicalIntoRepository";

    private static MedicalIntoRepository medicalIntoRepository;

    private PatientProfileAPIService apiService;

    private MedicalIntoRepository(){
        apiService = RetrofitHelper.getRetrofit().create(PatientProfileAPIService.class);
    }

    public static synchronized MedicalIntoRepository getMedicalIntoRepository(){
        if (medicalIntoRepository == null){
            medicalIntoRepository = new MedicalIntoRepository();
        }

        return medicalIntoRepository;
    }

    public void addNUpdateMedicalInfo(Map<String, RequestBody> body,
                                      int patient_id,
                                      String token,
                                      @NonNull ProfileContracts.AddMedicalIntoCallback medicalIntoCallback){

        apiService.addNUpdateMedicalInfo(body, patient_id, token)
                .enqueue(new Callback<CallResponse<MedicationInfo>>() {
                    @Override
                    public void onResponse(Call<CallResponse<MedicationInfo>> call, Response<CallResponse<MedicationInfo>> response) {
                        if (response.body() != null){
                            if (response.body().status != 200 || response.body().result == null){
                                medicalIntoCallback.onMedicalInfoAddFailed(new Exception(), response.body().message);
                                return;
                            }

                            medicalIntoCallback.onMedicalInfoAdded(response.body().result);
                        }else{
                            medicalIntoCallback.onMedicalInfoAddFailed(new Exception(), "Please try again later.");
                        }
                    }

                    @Override
                    public void onFailure(Call<CallResponse<MedicationInfo>> call, Throwable t) {
                        medicalIntoCallback.onMedicalInfoAddFailed(new Exception(), "Please try again later.");
                        Log.e(TAG, "onFailure: ", t);
                    }
                });

    }

    public void getMedicalInfo(int patient_id,
                               String token,
                               @NonNull ProfileContracts.GetMedicationInfoCallback medicationInfoCallback){

        apiService.getMedicalInfo(patient_id, token)
                .enqueue(new Callback<CallResponse<MedicationInfo>>() {
                    @Override
                    public void onResponse(Call<CallResponse<MedicationInfo>> call, Response<CallResponse<MedicationInfo>> response) {
                        if (response.body() != null){
                            if (response.body().status != 200){
                                medicationInfoCallback.onMedicationInfoFetchedFailed(new Exception(), response.body().message);
                                return;
                            }

                            medicationInfoCallback.onMedicalInfoFetched(response.body().result);
                        }else{
                            medicationInfoCallback.onMedicationInfoFetchedFailed(new Exception(), "Please try again later.");
                        }
                    }

                    @Override
                    public void onFailure(Call<CallResponse<MedicationInfo>> call, Throwable t) {
                        medicationInfoCallback.onMedicationInfoFetchedFailed(new Exception(), "Please try again later.");
                    }
                });

    }

}
