package com.adealink.frame.commonui.widget.binder

import android.view.LayoutInflater
import android.view.ViewGroup
import com.adealink.weparty.commonui.recycleview.adapter.BindingViewHolder
import com.adealink.weparty.commonui.recycleview.adapter.multitype.ItemViewBinder
import com.adealink.weparty.commonui.widget.CommonBottomListItem
import com.adealink.weparty.commonui.widget.switchbutton.SwitchButton
import com.adealink.weparty.databinding.ItemCommonBottomSwitchBinding


class CommonBottomListItemBinder(
    val onSwitchListener: ((checked: Boolean?, data: CommonBottomListItem<*>) -> Unit)? = null
) : ItemViewBinder<CommonBottomListItem<*>, CommonBottomListItemBinder.ViewHolder>() {


    inner class ViewHolder(binding: ItemCommonBottomSwitchBinding) :
        BindingViewHolder<ItemCommonBottomSwitchBinding>(binding) {
        fun update(item: CommonBottomListItem<*>) {
            binding.itemTitle.text = item.title
            binding.switchBtn.setChecked(item.itemChecked ?: false)
            binding.switchBtn.checkChangedListener = object : SwitchButton.OnCheckChangedListener {
                override fun onChecked(isChecked: Boolean, fromUser: Boolean) {
                    if (fromUser) {
                        onSwitchListener?.invoke(isChecked, item)
                    }
                }
            }
        }
    }

    override fun onCreateViewHolder(inflater: LayoutInflater, parent: ViewGroup): ViewHolder {
        return ViewHolder(ItemCommonBottomSwitchBinding.inflate(inflater, parent, false))
    }

    override fun onBindViewHolder(holder: ViewHolder, item: CommonBottomListItem<*>) {
        holder.update(item)
    }

}