package com.kaixed.kchat.ui.adapter

import android.annotation.SuppressLint
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.bumptech.glide.Glide
import com.kaixed.kchat.databinding.ContactRecycleFooterItemBinding
import com.kaixed.kchat.databinding.FriendRecycleItemBinding
import com.kaixed.kchat.model.friend.FriendItem
import com.kaixed.kchat.ui.activity.ChatActivity

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

    class ItemViewHolder(val binding: FriendRecycleItemBinding) : ViewHolder(binding.root)

    class FooterViewHolder(val binding: ContactRecycleFooterItemBinding) : ViewHolder(binding.root)

    companion object {
        const val TYPE_ITEM: Int = 0
        const val TYPE_FOOTER: Int = 1
    }

    private val defaultItems = listOf(
        com.kaixed.kchat.R.drawable.ic_contact_new_friend,
        com.kaixed.kchat.R.drawable.ic_contact_group,
        com.kaixed.kchat.R.drawable.ic_contact_label,
        com.kaixed.kchat.R.drawable.ic_contact_public_account,
        com.kaixed.kchat.R.drawable.ic_contact_service_account
    )

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        return when (viewType) {
            TYPE_FOOTER -> {
                val binding = ContactRecycleFooterItemBinding.inflate(
                    LayoutInflater.from(parent.context),
                    parent,
                    false
                )
                FooterViewHolder(binding)
            }

            else -> {
                val binding = FriendRecycleItemBinding.inflate(
                    LayoutInflater.from(parent.context),
                    parent,
                    false
                )
                ItemViewHolder(binding)
            }
        }
    }

    @SuppressLint("SetTextI18n")
    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        when (holder) {
            is FooterViewHolder -> {
                holder.binding.tvTip.text = "${itemCount - defaultItems.size - 1} 个朋友"
            }

            is ItemViewHolder -> {
                val item = items[position]
                holder.binding.tvNickname.text = item.nickname

                if (position <= defaultItems.size - 1) {
                    holder.binding.ifvAvatar.setImageResource(defaultItems[position])
                    holder.binding.ifvAvatar.setBackgroundColor(context.getColor(com.kaixed.kchat.R.color.gray))
                } else {
                    Glide.with(context).load(item.avatarUrl).into(holder.binding.ifvAvatar)
                    holder.binding.root.setOnClickListener {
                        val intent = Intent(context, ChatActivity::class.java)
                        intent.putExtra("friendId", item.username)
                        context.startActivity(intent)
                    }
                }
            }
        }
    }

    override fun getItemCount(): Int = items.size + 1

    override fun getItemViewType(position: Int): Int {
        return if (position + 1 == getItemCount()) {
            TYPE_FOOTER
        } else {
            TYPE_ITEM
        }
    }
}
