package com.adealink.weparty.playmate.setting.adapter

import android.view.LayoutInflater
import android.view.ViewGroup
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.commonui.widget.switchbutton.SwitchButton.OnCheckChangedListener
import com.adealink.weparty.playmate.databinding.LayoutPlaymateSkillSettingItemBinding
import com.adealink.weparty.playmate.setting.data.SkillSettingItemData

class SkillItemViewBinder(
    val goDetail: (SkillSettingItemData) -> Unit,
    val onSwitchChanged: (item: SkillSettingItemData, open: Boolean, position: Int) -> Unit
) : ItemViewBinder<SkillSettingItemData, BindingViewHolder<LayoutPlaymateSkillSettingItemBinding>>() {

    override fun onBindViewHolder(
        holder: BindingViewHolder<LayoutPlaymateSkillSettingItemBinding>,
        item: SkillSettingItemData,
    ) {
        holder.binding.root.onClick {
            goDetail.invoke(item)
        }
        holder.binding.ivIcon.setImageUrl(item.data.categoryIcon)
        holder.binding.tvName.text = item.data.categoryName
        holder.binding.vPrice.setPrice(item.data.price, item.data.unit)
        holder.binding.vSwitch.setChecked(item.data.open)
        holder.binding.vSwitch.checkChangedListener = object : OnCheckChangedListener {
            override fun onChecked(isChecked: Boolean, fromUser: Boolean) {
                if (fromUser) {
                    onSwitchChanged.invoke(item, isChecked, holder.layoutPosition)
                }
            }
        }
    }

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