package com.halo.assistant.fragment.user.avatar

import android.app.Application
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.MutableLiveData
import com.gh.gamecenter.common.retrofit.Response
import com.gh.gamecenter.common.utils.observableToMain
import com.gh.gamecenter.core.utils.ToastUtils
import com.gh.gamecenter.entity.DefaultAvatar
import com.gh.gamecenter.retrofit.RetrofitManager
import retrofit2.HttpException

class ChooseDefaultAvatarViewModel(application: Application) : AndroidViewModel(application) {
    private val mApi = RetrofitManager.getInstance().api
    var defaultAvatar: DefaultAvatar? = null

    val avatarListLivedata = MutableLiveData<List<DefaultAvatar>?>()

    init {
        getDefaultAvatars()
    }

    fun getDefaultAvatars() {
        mApi.defaultAvatars.compose(observableToMain())
            .subscribe(object : Response<List<DefaultAvatar>>() {
                override fun onResponse(response: List<DefaultAvatar>?) {
                    super.onResponse(response)
                    avatarListLivedata.postValue(response)
                }

                override fun onFailure(e: HttpException?) {
                    super.onFailure(e)
                    ToastUtils.showToast("加载失败")
                }
            })

    }
}