package com.renrenliao.im.utils

import android.widget.ImageView
import com.bumptech.glide.Glide
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.bumptech.glide.request.RequestOptions
import com.renrenliao.im.R
import com.renrenliao.im.config.Const
import com.renrenliao.im.utils.FileUtils.getOpenAdFile
import jp.wasabeef.glide.transformations.RoundedCornersTransformation
import java.io.File

object GlideUtils {

    private fun userOption(): RequestOptions {
        return RequestOptions().dontAnimate()
            .diskCacheStrategy(DiskCacheStrategy.ALL)
            .placeholder(R.drawable.default_avatar_50_3x)
            .error(R.drawable.default_avatar_50_3x)
            .transform(RoundedCornersTransformation(10, 0))
    }

    private fun comOption(): RequestOptions {
        return RequestOptions().dontAnimate()
            .diskCacheStrategy(DiskCacheStrategy.ALL)
            .placeholder(R.color.transparent)
            .error(R.color.transparent)
           // .transform(RoundedCornersTransformation(10, 0))
    }

    fun loadUserImage(imageView: ImageView, url:String){
        loadImage(imageView,url, userOption())
    }

    private fun loadImage(imageView: ImageView, url:String,options: RequestOptions){
        Glide.with(imageView.context).load(url).apply(options).into(imageView)
    }

    fun loadOpenAdImg(url: String, imageView: ImageView) {
        val file: File = getOpenAdFile(url)
        val loadOption =  RequestOptions().dontAnimate()
            .diskCacheStrategy(DiskCacheStrategy.ALL)
            .placeholder(R.color.transparent)
            .error(R.color.transparent)
        if (file.exists() && file.length() > 0) {
            Glide.with(imageView.context).load(file).apply(loadOption).into(imageView)
        } else {
            Glide.with(imageView.context).load(url).apply(loadOption).into(imageView)
        }
    }
    fun loadComImage(imageView: ImageView, url:String){
        loadImage(imageView,url, comOption())
    }
    /**根据uid 拼接的地址或者头像 主要是在用户头像变动后 自动更新头像
     通过avatarTag这张表  全局liveData自动更新头像
     **/
//    fun loadUserAvatarByVersion(imageView: ImageView,avatarVersion:Long){
//        val avatarUrl = Const.COMMON_IMG_USER_FILE + avatarVersion+"/"+avatarVersion+".png"
//        loadUserImage(imageView,avatarUrl)
//    }


    fun clearMemory(){

    }

    fun clearDisk(){

    }
}