package com.ccwangluo.accelerator.ui.home

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.ccwangluo.accelerator.model.*
import com.ccwangluo.accelerator.utils.http.HttpGo
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import javax.security.auth.callback.Callback

class NewsViewModel : ViewModel() {

    val isRefresh = MutableLiveData<Boolean>()


    val notice = MutableLiveData<Notice>()

    val pluginList = MutableLiveData<List<Plugin>>()

    fun getNotice(gameId: Int) {
        viewModelScope.launch(Dispatchers.IO) {
            val res =
                HttpGo.getSync<NoticeList>("/api/announcement/list?gameId=$gameId&page=1&size=1")
            res?.let {
                if (it.code == 200) {
                    it.data?.list?.let {
                        if(it.size > 0) {
                            notice.postValue(it.get(0))
                        }
                    }
                }
            }
        }
    }

    fun getPlugin(gameId: Int) {
        viewModelScope.launch(Dispatchers.IO) {
            val res =
                HttpGo.getSync<PluginModel>("/api/plugin/list?gameId=$gameId&page=1&size=10")
            res?.let {
                if (it.code == 200) {
                    it.data?.list?.let {
                        pluginList.postValue(it)
                    }
                }
            }
        }
    }
}