package com.base.locationsharewhite.ui.main

import android.content.Context
import android.view.View
import android.view.ViewGroup
import android.widget.CompoundButton
import android.widget.RadioGroup
import android.widget.RadioGroup.OnCheckedChangeListener
import androidx.recyclerview.widget.RecyclerView.ViewHolder
import com.base.locationsharewhite.R
import com.base.locationsharewhite.bean.ViewerBean
import com.base.locationsharewhite.databinding.ItemViewerBinding
import com.base.locationsharewhite.ui.views.XmlEx.inflate
import com.chad.library.adapter4.BaseQuickAdapter

class ViewerAdapter(
    private val switchAction: (viewerBean: ViewerBean, isChecked: Boolean) -> Unit
) : BaseQuickAdapter<ViewerBean, ViewerAdapter.ViewerViewHolder>() {

    inner class ViewerViewHolder(view: View) : ViewHolder(view)

    override fun onBindViewHolder(holder: ViewerViewHolder, position: Int, item: ViewerBean?) {
        item ?: return
        val binding = ItemViewerBinding.bind(holder.itemView)
        runCatching { binding.tvNameLetters.text = item.nickName.substring(0, 1) }
        binding.tvNameCode.text = item.nickName
        binding.tvTime.text = "time"
        binding.tvSwitch.isChecked = item.status == 1
        binding.tvSwitch.setOnCheckedChangeListener { buttonView, isChecked ->
            if (!buttonView.isPressed) {
                return@setOnCheckedChangeListener
            }
            switchAction.invoke(item, isChecked)
        }
    }

    override fun onCreateViewHolder(context: Context, parent: ViewGroup, viewType: Int): ViewerViewHolder {
        return ViewerViewHolder(R.layout.item_viewer.inflate(parent))
    }
}