package com.gh.gamecenter.energy.view

import android.annotation.SuppressLint
import android.app.Application
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.MutableLiveData
import com.gh.gamecenter.common.retrofit.BiResponse
import com.gh.gamecenter.energy.entity.CommodityCategoryEntity
import com.gh.gamecenter.energy.retrofit.RetrofitManager
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers

class EnergyCenterViewModel(application: Application) : AndroidViewModel(application) {

    private val api = RetrofitManager.getInstance().api
    var categories = MutableLiveData<List<CommodityCategoryEntity>>()


    init {
        getCommodityCategories()
    }

    @SuppressLint("CheckResult")
    fun getCommodityCategories() {
        api.getCommodityCategories()
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(object : BiResponse<List<CommodityCategoryEntity>>() {
                override fun onSuccess(data: List<CommodityCategoryEntity>) {
                    categories.postValue(data)
                }
            })
    }
}