package com.gh.gamecenter.forum.home

import android.annotation.SuppressLint
import android.app.Application
import androidx.lifecycle.MutableLiveData
import com.gh.gamecenter.common.utils.singleToMain
import com.gh.gamecenter.common.baselist.ListViewModel
import com.gh.gamecenter.entity.ForumActivityCategoryEntity
import com.gh.gamecenter.entity.ForumActivityEntity
import com.gh.gamecenter.common.retrofit.BiResponse
import com.gh.gamecenter.retrofit.RetrofitManager
import io.reactivex.Single

class ForumActivityViewModel(application: Application) :
    ListViewModel<ForumActivityEntity, ForumActivityEntity>(application) {

    var categoryId = ""
    var categories = MutableLiveData<List<ForumActivityCategoryEntity>>()

    init {
        getActivityCategories()
    }

    @SuppressLint("CheckResult")
    fun getActivityCategories() {
        RetrofitManager.getInstance()
            .api
            .forumActivityCategories
            .compose(singleToMain())
            .subscribe(object : BiResponse<List<ForumActivityCategoryEntity>>() {
                override fun onSuccess(data: List<ForumActivityCategoryEntity>) {
                    categories.postValue(data)
                }
            })
    }

    override fun provideDataObservable(page: Int) = null

    override fun provideDataSingle(page: Int): Single<MutableList<ForumActivityEntity>> {
        return RetrofitManager.getInstance()
            .api.getForumActivities(categoryId, page)
    }

    override fun mergeResultLiveData() {
        mResultLiveData.addSource(mListLiveData) { mResultLiveData.postValue(it) }
    }

}