package cn.dankal.basiclib.util;

import android.content.Context;
import androidx.annotation.DrawableRes;
import android.text.TextUtils;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;

import cn.dankal.basiclib.DKUserManager;
import cn.dankal.basiclib.GlideApp;
import cn.dankal.basiclib.R;

public class GlideUtils {


    public static final int DEFAULT_AVATAR_PLACEHOLDER_RESID = 0x1;
    public static final int DEFAULT_AVATAR_ERRORHOLDER_RESID = 0x2;



    public static String addHeaderUrl(String url){
        if (TextUtils.isEmpty(url)) {
            url = "";
        }
        if (!url.startsWith("http")) {
            if (url.startsWith("/")) {
                url = DKUserManager.getQiniuHeader().getBucket_domain() + url;
            } else {
                url = DKUserManager.getQiniuHeader().getBucket_domain() + "/" + url;
            }

        }

        return url;
    }

    public static void loadImage(Context context, String url, ImageView imageView) {

        GlideApp.with(context).load(url).into(imageView);
    }

    public static void loadImageWithSize(Context context, String url, ImageView imageView, int widthdp, int heightdp) {

        GlideApp.with(context).load(url).override(SizeUtils.dp2px(context, widthdp), SizeUtils.dp2px(context, heightdp)).into(imageView);
    }

    public static void loadRoundImage(Context context, String url, int dp, ImageView imageView) {

        //设置图片圆角角度
        RoundedCorners roundedCorners = new RoundedCorners(SizeUtils.dp2px(context, dp));
        Logger.e(SizeUtils.dp2px(context, dp) + "");
//通过RequestOptions扩展功能
        RequestOptions options = new RequestOptions().transforms(new CenterCrop(), roundedCorners);

        Glide.with(context).load(url).apply(options).into(imageView);
    }


    public static void loadRoundImage(Context context, String url, int dp, int widthdp, int heightdp, ImageView imageView) {

        //设置图片圆角角度
        RoundedCorners roundedCorners = new RoundedCorners(SizeUtils.dp2px(context, dp));
//通过RequestOptions扩展功能
        RequestOptions options = new RequestOptions().transforms(new CenterCrop(), roundedCorners).override(SizeUtils.dp2px(context, widthdp), SizeUtils.dp2px(context, heightdp));

        Glide.with(context).load(url).apply(options).into(imageView);
    }


    public static void loadCircleImage(Context context, String url, ImageView imageView) {

//通过RequestOptions扩展功能
        RequestOptions options = RequestOptions.circleCropTransform().placeholder(R.drawable.ic_avatar_placeholder);

        Glide.with(context).load(url).apply(options).into(imageView);
    }

    public static void loadCircleImage(Context context, String url, int dp, ImageView imageView) {
        if (TextUtils.isEmpty(url)) {
            url = "";
        }
        if (!url.startsWith("http")) {
            if (url.startsWith("/")) {
                url = DKUserManager.getQiniuHeader().getBucket_domain() + url;
            } else {
                url = DKUserManager.getQiniuHeader().getBucket_domain() + "/" + url;
            }

        }

//通过RequestOptions扩展功能
        RequestOptions options = RequestOptions.circleCropTransform().override(SizeUtils.dp2px(context, dp));

        Glide.with(context).load(url).apply(options).into(imageView);
    }


    /**
     * 从本地获取图片
     *
     * @param context
     * @param url
     * @param imageView
     */
    public static void loadNormalImage(Context context, String url, ImageView imageView) {

        Glide.with(context).load(url).into(imageView);
    }

    /**
     * 从本地获取图片
     *
     * @param context
     * @param url
     * @param imageView
     */
    public static void loadCircleImageFromDisk(Context context, String url, ImageView imageView) {
        RequestOptions options = RequestOptions.circleCropTransform();

        Glide.with(context).load(url).apply(options).into(imageView);
    }


    /**
     * 从本地获取图片
     *
     * @param context
     * @param url
     * @param imageView
     */
    public static void loadCircleImageFromRes(Context context, @DrawableRes int url, ImageView imageView) {
        RequestOptions options = RequestOptions.circleCropTransform();

        Glide.with(context).load(url).apply(options).into(imageView);
    }


    /**
     * 从本地获取图片
     *
     * @param context
     * @param url
     * @param imageView
     */
    public static void loadImageFromRes(Context context, @DrawableRes int url, ImageView imageView) {

        Glide.with(context).load(url).into(imageView);
    }




    /**
     * 从本地获取图片 圆形
     *
     * @param context
     * @param url
     * @param dp
     * @param imageView
     */
    public static void loadCircleImageWithSizeFromDisk(Context context, String url, int dp, ImageView imageView) {
        RequestOptions options = RequestOptions.circleCropTransform().override(SizeUtils.dp2px(context, dp));

        Glide.with(context).load(url).apply(options).into(imageView);
    }




    /**
     * 加载圆角矩形头像
     *
     * @param context
     * @param url
     * @param radiusDp
     * @param sizeDp
     * @param imageView
     */
    public static void loadRoundHeadImageWithSize(Context context, String url, int radiusDp, int sizeDp, ImageView imageView) {

        RoundedCorners roundedCorners = new RoundedCorners(SizeUtils.dp2px(context, radiusDp));

        GlideApp.with(context).load(url).transform(roundedCorners).diskCacheStrategy(DiskCacheStrategy.ALL).override(SizeUtils.dp2px(context, sizeDp)).into(imageView);
    }



}
