package com.adealink.weparty.profile.ui.about.adapter

import android.annotation.SuppressLint
import android.view.LayoutInflater
import android.view.ViewGroup
import com.adealink.frame.util.onClick
import com.adealink.weparty.commonui.ext.getActivity
import com.adealink.weparty.commonui.ext.hide
import com.adealink.weparty.commonui.recycleview.adapter.BindingViewHolder
import com.adealink.weparty.commonui.recycleview.adapter.multitype.ItemViewBinder
import com.adealink.weparty.profile.databinding.LayoutProfileDynamicPhotoBinding
import com.adealink.weparty.profile.ui.about.data.DynamicPhotoItemData
import com.adealink.weparty.util.goImagePreviewActivity


class DynamicPhotoViewBinder(
    val previewImage: (positon: Int, item: DynamicPhotoItemData) -> Unit
) : ItemViewBinder<DynamicPhotoItemData, DynamicPhotoViewBinder.ViewHolder>() {

    override fun onCreateViewHolder(
        inflater: LayoutInflater,
        parent: ViewGroup
    ): ViewHolder {
        return ViewHolder(LayoutProfileDynamicPhotoBinding.inflate(inflater, parent, false)).also {
            it.initView()
        }
    }

    @SuppressLint("SetTextI18n")
    override fun onBindViewHolder(
        holder: ViewHolder,
        item: DynamicPhotoItemData
    ) {
        holder.update(item)
    }

    inner class ViewHolder(
        rootBinding: LayoutProfileDynamicPhotoBinding,
    ) : BindingViewHolder<LayoutProfileDynamicPhotoBinding>(rootBinding) {

        private var item: DynamicPhotoItemData? = null

        fun initView() {
            binding.root.onClick {
                item?.let { item ->
                    previewImage.invoke(layoutPosition, item)
                }
            }
        }

        fun update(item: DynamicPhotoItemData) {
            this@ViewHolder.item = item
            if (item.data == null) {
                binding.ivImg.hide()
                return
            }
            binding.ivImg.setImageUrl(item.data.url)
        }

        private fun previewImage() {
            val itemData = item?.data
            if (itemData == null || itemData.url.isEmpty()) {
                return
            }
            val act = binding.root.getActivity() ?: return
            goImagePreviewActivity(
                act,
                arrayListOf<String>(itemData.url),
            )
        }

    }


}
