package com.cheng.blzb.ui.fragment.push.subscribe.add

import androidx.lifecycle.MutableLiveData
import com.cheng.blzb.bean.UserAuthEntity
import com.cheng.blzb.net.ApiFactory
import com.example.base.extensions.toast
import com.example.base.utils.L
import com.example.base.viewmodel.BaseViewModel
import okhttp3.RequestBody

class AddSubscribeViewModel : BaseViewModel() {
    val keywordsLiveData = MutableLiveData<List<String>>()
    val addLiveData = MutableLiveData<Any>()

    fun getKeywords(query: String) {
        launchOnUiTryCatch({
            val params = mutableMapOf<String, String>()
            params["query"] = query
            params["size"] = "9"
            val response = ApiFactory.apiService.getHotKeywords(params)
            if (response.status) {
                keywordsLiveData.postValue(response.data)
            } else toast(response.message, true)
        }, {
            setError(it)
            L.d(it)
        })
    }

    fun addSubscription(requestBody: RequestBody) {
        showDialog()
        launchOnUiTryCatch({
            val response = ApiFactory.apiService.addSubscription(requestBody)
            if (response.status) {
                addLiveData.postValue(Any())
            } else toast(response.message, true)
            dismissDialog()
        }, {
            dismissDialog()
            setError(it)
            L.d(it)
        })
    }

    fun updateSubscription(requestBody: RequestBody) {
        showDialog()
        launchOnUiTryCatch({
            val response = ApiFactory.apiService.updateSubscription(requestBody)
            if (response.status) {
                addLiveData.postValue(Any())
            } else toast(response.message, true)
            dismissDialog()
        }, {
            dismissDialog()
            setError(it)
            L.d(it)
        })
    }

    val authLiveData = MutableLiveData<UserAuthEntity>()

    fun checkAuth() {
        launchOnUiTryCatch({
            val response = ApiFactory.apiService.checkAuth()
            if (response.status) {
                authLiveData.postValue(response.data)
            } else toast(response.message, true)
        }, {
            setError(it)
            L.d(it)
        })
    }
}