package com.kaixed.kchat.ui.activity

import android.os.Bundle
import androidx.activity.enableEdgeToEdge
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
import com.kaixed.kchat.databinding.ActivityMessageBinding
import com.kaixed.kchat.model.friend.FriendRequestItem
import com.kaixed.kchat.ui.adapter.MessageListAdapter
import com.kaixed.kchat.utils.ConstantsUtil.getUsername
import com.kaixed.kchat.viewmodel.ContactViewModel

class MessageActivity : AppCompatActivity() {
    private lateinit var binding: ActivityMessageBinding
    private var items = mutableListOf<FriendRequestItem>()
    private val contactViewModel: ContactViewModel by viewModels()
    private lateinit var messageListAdapter: MessageListAdapter
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        binding = ActivityMessageBinding.inflate(layoutInflater)
        setContentView(binding.root)

        getItems()

        messageListAdapter = MessageListAdapter(items, this)

        binding.recycleFriendRequestList.layoutManager =
            LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false)
        binding.recycleFriendRequestList.adapter = messageListAdapter
    }

    private fun getItems() {
        contactViewModel.getContactRequestList(getUsername())
            .observe(this) { value ->
                if (value != null) {
                    items.addAll(value.data.toMutableList())
                    messageListAdapter.notifyDataSetChanged()
                }
            }
    }
}
