package com.example.petstoremobile.utils;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.widget.ImageView;
import androidx.annotation.Nullable;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.DataSource;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.engine.GlideException;
import com.bumptech.glide.load.model.GlideUrl;
import com.bumptech.glide.load.model.LazyHeaders;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.target.Target;
import com.example.petstoremobile.R;

/**
 * Utility class for loading images using Glide with authentication tokens.
 */
public class GlideUtils {

    /**
     * interface to check the status of the image load.
     */
    public interface ImageLoadListener {
        void onResourceReady();
        void onLoadFailed();
    }

    /**
     * Loads an image from a URL into an ImageView with token.
     */
    public static void loadImageWithToken(Context context, ImageView imageView, String url, String token, int placeholder) {
        loadImageWithToken(context, imageView, url, token, placeholder, null);
    }

    /**
     * Loads an image from a URL into an ImageView with token and listener.
     */
    public static void loadImageWithToken(Context context, ImageView imageView, String url, String token, int placeholder, ImageLoadListener listener) {
        if (url == null) {
            imageView.setImageResource(placeholder);
            if (listener != null) listener.onLoadFailed();
            return;
        }

        Object loadTarget = url;
        if (token != null && url.startsWith("http")) {
            loadTarget = new GlideUrl(url, new LazyHeaders.Builder()
                    .addHeader("Authorization", "Bearer " + token)
                    .build());
        }

        Glide.with(context)
                .load(loadTarget)
                .diskCacheStrategy(DiskCacheStrategy.NONE)
                .skipMemoryCache(true)
                .placeholder(placeholder)
                .error(placeholder)
                .listener(new RequestListener<Drawable>() {
                    @Override
                    public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
                        if (listener != null) listener.onLoadFailed();
                        return false;
                    }

                    @Override
                    public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
                        if (listener != null) listener.onResourceReady();
                        return false;
                    }
                })
                .into(imageView);
    }

    /**
     * Loads an image from a URL into an ImageView with token and applies circle cropping for image.
     */
    public static void loadImageWithTokenCircle(Context context, ImageView imageView, String url, String token, int placeholder) {
        loadImageWithTokenCircle(context, imageView, url, token, placeholder, null);
    }

    /**
     * Loads an image from a URL into an ImageView with token, circle cropping, and listener.
     */
    public static void loadImageWithTokenCircle(Context context, ImageView imageView, String url, String token, int placeholder, ImageLoadListener listener) {
        if (url == null) {
            imageView.setImageResource(placeholder);
            if (listener != null) listener.onLoadFailed();
            return;
        }

        Object loadTarget = url;
        if (token != null && url.startsWith("http")) {
            loadTarget = new GlideUrl(url, new LazyHeaders.Builder()
                    .addHeader("Authorization", "Bearer " + token)
                    .build());
        }

        Glide.with(context)
                .load(loadTarget)
                .circleCrop()
                .diskCacheStrategy(DiskCacheStrategy.NONE)
                .skipMemoryCache(true)
                .placeholder(placeholder)
                .error(placeholder)
                .listener(new RequestListener<Drawable>() {
                    @Override
                    public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
                        if (listener != null) listener.onLoadFailed();
                        return false;
                    }

                    @Override
                    public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
                        if (listener != null) listener.onResourceReady();
                        return false;
                    }
                })
                .into(imageView);
    }
}
