package com.app.simplitend.articles;

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 ArticlePresenter {

    private static ArticlePresenter articlePresenter;

    private ArticleApiService apiService;

    private ArticlePresenter() {
        this.apiService = RetrofitHelper.getRetrofit()
                .create(ArticleApiService.class);
    }

    public static synchronized ArticlePresenter getArticlePresenter() {
        if (articlePresenter == null) {
            articlePresenter = new ArticlePresenter();
        }

        return articlePresenter;
    }

    public void getArticles(String token,
                            String search_query,
                            int per_page,
                            int page_no,
                            @NonNull ArticleContracts.GetArticleCallback getArticleCallback) {

        apiService.getArticles(token, search_query, per_page, page_no)
                .enqueue(new Callback<CallResponse<ArrayList<ArticleResult>>>() {
                    @Override
                    public void onResponse(Call<CallResponse<ArrayList<ArticleResult>>> call, Response<CallResponse<ArrayList<ArticleResult>>> response) {
                        if (response.body() != null) {
                            if (response.body().status != 200 || response.body().result == null) {
                                getArticleCallback.onArticleFetchFailed(new Exception(), response.body().message);
                                return;
                            }

                            getArticleCallback.onArticlesFetched(response.body().result);
                        } else {
                            getArticleCallback.onArticleFetchFailed(new Exception(), "Please try again later.");
                        }
                    }

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

    public void getDefaultArticle(String token,
                            @NonNull ArticleContracts.GetArticleCallback getArticleCallback) {

        apiService.getDefaultArticle(token, 1)
                .enqueue(new Callback<CallResponse<ArticleResult>>() {
                    @Override
                    public void onResponse(Call<CallResponse<ArticleResult>> call, Response<CallResponse<ArticleResult>> response) {
                        if (response.body() != null) {
                            if (response.body().status != 200 || response.body().result == null) {
                                getArticleCallback.onArticleFetchFailed(new Exception(), response.body().message);
                                return;
                            }

                            ArrayList<ArticleResult> articleResults = new ArrayList<>();
                            articleResults.add(response.body().result);
                            getArticleCallback.onArticlesFetched(articleResults);
                        } else {
                            getArticleCallback.onArticleFetchFailed(new Exception(), "Please try again later.");
                        }
                    }

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

}
