package com.app.simplitend.faqs;

import androidx.annotation.NonNull;

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

import java.util.ArrayList;

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

public class FAQsPresenter {

    private static FAQsPresenter faqsPresenter;

    private FAQApiService apiService;

    private FAQsPresenter(){
        this.apiService = RetrofitHelper.getRetrofit()
                .create(FAQApiService.class);
    }

    public static synchronized FAQsPresenter getFaqsPresenter() {
        if (faqsPresenter == null) {
            faqsPresenter = new FAQsPresenter();
        }

        return faqsPresenter;
    }

    public void getFAQs(@NonNull FAQContracts.GETFAQsCallback faqCallbacks){
        apiService.getFAQs()
                .enqueue(new Callback<CallResponse<ArrayList<FAQResult>>>() {
                    @Override
                    public void onResponse(Call<CallResponse<ArrayList<FAQResult>>> call, Response<CallResponse<ArrayList<FAQResult>>> response) {
                        if (response.body() != null){
                            if (response.body().status != 200 || response.body().result == null){
                                faqCallbacks.onFAQsFetchedFailed(new Exception(), response.body().message);
                                return;
                            }

                            faqCallbacks.onFAQsFetched(response.body().result);
                        }else{
                            faqCallbacks.onFAQsFetchedFailed(new Exception(), "Please try again later.");
                        }
                    }

                    @Override
                    public void onFailure(Call<CallResponse<ArrayList<FAQResult>>> call, Throwable t) {
                        faqCallbacks.onFAQsFetchedFailed(new Exception(), "Please try again later.");
                    }
                });
    }

}
