package com.adealink.weparty.share.poster.adapter

import android.view.LayoutInflater
import android.view.ViewGroup
import com.adealink.frame.aab.util.getCompatDrawable
import com.adealink.frame.util.onClick
import com.adealink.weparty.commonui.commonSelectedDrawable
import com.adealink.weparty.commonui.recycleview.adapter.BindingViewHolder
import com.adealink.weparty.commonui.recycleview.adapter.multitype.ItemViewBinder
import com.adealink.weparty.share.R
import com.adealink.weparty.share.databinding.LayoutSharePosterPhotoItemBinding
import com.adealink.weparty.share.poster.data.PhotoItemData

class PhotoItemViewBinder(val listener: OnPhotoSelectedListener) :
    ItemViewBinder<PhotoItemData, BindingViewHolder<LayoutSharePosterPhotoItemBinding>>() {

    override fun onBindViewHolder(
        holder: BindingViewHolder<LayoutSharePosterPhotoItemBinding>,
        item: PhotoItemData,
    ) {
        holder.binding.ivPhoto.setImageUrl(item.data.url)
        if (item.selected) {
            holder.binding.vSelected.background = commonSelectedDrawable()
        } else {
            holder.binding.vSelected.background =
                getCompatDrawable(R.drawable.share_poster_photo_unselected)
        }
        holder.binding.root.onClick {
            listener.onSelected(item)
        }
    }

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

    interface OnPhotoSelectedListener {
        fun onSelected(data: PhotoItemData)
    }

}