package com.app.simplitend.apputils;

import android.app.ProgressDialog;
import android.content.Context;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.app.simplitend.welcome.welcomecg.WelcomeApiService;
import com.app.simplitend.welcome.welcomecg.mvvm.CareGiverData;
import com.app.simplitend.welcome.welcomepatient.mvvm.models.CallResponse;
import com.onesignal.OneSignal;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public abstract class CaregiverDataCache {

    private static CareGiverData careGiverData;

    public static void setCareGiverData(CareGiverData data){
        careGiverData = data;
    }

    public static void getCaregiverData(Context context,
                                        @NonNull GetCaregiverDataCallBack callBack,
                                        boolean show_progress){
        if (careGiverData != null && careGiverData.patientDetails != null){
            callBack.careGiverData(careGiverData);
            return;
        }

        updateCaregiverData(context, callBack, show_progress);
    }

    private static void updateCaregiverData(Context context,
                                     @Nullable GetCaregiverDataCallBack callBack,
                                            boolean show_progress) {
        WelcomeApiService apiService = RetrofitHelper.getRetrofit().create(WelcomeApiService.class);

        ProgressDialog progressDialog = new ProgressDialog(context);
        progressDialog.setTitle("Please wait...");
        progressDialog.setMessage("while we fetch details for you...");
        progressDialog.setCancelable(false);
        if (show_progress) progressDialog.show();

        String sub_id = OneSignal.getUser().getPushSubscription().getId() + "";

        apiService.getCgUserData("Bearer " + AppUtil.getCgToken(context), sub_id)
                .enqueue(new Callback<CallResponse<CareGiverData>>() {
                    @Override
                    public void onResponse(Call<CallResponse<CareGiverData>> call, Response<CallResponse<CareGiverData>> response) {
                        progressDialog.dismiss();
                        if (response.body() != null) {
                            if (response.body().status != 200 || response.body().result == null) {
                                if (callBack != null) callBack.careGiverData(null);
                                return;
                            }

                            if (callBack != null){
                                setCareGiverData(response.body().result);
                                callBack.careGiverData(response.body().result);
                            }
                        } else {
                            if (callBack != null) callBack.careGiverData(null);
                        }
                    }

                    @Override
                    public void onFailure(Call<CallResponse<CareGiverData>> call, Throwable t) {
                        progressDialog.dismiss();
                        if (callBack != null) callBack.careGiverData(null);
                    }
                });

    }


    // interfaces
    @FunctionalInterface
    public interface GetCaregiverDataCallBack{
        void careGiverData(CareGiverData careGiverData);
    }

}
