package com.abbidot.tracker.deprecated.ui.activity.vm

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.abbidot.baselibrary.constant.MMKVKey
import com.abbidot.baselibrary.util.MMKVUtil
import com.abbidot.tracker.base.BaseActivity
import com.abbidot.tracker.bean.CommonDataBean
import com.abbidot.tracker.bean.MessageBean
import com.abbidot.tracker.retrofit2.NetworkApi
import kotlinx.coroutines.launch

/**
 *Created  by .yzq on 2022/2/24/024.
 * @link
 * @description:
 */
class NotificationsViewModel : ViewModel() {

    val mNotificationsLiveData = MutableLiveData<Result<MutableList<MessageBean>>>()
    val mDeleteMessageLiveData = MutableLiveData<Result<String>>()
    val mReadMessageLiveData = MutableLiveData<Result<String>>()
    val mReadAllMessageLiveData = MutableLiveData<Result<String>>()
    val mMessageCountLiveData = MutableLiveData<Result<CommonDataBean>>()

    fun getMessageList() {
//        activity.showLoading(true)
        viewModelScope.launch {
            val userId = MMKVUtil.getString(MMKVKey.UserId)
            val result = NetworkApi.getMessageList(userId)
            mNotificationsLiveData.value = result
        }
    }

    fun getMessageUnReadCount() {
        viewModelScope.launch {
            val userId = MMKVUtil.getString(MMKVKey.UserId)
            val result = NetworkApi.getMessageUnReadCount(userId)
            mMessageCountLiveData.value = result
        }
    }

    fun messageListByPage(page: Int) {
        viewModelScope.launch {
            val userId = MMKVUtil.getString(MMKVKey.UserId)
            val result = NetworkApi.messageListByPage(userId, page)
            mNotificationsLiveData.value = result
        }
    }

    fun setMessageIsRead(messageId: String) {
        viewModelScope.launch {
            val result = NetworkApi.setMessageIsRead(messageId)
            mReadMessageLiveData.value = result
        }
    }

    fun setMessageAllIsRead() {
        viewModelScope.launch {
            val userId = MMKVUtil.getString(MMKVKey.UserId)
            val result = NetworkApi.setMessageAllIsRead(userId)
            mReadAllMessageLiveData.value = result
        }
    }

    fun deleteMessage(activity: BaseActivity<*>, messageIds: String) {
        activity.showLoading(true)
        viewModelScope.launch {
            val result = NetworkApi.deleteMessage(messageIds)
            mDeleteMessageLiveData.value = result
        }
    }
}