package com.kaixed.kchat.view.adapter

import android.content.Context
import android.content.Intent
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.RecyclerView.ViewHolder
import com.kaixed.kchat.databinding.FriendRecycleItemBinding
import com.kaixed.kchat.model.friend.FriendItem
import com.kaixed.kchat.view.activity.ChatActivity

/**
 * @Author: kaixed
 * @Date: 2024/10/14 14:47
 * @Description: TODO
 */
class FriendListAdapter(private val items: MutableList<FriendItem>, private val context: Context) :
    RecyclerView.Adapter<FriendListAdapter.MyViewHolder>() {
    class MyViewHolder(val binding: FriendRecycleItemBinding) : ViewHolder(binding.root)

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
        val binding = FriendRecycleItemBinding.inflate(
            LayoutInflater.from(parent.context),
            parent,
            false
        )
        return MyViewHolder(binding)
    }

    override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
        val item = items[position]
        holder.binding.tvSendMessage.setOnClickListener {
            val intent = Intent(context, ChatActivity::class.java)
            intent.putExtra("friendId", item.username)
            context.startActivity(intent)
        }
    }

    override fun getItemCount(): Int = items.size
}