package it.integry.integrywmsnative.core.rest.consumers;

import android.util.Log;

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

import it.integry.integrywmsnative.core.exception.InvalidLicenseException;
import it.integry.integrywmsnative.core.expansion.RunnableArgs;
import it.integry.integrywmsnative.core.rest.CommonRESTException;
import it.integry.integrywmsnative.core.rest.model.EsitoType;
import it.integry.integrywmsnative.core.rest.model.ServiceRESTResponse;
import it.integry.integrywmsnative.core.utility.UtilityString;
import retrofit2.Response;

public abstract class _BaseRESTConsumer {

    public static <T> void analyzeAnswer(Response<ServiceRESTResponse<T>> response, String logTitle, RunnableArgs<T> onComplete, RunnableArgs<Exception> onFailed) {
        if (response.isSuccessful()) {
            if (response.body() != null) {
                if (response.body().getEsito() == EsitoType.OK) {
                    if (!UtilityString.isNullOrEmpty(response.body().getErrorMessage())) {
                        onFailed.run(new Exception(response.body().getErrorMessage()));
                    } else {

                        T dataObj = response.body().getDto() != null ?
                                response.body().getDto() :
                                response.body().getEntity();

                        onComplete.run(dataObj);
                    }
                } else {
                    String errorMessage = UtilityString.isNull(response.body().getErrorMessage(), "Empty message");
                    Log.e(logTitle, errorMessage);
                    onFailed.run(CommonRESTException.tryRecognizeException(errorMessage));
                }
            } else {
                Log.e(logTitle, response.message());
                onFailed.run(new Exception(response.message()));
            }
        } else {
            if (response.code() == 404) {
                Log.e(logTitle, "Errore " + response.code() + ": risorsa non trovata (" + response.raw().request().url().toString() + ")");
                onFailed.run(new Exception("Errore " + response.code() + ": risorsa non trovata (" + logTitle + ")"));
            } else if (response.code() == 550)
                onFailed.run(new InvalidLicenseException());
            else {
                Log.e(logTitle, "Status " + response.code() + ": " + response.message());
                onFailed.run(new Exception("Status " + response.code() + ": " + response.message()));
            }


        }
    }

    public static <T> void analyzeAnswerList(Response<ServiceRESTResponse<T>> response, String logTitle, RunnableArgs<List<T>> onComplete, RunnableArgs<Exception> onFailed) {
        if (response.isSuccessful()) {
            if (response.body() != null) {
                if (response.body().getEsito() == EsitoType.OK) {
                    if (!UtilityString.isNullOrEmpty(response.body().getErrorMessage())) {
                        onFailed.run(new Exception(response.body().getErrorMessage()));
                    } else {

                        List<T> dataObj = response.body().getEntityList();

                        onComplete.run(dataObj);
                    }
                } else {
                    Log.e(logTitle, response.body().getErrorMessage());
                    onFailed.run(CommonRESTException.tryRecognizeException(response.body().getErrorMessage()));
                }
            } else {
                Log.e(logTitle, response.message());
                onFailed.run(new Exception(response.message()));
            }
        } else {
            if (response.code() == 404) {
                Log.e(logTitle, "Errore " + response.code() + ": risorsa non trovata");
                onFailed.run(new Exception("Errore " + response.code() + ": risorsa non trovata (" + logTitle + ")"));
            } else if (response.code() == 550)
                onFailed.run(new InvalidLicenseException());
            else {
                Log.e(logTitle, "Status " + response.code() + ": " + response.message());
                onFailed.run(new Exception("Status " + response.code() + ": " + response.message()));
            }
        }
    }

    public static <T> void analyzeListOfAnswers(Response<List<ServiceRESTResponse<T>>> responseList, String logTitle, RunnableArgs<List<T>> onComplete, RunnableArgs<Exception> onFailed) {
        if (responseList.isSuccessful()) {
            if (responseList.body() != null) {
                List<T> analyzedList = new ArrayList<>();
                for (ServiceRESTResponse<T> response : responseList.body()) {
                    if (response.getEsito() == EsitoType.OK) {
                        if (!UtilityString.isNullOrEmpty(response.getErrorMessage())) {
                            onFailed.run(new Exception(response.getErrorMessage()));
                        } else {
                            T dataObj = response.getDto() != null ?
                                    response.getDto() :
                                    response.getEntity();
                            if (dataObj != null) analyzedList.add(dataObj);

                        }
                    } else {
                        Log.e(logTitle, response.getErrorMessage());
                        onFailed.run(CommonRESTException.tryRecognizeException(response.getErrorMessage()));
                        return;
                    }
                }
                onComplete.run(analyzedList);
            } else {
                Log.e(logTitle, responseList.message());
                onFailed.run(new Exception(responseList.message()));
            }
        } else {
            if (responseList.code() == 404) {
                Log.e(logTitle, "Errore " + responseList.code() + ": risorsa non trovata");
                onFailed.run(new Exception("Errore " + responseList.code() + ": risorsa non trovata (" + logTitle + ")"));
            } else if (responseList.code() == 550)
                onFailed.run(new InvalidLicenseException());
            else {
                Log.e(logTitle, "Status " + responseList.code() + ": " + responseList.message());
                onFailed.run(new Exception("Status " + responseList.code() + ": " + responseList.message()));
            }
        }
    }
}
