package com.duben.library.utils

import android.annotation.SuppressLint
import android.content.Context
import android.graphics.Bitmap
import android.graphics.drawable.Drawable
import android.widget.ImageView
import com.bumptech.glide.Glide
import com.bumptech.glide.Priority
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.bumptech.glide.load.resource.bitmap.CenterCrop
import com.bumptech.glide.load.resource.bitmap.CenterInside
import com.bumptech.glide.load.resource.bitmap.FitCenter
import com.bumptech.glide.load.resource.bitmap.RoundedCorners
import com.bumptech.glide.load.resource.gif.GifDrawable
import com.bumptech.glide.request.RequestOptions
import com.bumptech.glide.request.target.SimpleTarget
import com.bumptech.glide.request.target.ViewTarget
import com.bumptech.glide.request.transition.Transition
import com.bumptech.glide.signature.ObjectKey
import com.duben.microtribe.utils.BubbleUtils
import jp.wasabeef.glide.transformations.BlurTransformation

/**
 * 描述：GlideUtils
 * 作者：孟崔广
 * 时间：2018/1/10 17:51
 * 邮箱：mengcga@163.com
 */
@SuppressLint("CheckResult")
object GlideUtils {
    /**
     * Glide特点
     * 使用简单
     * 可配置度高，自适应程度高
     * 支持常见图片格式 Jpg png gif webp
     * 支持多种数据源  网络、本地、资源、Assets 等
     * 高效缓存策略    支持Memory和Disk图片缓存 默认Bitmap格式采用RGB_565内存使用至少减少一半
     * 生命周期集成   根据Activity/Fragment生命周期自动管理请求
     * 高效处理Bitmap  使用Bitmap Pool使Bitmap复用，主动调用recycle回收需要回收的Bitmap，减小系统回收压力
     * 这里默认支持Context，Glide支持Context,Activity,Fragment，FragmentActivity
     */
    //默认加载
    fun loadImageView(mContext: Context?, path: Any?, mImageView: ImageView?) {
        Glide.with(mContext!!).load(path).into(mImageView!!)
    }

    fun loadImageViewFake(mContext: Context?, path: Any?, mImageView: ImageView?) {
        Glide.with(mContext!!)
            .load(path)
            .apply(
                RequestOptions.bitmapTransform(
                    RoundedCorners(BubbleUtils.px2dp(10f))
                )
            ) //圆角半径
            .skipMemoryCache(true)
            .dontAnimate()
            .into(object : SimpleTarget<Drawable?>() {
                override fun onResourceReady(
                    resource: Drawable,
                    transition: Transition<in Drawable?>?
                ) {
                    mImageView?.setImageDrawable(resource)
                }
            })
    }

    //加载Gif*****
    fun loadRoundImageViewGif(mContext: Context, drawable: Int, imageView: ImageView) {
        val options = RequestOptions()
            .fitCenter()
            .diskCacheStrategy(DiskCacheStrategy.DATA)
        Glide.with(mContext)
            .load(drawable)
            .apply(options)
            .into(imageView)
    }

    //默认加载
    fun loadImageViewNoAnim(mContext: Context?, path: Any?, mImageView: ImageView?) {
        Glide.with(mContext!!).load(path).dontAnimate().skipMemoryCache(true).into(mImageView!!)
    }

    //默认加载
    fun loadImageViewNoAnim2(mContext: Context?, path: Any?, mImageView: ImageView?) {
        Glide.with(mContext!!).load(path).apply(
            RequestOptions().transform(
                CenterCrop(),
                RoundedCorners(BubbleUtils.dp2px(10))
            )
        ).dontAnimate().into(mImageView!!)
    }

    fun loadImageViewGifForFitCenter(mContext: Context, drawable: String, imageView: ImageView) {
        val options = RequestOptions()
            .transform(
                FitCenter(),
                RoundedCorners(BubbleUtils.dp2px(5))
            )
            .diskCacheStrategy(DiskCacheStrategy.DATA)
        Glide.with(mContext)
            .load(drawable)
            .apply(options)
            .into(imageView)
    }

