package com.adealink.weparty.joinus.step2.dialog

import android.view.LayoutInflater
import android.view.ViewGroup
import com.adealink.frame.util.onClick
import com.adealink.weparty.commonui.ext.gone
import com.adealink.weparty.commonui.ext.show
import com.adealink.weparty.commonui.recycleview.adapter.BindingViewHolder
import com.adealink.weparty.commonui.recycleview.adapter.multitype.ItemViewBinder
import com.adealink.weparty.joinus.databinding.LayoutJoinUsLanguageItemBinding
import com.adealink.weparty.joinus.step2.data.LanguageConfigData
import com.adealink.weparty.joinus.step2.data.LanguageItemData
import com.adealink.weparty.R as APP_R

class LanguageItemViewBinder(val listener: OnLanguageChooseListener) :
    ItemViewBinder<LanguageItemData, BindingViewHolder<LayoutJoinUsLanguageItemBinding>>() {

    override fun onBindViewHolder(
        holder: BindingViewHolder<LayoutJoinUsLanguageItemBinding>,
        item: LanguageItemData,
    ) {
        if (item.showDivider) {
            holder.binding.vDivider.show()
        } else {
            holder.binding.vDivider.gone()
        }
        holder.binding.tvLanguage.text = item.language.name
        if (item.selected) {
            holder.binding.ivSelected.setImageResource(APP_R.drawable.common_check_selected_ic)
        } else {
            holder.binding.ivSelected.setImageResource(APP_R.drawable.common_check_unselected_ic)
        }
        holder.binding.root.onClick {
            listener.onChoose(item.language, holder.layoutPosition)
        }
    }

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

    interface OnLanguageChooseListener {
        fun onChoose(language: LanguageConfigData, position: Int)
    }
}