package com.kaixed.kchat.ui.fragment

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.viewModels
import androidx.recyclerview.widget.LinearLayoutManager
import com.kaixed.kchat.R
import com.kaixed.kchat.databinding.FragmentContactBinding
import com.kaixed.kchat.model.friend.FriendItem
import com.kaixed.kchat.ui.adapter.FriendListAdapter
import com.kaixed.kchat.ui.base.BaseFragment
import com.kaixed.kchat.utils.ConstantsUtil.getUsername
import com.kaixed.kchat.viewmodel.FriendListViewModel


class ContactFragment : BaseFragment<FragmentContactBinding>() {

    private val friendListViewModel: FriendListViewModel by viewModels()

    private var friendList: MutableList<FriendItem> = mutableListOf()

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

    override fun inflateBinding(
        inflater: LayoutInflater,
        container: ViewGroup?
    ): FragmentContactBinding {
        return FragmentContactBinding.inflate(inflater, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        getDefaultItem()
        getFriendList(getUsername())

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


    private fun getFriendList(username: String) {
        friendListViewModel.getFriendList(username).observe(requireActivity()) { value ->
            value?.let {
                friendList.addAll(value)
                binding.recycleFriendList.adapter?.notifyDataSetChanged()
            }
        }
    }

    private fun getDefaultItem() {
        friendList.add(
            FriendItem(
                username = "",
                avatarUrl = "",
                nickname = "新的朋友",
                remark = "",
                signature = ""
            )
        )
        friendList.add(
            FriendItem(
                username = "",
                avatarUrl = "",
                nickname = "群聊",
                remark = "",
                signature = ""
            )
        )
        friendList.add(
            FriendItem(
                username = "",
                avatarUrl = "",
                nickname = "标签",
                remark = "",
                signature = ""
            )
        )
        friendList.add(
            FriendItem(
                username = "",
                avatarUrl = "",
                nickname = "公众号",
                remark = "",
                signature = ""
            )
        )
        friendList.add(
            FriendItem(
                username = "",
                avatarUrl = "",
                nickname = "服务号",
                remark = "",
                signature = ""
            )
        )
    }
}
