package com.example.base.utils

import android.content.Context
import android.graphics.drawable.Drawable
import android.widget.ImageView
import androidx.annotation.DrawableRes
import com.bumptech.glide.Glide
import com.bumptech.glide.RequestBuilder
import com.bumptech.glide.load.resource.bitmap.BitmapTransformation
import com.bumptech.glide.request.RequestOptions


object GlideUtils {

    /**
     * 普通加载
     */
    @JvmOverloads
    fun load(
        url: Any?,
        imageView: ImageView?,
        @DrawableRes errorDefaultRes: Int? = null,
    ) {
        if (imageView == null) return
        Glide.with(imageView.context).load(url).apply {
            if (errorDefaultRes != null) {
                placeholder(errorDefaultRes)
                    .fallback(errorDefaultRes)
                    .error(errorDefaultRes)
            }
        }.into(imageView)
    }

    @JvmOverloads
    fun loadCrop(
        url: Any?,
        imageView: ImageView,
        overrideSize: Int? = null,
        errorDrawable: Drawable? = null,
        @DrawableRes errorDefaultRes: Int? = null,
    ) {
        Glide.with(imageView.context).load(url).apply {
            if (overrideSize != null) override(overrideSize)
            if (errorDefaultRes != null) {
                placeholder(errorDefaultRes)
                    .fallback(errorDefaultRes)
                    .error(errorDefaultRes)
            } else if (errorDrawable != null) {
                placeholder(errorDrawable)
                    .fallback(errorDrawable)
                    .error(errorDrawable)
            }
        }.into(imageView)
    }

    @JvmOverloads
    fun loadTransform(
        url: Any?,
        loadTransform: BitmapTransformation,
        imageView: ImageView,
        errorDrawable: Drawable? = null,
        @DrawableRes errorDefaultRes: Int? = null,
    ) {
        Glide.with(imageView.context).load(url).transform(loadTransform).apply {
            if (errorDefaultRes != null) {
                placeholder(errorDefaultRes)
                    .fallback(errorDefaultRes)
                    .error(errorDefaultRes)
                    .thumbnail(getTransform(imageView.context, errorDefaultRes, loadTransform))
            } else if (errorDrawable != null) {
                placeholder(errorDrawable)
                    .fallback(errorDrawable)
                    .error(errorDrawable)
                    .thumbnail(getTransform(imageView.context, errorDrawable, loadTransform))
            }
        }
            .into(imageView)
    }

    @JvmOverloads
    private fun getTransform(
        context: Context,
        url: Any?,
        loadTransform: BitmapTransformation,
    ): RequestBuilder<Drawable> {
        return Glide.with(context).load(url).transform(loadTransform)
    }

    /**
     * 加载圆形
     */
    @JvmOverloads
    fun loadCircle(
        url: Any?,
        imageView: ImageView,
        errorDrawable: Drawable? = null,
        @DrawableRes errorDefaultRes: Int? = null,
    ) {
        loadTransform(url, CircleGlideTransform(), imageView, errorDrawable, errorDefaultRes)
    }

    /**
     * 加载圆角
     */
    @JvmOverloads
    fun loadRound(
        url: Any?,
        imageView: ImageView,
        errorDrawable: Drawable? = null,
        @DrawableRes errorDefaultRes: Int? = null,
    ) {
        loadTransform(
            url,
            RoundGlideTransform(isSquare = false),
            imageView,
            errorDrawable,
            errorDefaultRes
        )
    }

    /**
     * load round
     */
    @JvmOverloads
    fun loadRound(
        url: Any?,
        imageView: ImageView,
        round: Float,
        errorDrawable: Drawable? = null,
        @DrawableRes errorDefaultRes: Int? = null,
    ) {
        loadTransform(
            url,
            RoundGlideTransform(round, round, round, round, isSquare = false),
            imageView,
            errorDrawable,
            errorDefaultRes
        )
    }

    @JvmOverloads
    fun loadRound(
        url: Any?,
        roundLeftTop: Float,
        roundRightTop: Float,
        roundRightBottom: Float,
        roundLeftBottom: Float,
        imageView: ImageView,
        errorDrawable: Drawable? = null,
        @DrawableRes errorDefaultRes: Int? = null,
    ) {
        loadTransform(
            url,
            RoundGlideTransform(
                roundLeftTop,
                roundRightTop,
                roundRightBottom,
                roundLeftBottom,
                isSquare = false
            ),
            imageView,
            errorDrawable,
            errorDefaultRes
        )
    }

    /**
     * 加载第四秒的帧数作为封面
     * url就是视频的地址
     */
    fun loadCover(url: String?, imageView: ImageView, round: Float) {
        imageView.scaleType = ImageView.ScaleType.CENTER_CROP
        Glide.with(imageView.context)
            .setDefaultRequestOptions(
                RequestOptions()
                    .frame(1000000)
                    .centerCrop()
            )
            .load(url).transform(
                RoundGlideTransform(
                    round,
                    round,
                    round,
                    round,
                    isSquare = false
                )
            )
            .into(imageView)
    }

}