package com.adealink.weparty.setting.language.adapter

import android.view.LayoutInflater
import android.view.ViewGroup
import com.adealink.frame.locale.language.data.Language
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.setting.databinding.LayoutLanguageItemBinding
import com.adealink.weparty.setting.language.data.LanguageItemData
import com.adealink.weparty.R as APP_R

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

    override fun onBindViewHolder(
        holder: BindingViewHolder<LayoutLanguageItemBinding>,
        item: LanguageItemData,
    ) {
        if (item.showDivider) {
            holder.binding.vDivider.show()
        } else {
            holder.binding.vDivider.gone()
        }
        holder.binding.tvLanguage.text = item.language.desc
        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)
        }
    }

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

    interface OnLanguageChooseListener {
        fun onChoose(language: Language)
    }
}