package com.kaixed.kchat.ui.adapter

import android.annotation.SuppressLint
import android.content.Context
import android.content.Intent
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.bumptech.glide.request.RequestOptions
import com.kaixed.kchat.R
import com.kaixed.kchat.data.objectbox.entity.Conversation
import com.kaixed.kchat.databinding.ChatMainItemBinding
import com.kaixed.kchat.ui.activity.ChatActivity
import com.kaixed.kchat.ui.i.OnChatListItemClickListener
import com.kaixed.kchat.utils.TextUtil

/**
 * @Author: kaixed
 * @Date: 2024/10/27 18:22
 */
class ConversationAdapter(
    private var chatLists: List<Conversation>, private val context: Context
) : RecyclerView.Adapter<ConversationAdapter.MyViewHolder?>() {
    private var onChatListItemClickListener: OnChatListItemClickListener? = null

    companion object {
        private const val TAG = "ConversationAdapter"
    }

    fun updateData(newChatLists: List<Conversation>) {
        chatLists = newChatLists
    }

    class MyViewHolder(val binding: ChatMainItemBinding) : RecyclerView.ViewHolder(binding.root) {
        fun bindData(chatList: Conversation) {
            binding.tvContent.text = chatList.lastContent
            binding.tvNickname.text = chatList.talkerId
            binding.tvTimestamp.text = TextUtil.getTimestampString(chatList.timestamp)
        }
    }

    fun setItemListener(i: OnChatListItemClickListener?) {
        this.onChatListItemClickListener = i
    }

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

    override fun onBindViewHolder(
        holder: MyViewHolder, @SuppressLint("RecyclerView") position: Int
    ) {
        Log.d(TAG, "onBindViewHolder: $chatLists")
        val options = RequestOptions()
            .placeholder(R.drawable.ic_default_avatar)
            .error(R.drawable.ic_default_avatar)

        val chatList = chatLists[position]
        if (chatList.avatarUrl.isNotEmpty()) {
            Glide.with(context).load(chatList.avatarUrl).apply(options)
                .into(holder.binding.ifvAvatar)
        }

        if (chatList.unreadCount != 0) {
            holder.binding.tvUnreadCount.text = chatList.unreadCount.toString()
            holder.binding.rlUnreadCount.visibility = View.VISIBLE
        } else {
            holder.binding.rlUnreadCount.visibility = View.INVISIBLE
        }

        holder.bindData(chatLists[position])
        holder.itemView.setOnClickListener {
            onChatListItemClickListener!!.onItemClick(chatLists[position].talkerId)

            context.startActivity(Intent(context, ChatActivity::class.java).apply {
                putExtra("contactId", chatLists[position].talkerId)
                putExtra("contactNickname", chatLists[position].nickname)
            })
        }
    }

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