package com.common.commonlib.image.loader

import android.graphics.drawable.Drawable
import com.bumptech.glide.RequestBuilder
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.bumptech.glide.load.resource.bitmap.BitmapTransformation
import com.common.commonlib.image.config.GlideBaseConfig

/**
 * 加载配置辅助类
 *
 * @author wangym
 * @since 2021/7/28
 */
object ConfigHelper {

    /**
     * 带有过渡效果的加载
     * @param requestBuilder 请求构建器
     * @param transformations 过渡效果
     *
     * @return 请求构建器
     */
    fun loadWithTransformation(
        requestBuilder: RequestBuilder<Drawable>,
        transformations: List<BitmapTransformation>
    ): RequestBuilder<Drawable> {
        var temp = requestBuilder
        for (item in transformations) {
            temp = temp.transform(item)
        }
        return temp
    }

    /**
     * 带有缓存模式的加载
     * @param requestBuilder 请求构建器
     * @param loadingMode 加载缓存模式
     *
     * @return 请求构建器
     */
    fun loadWithMod(
        requestBuilder: RequestBuilder<Drawable>,
        loadingMode: GlideBaseConfig.LoadMode
    ): RequestBuilder<Drawable> {
        var request = requestBuilder
        request = when (loadingMode) {
            GlideBaseConfig.LoadMode.DEFAULT ->
                request
            GlideBaseConfig.LoadMode.ALL ->
                request.diskCacheStrategy(DiskCacheStrategy.ALL)
            GlideBaseConfig.LoadMode.SKIP_CACHE ->
                request.skipMemoryCache(true)
                    .diskCacheStrategy(DiskCacheStrategy.NONE)
            GlideBaseConfig.LoadMode.SKIP_DISK ->
                request.diskCacheStrategy(DiskCacheStrategy.NONE)
            GlideBaseConfig.LoadMode.SKIP_MEM ->
                request.skipMemoryCache(true)
            GlideBaseConfig.LoadMode.SKIP_NET ->
                request.onlyRetrieveFromCache(true)
        }
        return request
    }
}