package com.kaixed.kchat.view.activity

import android.os.Bundle
import android.util.Log
import android.view.View
import androidx.activity.enableEdgeToEdge
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
import com.kaixed.kchat.database.UserManager
import com.kaixed.kchat.databinding.ActivityFriendListBinding
import com.kaixed.kchat.model.friend.FriendItem
import com.kaixed.kchat.view.adapter.FriendListAdapter
import com.kaixed.kchat.viewmodel.ApplyFriendViewModel
import com.kaixed.kchat.viewmodel.FriendListViewModel

class FriendListActivity : AppCompatActivity() {
    private lateinit var binding: ActivityFriendListBinding
    private val friendListViewModel: FriendListViewModel by viewModels()
    private var friendList: MutableList<FriendItem> = mutableListOf()
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        binding = ActivityFriendListBinding.inflate(layoutInflater)
        setContentView(binding.root)

        getFriendList(UserManager.getInstance().username)

        if (friendList.isEmpty()) {
            binding.tvNothing.visibility = View.VISIBLE
        } else {
            binding.tvNothing.visibility = View.GONE
        }

        binding.recycleFriendList.layoutManager =
            LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false)
        binding.recycleFriendList.adapter = FriendListAdapter(friendList, this)

    }

    private fun getFriendList(username: String) {
        friendListViewModel.getFriendList(username).observe(this) { value ->
            if (value != null) {
                friendList = value as MutableList<FriendItem>
            }
        }
    }
}