package com.example.petstoremobile.utils;

import android.content.Context;
import android.util.Log;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.lifecycle.MutableLiveData;

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

/**
 * Utility class for common Retrofit operations and standardized callbacks.
 */
public class RetrofitUtils {

    /**
     * Interface for handling successful API responses.
     * @param <T> The type of the response body.
     */
    public interface SuccessCallback<T> {
        void onSuccess(T result);
    }

    /**
     * Enqueues a Retrofit call and updates the provided MutableLiveData with Resource states.
     */
    public static <T> void enqueue(@NonNull Call<T> call, @NonNull MutableLiveData<Resource<T>> data, String tag) {
        data.setValue(Resource.loading(null));
        call.enqueue(new Callback<T>() {
            @Override
            public void onResponse(@NonNull Call<T> call, @NonNull Response<T> response) {
                if (response.isSuccessful()) {
                    data.setValue(Resource.success(response.body()));
                } else {
                    String errorMsg = ErrorUtils.getErrorMessage(response, "API Error: " + response.code());
                    Log.e(tag, errorMsg);
                    data.setValue(Resource.error(errorMsg, null));
                }
            }

            @Override
            public void onFailure(@NonNull Call<T> call, @NonNull Throwable t) {
                String errorMsg = ErrorUtils.getFailureMessage(t);
                Log.e(tag, "Network Error: " + t.getMessage(), t);
                data.setValue(Resource.error(errorMsg, null));
            }
        });
    }

    /**
     * Creates a callback for Retrofit calls that handles errors and logging.
     * @deprecated Use {@link #enqueue(Call, MutableLiveData, String)} for LiveData-based architecture.
     */
    @Deprecated
    public static <T> Callback<T> createCallback(Context context, String tag, String successMsg, SuccessCallback<T> successCallback) {
        return new Callback<T>() {
            @Override
            public void onResponse(@NonNull Call<T> call, @NonNull Response<T> response) {
                if (response.isSuccessful()) {
                    if (successMsg != null) {
                        Toast.makeText(context, successMsg, Toast.LENGTH_SHORT).show();
                    }
                    if (successCallback != null) {
                        successCallback.onSuccess(response.body());
                    }
                } else {
                    ErrorUtils.showErrorMessage(context, response, "Operation failed");
                    Log.e(tag, "API Error: " + response.code());
                }
            }

            @Override
            public void onFailure(@NonNull Call<T> call, @NonNull Throwable t) {
                String errorMsg = ErrorUtils.getFailureMessage(t);
                Log.e(tag, "Network Error: " + t.getMessage(), t);
                Toast.makeText(context, errorMsg, Toast.LENGTH_SHORT).show();
            }
        };
    }

    /**
     * Creates a callback that doesn't show toasts
     */
    @Deprecated
    public static <T> Callback<T> createSilentCallback(String tag, SuccessCallback<T> successCallback) {
        return new Callback<T>() {
            @Override
            public void onResponse(@NonNull Call<T> call, @NonNull Response<T> response) {
                if (response.isSuccessful() && successCallback != null) {
                    successCallback.onSuccess(response.body());
                } else {
                    Log.e(tag, "API Error: " + response.code());
                }
            }

            @Override
            public void onFailure(@NonNull Call<T> call, @NonNull Throwable t) {
                Log.e(tag, "Network Error: " + t.getMessage(), t);
            }
        };
    }
}
