package com.atmob.keyboard_android.ui.popup.item

import android.graphics.Typeface
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.atmob.keyboard_android.R
import com.atmob.keyboard_android.ext.click
import com.atmob.keyboard_android.model.ModeItem
import me.drakeet.multitype.ItemViewBinder

/**
 * 模式列表
 */
class ModeListItemViewBinder(
    private val onItemClick: (item: ModeItem) -> Unit
) : ItemViewBinder<ModeItem, ModeListItemViewBinder.InnerViewHolder>() {
    override fun onCreateViewHolder(
        inflater: LayoutInflater,
        parent: ViewGroup
    ): InnerViewHolder {
        return InnerViewHolder(
            inflater.inflate(
                R.layout.item_mode,
                parent,
                false
            )
        )
    }

    override fun onBindViewHolder(
        holder: InnerViewHolder,
        item: ModeItem
    ) {
        holder.vMode.apply {
            text = item.helpMode.modeName
            // 选中
            if (item.selected) {
                setTypeface(typeface, Typeface.BOLD)
                setBackgroundResource(R.drawable.bg_mode_selected)
            } else {
                // 未选中
                setTypeface(typeface, Typeface.NORMAL)
                setBackgroundDrawable(null)
            }
        }
        holder.itemView.click {
            onItemClick.invoke(item)
        }
    }

    class InnerViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val vMode: TextView = itemView.findViewById(R.id.mode)
    }
}