package com.bonait.bnframework.common.glide;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Environment;
import android.view.View;
import android.widget.ImageView;

import androidx.annotation.Nullable;

import com.bonait.bnframework.common.glide.transform.FitXYRoundRadiusTransform;
import com.bonait.bnframework.modules.home.fragment.from.ImageSourceActivity;
import com.bumptech.glide.load.DataSource;
import com.bumptech.glide.load.engine.GlideException;
import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.Target;

/**
 * @author: liup
 * @description:
 * @date: 2024/5/8 16:45.
 */
public class GlideUtil {

    private static final int CROSS_FADE_DURATION = 100;

    private GlideUtil() {
        throw new AssertionError();
    }

//    public static String exchangeUrl(String path){
//        String result=path;
//        if(!TextUtils.isEmpty(PATH_PRE)&&!TextUtils.isEmpty(path)) {
//            if(!path.contains("http:")&&!path.contains("https:")){
//                if(path.contains(".png")||path.contains(".jpg")||path.contains(".jpeg")||path.contains(".gif")){
//                    if(TEST) {
//                        result = PATH_PRE + path.replace("images", "modules");
//                    }else{
//                        result = PATH_PRE + path;
//                    }
//                    if(first){
//                        LogUtils.d("GlideUtil exchangeUrl result:"+result);
//                        first=false;
//                    }
//                }
//            }
//        }
////        LogUtils.i("GlideUtil exchangeUrl"+result);
//        return result;
//    }

    public static void showImage(Context context, String path, ImageView imageView,int placeId) {
        if (context == null||path==null) {
            return;
        }
        if(!path.startsWith("http")){
            path = Environment.getExternalStorageDirectory().getAbsolutePath()+"/hblchayingdb/WebImage/" + path;
        }
        GlideApp.with(context)
                .load(path)
                .addListener(new RequestListener<Drawable>() {
                    @Override
                    public boolean onLoadFailed(@Nullable GlideException e, Object model,
                                                Target<Drawable> target, boolean isFirstResource) {
                        return false;
                    }

                    @Override
                    public boolean onResourceReady(Drawable resource, Object model,
                                                   Target<Drawable> target, DataSource dataSource,
                                                   boolean isFirstResource) {
                        if (imageView == null) {
                            return false;
                        }
                        imageView.setBackground(null);
                        return false;
                    }
                })
                .placeholder(placeId)
                .transition(DrawableTransitionOptions.withCrossFade(CROSS_FADE_DURATION))
                .into(imageView);
    }

    public static void showImageWithRadius(Context context, String path, ImageView imageView,int placeId,int radius) {
        if (context == null||path==null) {
            return;
        }
        setType(imageView);
        GlideApp.with(context)
                .load(path)
                .addListener(new RequestListener<Drawable>() {
                    @Override
                    public boolean onLoadFailed(@Nullable GlideException e, Object model,
                                                Target<Drawable> target, boolean isFirstResource) {
                        return false;
                    }

                    @Override
                    public boolean onResourceReady(Drawable resource, Object model,
                                                   Target<Drawable> target, DataSource dataSource,
                                                   boolean isFirstResource) {
                        if (imageView == null) {
                            return false;
                        }
                        imageView.setBackground(null);
                        return false;
                    }
                })
                .transition(DrawableTransitionOptions.withCrossFade(CROSS_FADE_DURATION))
//                .apply(RequestOptions.bitmapTransform(new RoundedCorners(RADIUS)))
                .apply(new RequestOptions().transform(new FitXYRoundRadiusTransform(radius)))
                .into(imageView);
    }

    private static void setType(ImageView imageView){//硬件加速
        imageView.setLayerType(View.LAYER_TYPE_HARDWARE,null);
    }
}
