package com.app.simplitend.apputils;

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

import androidx.annotation.NonNull;

import com.app.simplitend.patientprofile.PatientProfileAPIService;
import com.app.simplitend.welcome.welcomepatient.fragments.contacts.mvvm.models.ContactData;
import com.app.simplitend.welcome.welcomepatient.fragments.contacts.mvvm.models.ContactListResponse;
import com.app.simplitend.welcome.welcomepatient.mvvm.WelcomeApiService;
import com.app.simplitend.welcome.welcomepatient.mvvm.models.CallResponse;
import com.app.simplitend.welcome.welcomepatient.mvvm.models.PatientData;
import com.onesignal.OneSignal;

import java.util.ArrayList;
import java.util.List;

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

public class PatientDataCache {

    private static PatientData patientData;

    private static ArrayList<ContactData> contactList;

    public synchronized static void setContactList(ArrayList<ContactData> newContactList){
        contactList = newContactList;
    }

    public synchronized static void getContactList(Context context,
                                                   String token,
                                                   @NonNull PatientDataCache.GetContactList callBack,
                                                   boolean show_progress){
        if (contactList != null){
            callBack.contactList(contactList);
            return;
        }

        updateContactList(context, token, callBack, show_progress);
    }

    private static void updateContactList(Context context, String token, GetContactList 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();
        }

        apiService.getContactList(token)
                .enqueue(new Callback<CallResponse<List<ContactListResponse>>>() {
                    @Override
                    public void onResponse(Call<CallResponse<List<ContactListResponse>>> call, Response<CallResponse<List<ContactListResponse>>> response) {
                        progressDialog.dismiss();
                        if (response.code() == 200 && response.body() != null && response.body().result != null){
                            ArrayList<ContactData> contactList = new ArrayList<>();

                            for (ContactListResponse contactResponse : response.body().result){
                                contactResponse.contact_data.contact_id = contactResponse.id;
                                contactResponse.contact_data.is_doctor = contactResponse.is_doctor;
                                contactList.add(contactResponse.contact_data);
                            }

                            setContactList(contactList);
                            callBack.contactList(contactList);
                        }else{
                            callBack.contactList(null);
                        }
                    }

                    @Override
                    public void onFailure(Call<CallResponse<List<ContactListResponse>>> call, Throwable t) {
                        progressDialog.dismiss();
                        callBack.contactList(null);
                    }
                });
    }

    public synchronized static void setPatientData(PatientData data){
        patientData = data;
    }

    public synchronized static void getPatientData(Context context,
                                        @NonNull PatientDataCache.GetPatientCallBack callBack,
                                        boolean show_progress){
        if (patientData != null){
            callBack.patientData(patientData);
            return;
        }

        updatePatientData(context, callBack, show_progress);
    }

    private static void updatePatientData(Context context, GetPatientCallBack callBack, boolean show_progress) {
        PatientProfileAPIService apiService = RetrofitHelper.getRetrofit().create(PatientProfileAPIService.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.getUsrProfileProgress("Bearer " + AppUtil.getPatientToken(context),
                        sub_id, AppUtil.getTimeZone())
                .enqueue(new Callback<CallResponse<PatientData>>() {
                    @Override
                    public void onResponse(Call<CallResponse<PatientData>> call, Response<CallResponse<PatientData>> response) {
                        progressDialog.dismiss();
                        if (response.body() != null) {
                            if (response.body().status != 200 || response.body().result == null) {
                                if (callBack != null) callBack.patientData(null);
                                return;
                            }

                            if (callBack != null){
                                setPatientData(response.body().result);
                                callBack.patientData(response.body().result);
                            }
                        } else {
                            if (callBack != null) callBack.patientData(null);
                        }
                    }

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

    @FunctionalInterface
    public interface GetPatientCallBack{
        void patientData(PatientData patientData);
    }

    @FunctionalInterface
    public interface GetContactList{
        void contactList(ArrayList<ContactData> contactList);
    }
}
