package com.hh.lib_base.utils

import android.app.Activity
import android.content.Context
import android.widget.ImageView
import androidx.annotation.DrawableRes
import com.bumptech.glide.Glide
import com.bumptech.glide.request.BaseRequestOptions

/**
 * @Describe 加载图片工具类
 * @author Xia WeiHao
 * @date 2021/9/18
 */
class ImageLoaderUtils {

    companion object {
        /**
         * @Describe 加载全屏背景图
         * @author Xia WeiHao
         * @date 2021/9/18
         */
        fun loadFullScreenImage(context: Context?, pic: String?, iv: ImageView?) {
            if (context is Activity && context.isFinishing) {
                return
            }
            Glide.with(context!!).load(pic).override(
                DensityUtil.getDisplayMetricsWidth(context),
                DensityUtil.getDisplayMetricsHeight(context)
            ).dontAnimate().into(iv!!)
        }

        /**
         * @Describe 加载本地资源图片
         * @author Xia WeiHao
         * @date 2021/9/18
         */
        fun loadResNotCenterCrop(context: Context?, @DrawableRes res: Int, iv: ImageView?) {
            if (context is Activity && context.isFinishing) {
                return
            }
            Glide.with(context!!).load(res).dontAnimate().placeholder(res).into(iv!!)
        }

        fun loadRes(context: Context?, @DrawableRes res: Int, iv: ImageView?) {
            if (context is Activity && context.isFinishing) {
                return
            }
            Glide.with(context!!).load(res).dontAnimate().centerCrop().placeholder(res).into(iv!!)
        }

        fun loadRes(
            context: Context?,
            @DrawableRes res: Int,
            mWidth: Int,
            mHeight: Int,
            iv: ImageView?
        ) {
            if (context is Activity && context.isFinishing) {
                return
            }
            Glide.with(context!!).load(res).override(mWidth, mHeight).placeholder(res).into(iv!!)
        }

        /**
         * @Describe 加载网络图片，没有预加载
         * @author Xia WeiHao
         * @date 2021/9/18
         */
        fun loadNetworkPic(context: Context?, pic: Any?, iv: ImageView?) {
            if (context is Activity && context.isFinishing) {
                return
            }
            Glide.with(context!!).load(pic).dontAnimate().centerCrop().into(iv!!)
        }

        fun loadNotCenterCropNetworkPic(context: Context?, pic: String?, iv: ImageView?) {
            if (context is Activity && context.isFinishing) {
                return
            }
            Glide.with(context!!).load(pic).dontAnimate().into(iv!!)
        }

        fun loadImage(imageUrl: Any?, imageView: ImageView){
            loadImage(imageUrl,imageView,null)
        }
        fun loadImage(
            imageUrl: Any?,
            imageView: ImageView,
            requestOptions: BaseRequestOptions<*>?
        ) {
            imageView?.let {
                if (imageView.context is Activity && (imageView.context as Activity).isFinishing) {
                    return
                }
            }

            if (requestOptions == null) {
                loadNetworkPic(imageView.context, imageUrl, imageView)
            } else {
                Glide.with(imageView.context!!).load(imageUrl).dontAnimate().apply(requestOptions)
                    .into(imageView!!)
            }

        }
    }
}