package com.kaixed.kchat.viewmodel

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.kaixed.kchat.data.objectbox.ObjectBox.get
import com.kaixed.kchat.data.objectbox.entity.Contact
import com.kaixed.kchat.model.friend.FriendRequestItem
import com.kaixed.kchat.model.search.User
import com.kaixed.kchat.repository.ContactRepository
import io.objectbox.Box
import kotlinx.coroutines.launch

class ContactViewModel : ViewModel() {

    private val contactRepository = ContactRepository()

    // 获取联系人请求列表
    private val _contactRequestListResult = MutableLiveData<Result<List<FriendRequestItem>?>>()
    val contactRequestListResult: LiveData<Result<List<FriendRequestItem>?>> =
        _contactRequestListResult

    // 接受联系人请求
    private val _acceptContactRequestResult = MutableLiveData<Result<Contact?>>()
    val acceptContactRequestResult: LiveData<Result<Contact?>> =
        _acceptContactRequestResult

    // 添加联系人
    private val _addContactResult = MutableLiveData<Result<String>>()
    val addContactResult: LiveData<Result<String>> = _addContactResult

    // 搜索联系人
    private val _searchContactResult = MutableLiveData<Result<User?>>()
    val searchContactResult: LiveData<Result<User?>> = _searchContactResult

    // 获取联系人列表
    private val _contactListResult = MutableLiveData<Result<List<Contact>?>>()
    val contactListResult: LiveData<Result<List<Contact>?>> = _contactListResult

    // 删除联系人
    private val _deleteContactResult = MutableLiveData<Result<String?>>()
    val deleteContactResult: LiveData<Result<String?>> = _deleteContactResult

    // 删除联系人
    fun deleteContact(username: String, contactId: String) {
        viewModelScope.launch {
            val result = contactRepository.deleteContact(username, contactId)
            _deleteContactResult.postValue(result)
        }
    }

    // 获取联系人请求列表
    fun getContactRequestList(username: String) {
        viewModelScope.launch {
            val result = contactRepository.getContactRequestList(username)
            _contactRequestListResult.postValue(result)
        }
    }

    // 接受联系人请求
    fun acceptContactRequest(username: String, contactId: String, remark: String) {
        viewModelScope.launch {
            val result = contactRepository.acceptContactRequest(username, contactId, remark)
            _acceptContactRequestResult.postValue(result)
        }
    }

    // 添加联系人
    fun addContact(contactId: String, message: String) {
        viewModelScope.launch {
            val result = contactRepository.addContact(contactId, message)
            _addContactResult.postValue(result)
        }
    }

    // 搜索联系人
    fun searchContact(username: String) {
        viewModelScope.launch {
            val result = contactRepository.searchContact(username)
            _searchContactResult.postValue(result)
        }
    }

    fun loadFriendListInDb(): List<Contact> {
        val contactBox: Box<Contact> = get().boxFor(Contact::class.java)

        val sortedContacts = contactBox.query()
            .sort { contact1, contact2 ->
                val str1 = contact1.remarkquanpin ?: contact1.quanpin ?: contact1.nickname
                val str2 = contact2.remarkquanpin ?: contact2.quanpin ?: contact2.nickname

                val type1 = when {
                    str1.matches(Regex("[a-zA-Z]+")) -> 1  // 字母
                    str1.matches(Regex("[0-9]+")) -> 2    // 数字
                    else -> 3                            // 特殊字符
                }

                val type2 = when {
                    str2.matches(Regex("[a-zA-Z]+")) -> 1  // 字母
                    str2.matches(Regex("[0-9]+")) -> 2    // 数字
                    else -> 3                            // 特殊字符
                }

                // 比较类型，如果相同再按字母升序排序
                if (type1 != type2) {
                    type1 - type2  // 按类型排序
                } else {
                    str1.compareTo(str2)  // 如果类型相同，按字母顺序排序
                }

            }
            .build()
            .find()

        return sortedContacts.toMutableList()
    }

    fun loadFriendList(username: String) {
        viewModelScope.launch {
            val result = contactRepository.getContactList(username)
            _contactListResult.postValue(result)
        }
    }
}
