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

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.core.graphics.toColorInt
import com.adealink.frame.aab.util.getCompatDrawable
import com.adealink.frame.util.onClick
import com.adealink.weparty.commonui.drawabletoolbox.GradientBorderBuilder
import com.adealink.weparty.commonui.drawabletoolbox.GradientBorderDrawable
import com.adealink.weparty.commonui.ext.dpf
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 = PHOTO_SELECT_DRAWABLE
        } 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)
    }

    companion object {
        val PHOTO_SELECT_DRAWABLE: GradientBorderDrawable = GradientBorderBuilder()
            .setRadii(
                floatArrayOf(
                    12.dpf(),
                    12.dpf(),
                    12.dpf(),
                    12.dpf(),
                    12.dpf(),
                    12.dpf(),
                    12.dpf(),
                    12.dpf()
                )
            )
            .setBorderWidth(3.dpf())
            .setBorderAngle(315)
            .setBorderColors(intArrayOf("#4ED2FF".toColorInt(), "#B1EF5D".toColorInt()))
            .build()
    }
}