package com.cooleshow.base.utils

//import com.bumptech.glide.load.resource.drawable.GlideDrawable
//import com.bumptech.glide.request.animation.GlideAnimation
import android.content.Context
import android.widget.ImageView
import com.bumptech.glide.Glide
import com.bumptech.glide.load.MultiTransformation
import com.bumptech.glide.request.RequestOptions
import com.cooleshow.base.R
import com.cooleshow.base.widgets.transformation.GlideRoundTransform
import jp.wasabeef.glide.transformations.BlurTransformation

/*
    Glide工具类
 */
object GlideUtils {
    fun loadImage(context: Context, url: String?, imageView: ImageView) {
        Glide.with(context).load(url).centerCrop().into(imageView)
    }

    fun loadImage(context: Context, url: String?, imageView: ImageView, defaultDrawable: Int) {
        Glide.with(context).load(url).placeholder(defaultDrawable).error(defaultDrawable)
            .into(imageView)
    }

    fun loadImageFitCenter(context: Context, url: String?, imageView: ImageView) {
        Glide.with(context).load(url).fitCenter().into(imageView)
    }

    fun loadImageWhitVague(
        context: Context,
        url: String?,
        imageView: ImageView,
        defaultDrawable: Int
    ) {
        Glide.with(context).load(url).apply(
            RequestOptions.bitmapTransform(MultiTransformation(BlurTransformation(90)))
        ).placeholder(defaultDrawable).error(defaultDrawable).into(imageView)
    }

    /**
     * 加载顶部圆角图片
     */
    fun loadTopRoundImage(
        context: Context,
        url: String?,
        imageView: ImageView,
        radius: Int,
        defaultDrawable: Int
    ) {
        var options = RequestOptions();
        options.transform(GlideRoundTransform(radius))
            .placeholder(defaultDrawable).error(defaultDrawable)
        Glide.with(context).load(url).apply(options).into(imageView)
    }

    /*
        当fragment或者activity失去焦点或者destroyed的时候，Glide会自动停止加载相关资源，确保资源不会被浪费
     */
    fun loadUrlImage(context: Context, url: String?, imageView: ImageView) {
//        Glide.with(context).load(url).placeholder(R.drawable.icon_back).error(R.drawable.icon_back).centerCrop().into(
//                object : SimpleTarget<GlideDrawable>() {
//                    override fun onResourceReady(resource: GlideDrawable,
//                                                 glideAnimation: GlideAnimation<in GlideDrawable>) {
//                        imageView.setImageDrawable(resource)
//                    }
//                })
    }


    fun loadVideoThumbnail(context: Context, videoUrl: String, view: ImageView) {
        Glide.with(context)
            .setDefaultRequestOptions(
                RequestOptions()
                    .frame(0)
                    .centerCrop()
                    .error(R.drawable.bg_video_placeholder)
                    .placeholder(R.drawable.bg_video_placeholder)
            )
            .load(videoUrl)
            .into(view)
    }
}
