package com.lukouguoji.module_base.impl

import android.view.View
import com.lukouguoji.module_base.adapter.loadImage
import com.lukouguoji.module_base.base.BaseViewHolder
import com.lukouguoji.module_base.bean.FileBean
import com.lukouguoji.module_base.databinding.PdaItemImageSelectBinding
import com.lukouguoji.module_base.ktx.commonAdapter
import com.lukouguoji.module_base.ktx.logd
import com.lukouguoji.module_base.ktx.loge
import com.lukouguoji.module_base.ui.page.preview.PreviewActivity
import com.lukouguoji.module_base.util.MediaUtil

class PdaImageSelectViewHolder(view: View) : BaseViewHolder<FileBean, PdaItemImageSelectBinding>(view) {

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

        binding.rl.setOnClickListener {
            if (bean.path.isEmpty()) {
                MediaUtil.pickImage(itemView.context, maxNum = 10) {
                    it.forEach {
                        logd("添加了图片 : ${it.realPath}")
                        getRecyclerView()?.commonAdapter()?.addItem(FileBean(path = it.realPath))
                    }
                }
            } else {
                PreviewActivity.start(itemView.context, listOf(bean))
            }
        }

        // 长按事件
        binding.rl.setOnLongClickListener {
            clickListener?.onItemClick(bindingAdapterPosition, binding.rl.id)
            true
        }

        notifyItemClick(position, binding.ivDelete)

        if (bean.isOnlineResource()) {
            loge("开始下载 : ${bean.path}")
            bean.download {
                loadImage(binding.iv, it)
            }
        }
    }

}