    fun loadImageViewGifForCenterCrop(mContext: Context, drawable: String, imageView: ImageView) {
        val options = RequestOptions()
            .transform(
                CenterCrop(),
                RoundedCorners(BubbleUtils.dp2px(5))
            )
            .diskCacheStrategy(DiskCacheStrategy.DATA)
        Glide.with(mContext)
            .load(drawable)
            .apply(options)
            .into(imageView)
    }

    fun loadImageViewGifForCenterCrop(
        mContext: Context,
        drawable: String,
        imageView: ImageView,
        dp: Int
    ) {
        val options = RequestOptions()
            .transform(
                CenterCrop(),
                RoundedCorners(BubbleUtils.dp2px(dp))
            )
            .diskCacheStrategy(DiskCacheStrategy.DATA)
        Glide.with(mContext)
            .load(drawable)
            .apply(options)
            .into(imageView)
    }

    fun loadImageViewGifForCenterInside(mContext: Context, drawable: String, imageView: ImageView) {
        val options = RequestOptions()
            .transform(
                CenterInside(),
                RoundedCorners(BubbleUtils.dp2px(5))
            )
            .diskCacheStrategy(DiskCacheStrategy.DATA)
        Glide.with(mContext)
            .load(drawable)
            .apply(options)
            .into(imageView)
    }

    //设置加载中图片
    fun loadImageViewLoding(
        mContext: Context?,
        path: String?,
        mImageView: ImageView?,
        loadingImage: Int
    ) {
        val requestOptions = RequestOptions()
        requestOptions.placeholder(loadingImage)
        Glide.with(mContext!!).load(path).apply(requestOptions).into(mImageView!!)
    }

    //设置加载中以及加载失败图片
    fun loadImageViewLoding(
        mContext: Context?,
        path: Any?,
        mImageView: ImageView?,
        loadingImage: Int,
        errorImageView: Int
    ) {
        val requestOptions = RequestOptions()
        requestOptions.placeholder(loadingImage)
        requestOptions.error(errorImageView)
        Glide.with(mContext!!).load(path).apply(requestOptions).into(mImageView!!)
    }

    //设置加载中以及加载失败圆形图片
    fun loadCircleImageView(
        mContext: Context?,
        path: Any?,
        mImageView: ImageView?,
        loadingImage: Int,
        errorImageView: Int
    ) {
        val requestOptions = RequestOptions()
        requestOptions.placeholder(loadingImage)
        requestOptions.error(errorImageView)
        requestOptions.circleCrop()
        Glide.with(mContext!!).load(path).apply(requestOptions).into(mImageView!!)
    }

    //加载Gif
    fun loadImageViewGif(mContext: Context?, drawable: Int, imageView: ImageView?) {
        Glide.with(mContext!!).asGif().load(drawable).diskCacheStrategy(DiskCacheStrategy.ALL)
            .into(imageView!!)
    }

    //加载Gif
    fun loadImageViewGifUrl(mContext: Context?, view: ImageView?, url: Any?) {
        val options = RequestOptions()
            .diskCacheStrategy(DiskCacheStrategy.RESOURCE)
            .timeout(60000)
            .signature(ObjectKey(System.currentTimeMillis()))
            .priority(Priority.HIGH)
        Glide.with(mContext!!).load(url).apply(options).into(view!!)
    }

    //加载Gif
    fun loadImageViewBitMap(mContext: Context?, drawable: Int, imageView: ImageView?) {
        Glide.with(mContext!!).asBitmap().load(drawable).into(imageView!!)
    }

    //加载Gif
    fun loadImageViewGif2(mContext: Context, drawable: Int, imageView: ImageView) {
        Glide.with(mContext)
            .asGif()
            .load(drawable)
            .apply(RequestOptions().skipMemoryCache(true))
            .into(imageView)
    }


    //加载毛玻璃图片
    fun loadBlurImageView(mContext: Context, url: String, imageView: ImageView) {
        Glide.with(mContext).load(url)
            .apply(RequestOptions.bitmapTransform(BlurTransformation(25, 4)))
            .into(imageView)
    }
}