package com.adealink.weparty.order.refund.adapter

import android.view.LayoutInflater
import android.view.ViewGroup
import com.adealink.frame.util.onClick
import com.adealink.weparty.commonui.ext.gone
import com.adealink.weparty.commonui.ext.show
import com.adealink.weparty.commonui.recycleview.adapter.BindingViewHolder
import com.adealink.weparty.commonui.recycleview.adapter.multitype.ItemViewBinder
import com.adealink.weparty.order.databinding.ItemRefundPhotoBinding
import com.adealink.weparty.order.refund.data.PhotoItemData


class PhotoItemViewBinder(
    val onItemClick: (item: PhotoItemData) -> Unit,
    val onDeleteClick: (item: PhotoItemData) -> Unit
) :
    ItemViewBinder<PhotoItemData, BindingViewHolder<ItemRefundPhotoBinding>>() {

    override fun onBindViewHolder(
        holder: BindingViewHolder<ItemRefundPhotoBinding>,
        item: PhotoItemData,
    ) {
        holder.binding.root.onClick {
            onItemClick.invoke(item)
        }
        if (item.showDelete) {
            holder.binding.ivDelete.show()
            holder.binding.ivDelete.onClick {
                onDeleteClick.invoke(item)
            }
        } else {
            holder.binding.ivDelete.gone()
        }
        if (item.data.isRemote()) {
            holder.binding.ivPicture.setImageUrl(item.data.url)
        } else {
            holder.binding.ivPicture.setImageURI(item.data.uri)
        }
    }

    override fun onCreateViewHolder(
        inflater: LayoutInflater,
        parent: ViewGroup,
    ): BindingViewHolder<ItemRefundPhotoBinding> {
        return BindingViewHolder(
            ItemRefundPhotoBinding.inflate(
                inflater,
                parent,
                false
            )
        )
    }
}