package com.adealink.weparty.profile.dynamic.adapter

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.lifecycle.LifecycleOwner
import com.adealink.frame.util.onClick
import com.adealink.weparty.commonui.recycleview.adapter.BindingViewHolder
import com.adealink.weparty.commonui.recycleview.adapter.multitype.ItemViewBinder
import com.adealink.weparty.profile.databinding.ItemEditProfilePhotoBinding
import com.adealink.weparty.profile.edit.data.PhotoItemData


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

    override fun onBindViewHolder(
        holder: BindingViewHolder<ItemEditProfilePhotoBinding>,
        item: PhotoItemData,
    ) {
        holder.binding.root.onClick {
            onItemClick.invoke(item)
        }
        holder.binding.ivDelete.onClick {
            onDeleteClick.invoke(item)
        }
        if (item.data.isRemote()) {
            holder.binding.ivPicture.setImageUrl(item.data.url)
        } else {
            holder.binding.ivPicture.setImageURI(item.data.uri)
        }
        holder.binding.vUploadProgress.observeProgress(lifecycleOwner, item.data.mediaUpload)
    }

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