package com.lukouguoji.gjj.holder

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.gjj.databinding.ItemGjjManifestPicBinding
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.db.perference.SharedPreferenceUtil
import com.lukouguoji.module_base.ktx.commonAdapter
import com.lukouguoji.module_base.ui.page.preview.PreviewActivity

class GjjManifestPicViewHolder(view: View) :
    BaseViewHolder<FileBean, ItemGjjManifestPicBinding>(view) {

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

        // 带 Authorization header 加载图片
        if (bean.path.isNotEmpty()) {
            val glideUrl = GlideUrl(
                bean.path,
                LazyHeaders.Builder()
                    .addHeader("Authorization", SharedPreferenceUtil.getString(Constant.Share.token))
                    .build()
            )
            Glide.with(itemView.context).load(glideUrl).into(binding.ivThumbnail)
        }

        binding.ivThumbnail.setOnClickListener {
            val items = getRecyclerView()?.commonAdapter()?.items
                ?.filterIsInstance<FileBean>() ?: listOf(bean)
            val originalList = items.map { fb ->
                FileBean(path = if (fb.originalPic.isNotEmpty()) fb.originalPic else fb.path)
            }
            PreviewActivity.start(itemView.context, originalList, position)
        }
    }
}
