package com.gh.gamecenter.servers.add

import android.content.Context
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.gh.gamecenter.common.base.BaseRecyclerViewHolder
import com.gh.gamecenter.R
import com.gh.gamecenter.databinding.LayoutAddKaifuPopupItemBinding
import com.lightgame.adapter.BaseRecyclerAdapter

class AddKaiFuPopupAdapter(
    context: Context,
    data: ArrayList<String>,
    callback: (text: String) -> Unit
) :
    BaseRecyclerAdapter<RecyclerView.ViewHolder>(context) {

    var nameList = data

    val action = callback

    fun setListData(data: ArrayList<String>) {
        nameList = data
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        return AddKaiFuPopupViewHolder(
            LayoutAddKaifuPopupItemBinding.bind(
                mLayoutInflater.inflate(
                    R.layout.layout_add_kaifu_popup_item,
                    parent,
                    false
                )
            )
        )
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        if (holder is AddKaiFuPopupViewHolder) {
            holder.binding.hintText.text = nameList[position]
            holder.binding.hintText.setOnClickListener { action.invoke(nameList[position]) }
            if (position == nameList.size - 1) {
                holder.binding.divider.visibility = View.GONE
            }
        }
    }

    override fun getItemCount(): Int = nameList.size

    class AddKaiFuPopupViewHolder(val binding: LayoutAddKaifuPopupItemBinding) :
        BaseRecyclerViewHolder<String>(binding.root)
}