package com.lukouguoji.module_base.ui.page.preview

import android.view.View
import com.bumptech.glide.Glide
import com.bumptech.glide.load.model.GlideUrl
import com.bumptech.glide.load.model.LazyHeaders
import com.lukouguoji.module_base.base.BaseViewHolder
import com.lukouguoji.module_base.bean.FileBean
import com.lukouguoji.module_base.common.Constant
import com.lukouguoji.module_base.databinding.ItemPreviewImageBinding
import com.lukouguoji.module_base.db.perference.SharedPreferenceUtil

/**
 * @author：孟凡华
 * @date：2023/4/21
 * @desc：
 */
class PreviewImageViewHolder(view: View) :
    BaseViewHolder<FileBean, ItemPreviewImageBinding>(view) {

    override fun onBind(item: Any?, position: Int) {
        val bean = getItemBean(item) ?: return
        binding.bean = bean

        // 加载图片
        val path = bean.path
        if (path.isNotEmpty()) {
            if (path.startsWith("http")) {
                // 网络图片带 Authorization header
                val glideUrl = GlideUrl(
                    path,
                    LazyHeaders.Builder()
                        .addHeader("Authorization", SharedPreferenceUtil.getString(Constant.Share.token))
                        .build()
                )
                Glide.with(itemView.context).load(glideUrl).into(binding.photoView)
            } else {
                // 本地图片直接加载
                Glide.with(itemView.context).load(path).into(binding.photoView)
            }
        }
    }

}