package com.gh.gamecenter.game.commoncollection.detail

import android.app.Application
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import com.gh.gamecenter.common.baselist.ListViewModel
import com.gh.gamecenter.entity.CommonCollectionContentEntity
import com.gh.gamecenter.entity.CommonCollectionEntity
import com.gh.gamecenter.retrofit.RetrofitManager
import com.halo.assistant.HaloApp
import io.reactivex.Observable

class CommonCollectionDetailViewModel(
    application: Application,
    private val mCollectionId: String
) : ListViewModel<CommonCollectionContentEntity, CommonCollectionContentEntity>(application) {

    val commonCollectionLiveData = MutableLiveData<CommonCollectionEntity>()

    override fun provideDataObservable(page: Int): Observable<MutableList<CommonCollectionContentEntity>>? {
        return if (page == 1) {
            RetrofitManager.getInstance().api
                .getCommonCollectionDetail(mCollectionId).map {
                    commonCollectionLiveData.postValue(it)
                    it.collectionList
                }
        } else null
    }

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

    class Factory(private val collectionId: String) : ViewModelProvider.NewInstanceFactory() {

        override fun <T : ViewModel> create(modelClass: Class<T>): T {
            return CommonCollectionDetailViewModel(HaloApp.getInstance().application, collectionId) as T
        }
    }
}