package com.common.lib_base.utils;

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

import androidx.appcompat.app.AppCompatActivity;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.request.RequestOptions;
import com.common.lib_base.R;
import com.common.lib_base.utils.ui.UIUtils;

import java.io.File;

/**
 * Date: 2022/6/1 0001 15:23
 * Description:
 */
public class GlideUtils {


    public static void loadImageView(Context context, String path, ImageView mImageView) {
        Glide.with(context).load(path).into(mImageView);
    }

    public static void loadImageView(AppCompatActivity context, String path, ImageView mImageView) {
        if(context.isDestroyed()||context.isDestroyed()){
            return;
        }
        Glide.with(context).load(path).into(mImageView);
    }


    public static void loadImageView(Context context, Integer resourceId, ImageView mImageView) {
        Glide.with(context).load(resourceId).into(mImageView);
    }

    public static void loadImageView(AppCompatActivity context, Integer resourceId, ImageView mImageView) {
        Glide.with(context).load(resourceId).into(mImageView);
    }


    public static void loadImageViewOverride(AppCompatActivity context, String path, ImageView mImageView, int width, int height) {

        RequestOptions options = new RequestOptions()
                .centerCrop()
                .override(UIUtils.dp2px(width), UIUtils.dp2px(height))
                .placeholder(R.color.gray);


        Glide.with(context).load(path).apply(options).into(mImageView);
    }

    public static void loadImageViewRound(AppCompatActivity context, String path, ImageView mImageView,int roundDp) {

        RequestOptions options = new RequestOptions()
                .centerCrop()
//                .placeholder(R.mipmap.ic_launcher_round) //预加载图片
//                .error(R.drawable.ic_launcher_foreground) //加载失败图片
//                .priority(Priority.HIGH) //优先级
//                .diskCacheStrategy(DiskCacheStrategy.NONE) //缓存
                .transform(new GlideRoundTransform(roundDp)); //圆角

        Glide.with(context).load(path).apply(options).into(mImageView);
    }


    public static void loadImageViewRound(Context context, String path, ImageView mImageView,int roundDp) {

        RequestOptions options = new RequestOptions()
                .centerCrop()
//                .placeholder(R.mipmap.ic_launcher_round) //预加载图片
//                .error(R.drawable.ic_launcher_foreground) //加载失败图片
//                .priority(Priority.HIGH) //优先级
//                .diskCacheStrategy(DiskCacheStrategy.NONE) //缓存
                .transform(new GlideRoundTransform(roundDp)); //圆角

        Glide.with(context).load(path).apply(options).into(mImageView);
    }


    public static void loadImageViewRound(Context context, int path, ImageView mImageView,int roundDp) {

        RequestOptions options = new RequestOptions()
                .centerCrop()
//                .placeholder(R.mipmap.ic_launcher_round) //预加载图片
//                .error(R.drawable.ic_launcher_foreground) //加载失败图片
//                .priority(Priority.HIGH) //优先级
//                .diskCacheStrategy(DiskCacheStrategy.NONE) //缓存
                .transform(new GlideRoundTransform(roundDp)); //圆角

        Glide.with(context).load(path).apply(options).into(mImageView);
    }


    /**
     * 左上右上圆角
     * @param context
     * @param path
     * @param mImageView
     * @param roundDp
     */
    public static void loadImageViewTopRound(Context context, String path, ImageView mImageView,int roundDp) {

        GlideFourRoundTransform transformation = new GlideFourRoundTransform(context, UIUtils.dip2px(context, roundDp));
        //只是绘制左上角和右上角圆角
        transformation.setExceptCorner(false, false, true, true);

        RequestOptions options = new RequestOptions()
                .centerCrop()
//                .placeholder(R.mipmap.ic_launcher_round) //预加载图片
//                .error(R.drawable.ic_launcher_foreground) //加载失败图片
//                .priority(Priority.HIGH) //优先级
//                .diskCacheStrategy(DiskCacheStrategy.NONE) //缓存
                .transform(transformation); //圆角

        Glide.with(context).load(path).apply(options).into(mImageView);
    }



    public static void loadImageViewTopRound(Context context, int path, ImageView mImageView,int roundDp) {

        GlideFourRoundTransform transformation = new GlideFourRoundTransform(context, UIUtils.dip2px(context, roundDp));
        //只是绘制左上角和右上角圆角
        transformation.setExceptCorner(false, false, true, true);

        RequestOptions options = new RequestOptions()
                .centerCrop()
//                .placeholder(R.mipmap.ic_launcher_round) //预加载图片
//                .error(R.drawable.ic_launcher_foreground) //加载失败图片
//                .priority(Priority.HIGH) //优先级
//                .diskCacheStrategy(DiskCacheStrategy.NONE) //缓存
                .transform(transformation); //圆角

        Glide.with(context).load(path).apply(options).into(mImageView);
    }



    /**
     * 左上和左下为圆角
     * @param context
     * @param path
     * @param mImageView
     * @param roundDp
     */
    public static void loadImageViewLeftRound(Context context, String path, ImageView mImageView,int roundDp) {

        GlideFourRoundTransform transformation = new GlideFourRoundTransform(context, UIUtils.dip2px(context, roundDp));
        //只是绘制左上角和右上角圆角
        transformation.setExceptCorner(false, true, false, true);

        RequestOptions options = new RequestOptions()
                .centerCrop()
//                .placeholder(R.mipmap.ic_launcher_round) //预加载图片
//                .error(R.drawable.ic_launcher_foreground) //加载失败图片
//                .priority(Priority.HIGH) //优先级
//                .diskCacheStrategy(DiskCacheStrategy.NONE) //缓存
                .transform(transformation); //圆角

        Glide.with(context).load(path).apply(options).into(mImageView);
    }



    public static void loadImageViewLeftRound(Context context, int path, ImageView mImageView,int roundDp) {

        GlideFourRoundTransform transformation = new GlideFourRoundTransform(context, UIUtils.dip2px(context, roundDp));
        //只是绘制左上角和右上角圆角
        transformation.setExceptCorner(false, true, false, true);

        RequestOptions options = new RequestOptions()
                .centerCrop()
//                .placeholder(R.mipmap.ic_launcher_round) //预加载图片
//                .error(R.drawable.ic_launcher_foreground) //加载失败图片
//                .priority(Priority.HIGH) //优先级
//                .diskCacheStrategy(DiskCacheStrategy.NONE) //缓存
                .transform(transformation); //圆角

        Glide.with(context).load(path).apply(options).into(mImageView);
    }


    public static void loadRoundImageView(Context context, String path, ImageView mImageView, int radius) {

        Glide.with(context).load(path)
//                .placeholder(R.drawable.placeholder_round_grey_5dp)
//                .bitmapTransform(new CenterCrop(context), new GlideRoundTransform(context, radius))
                .into(mImageView);
    }


    public static void loadRoundImageView(AppCompatActivity context, String path, ImageView mImageView) {

        Glide.with(context).load(path)
//                .placeholder(R.drawable.placeholder_round_grey_5dp)
//                .bitmapTransform(new CenterCrop(context), new GlideRoundTransform(context, 5))
                .into(mImageView);
    }


}
