package com.gh.gamecenter.subject

import android.app.Application
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import com.gh.gamecenter.common.baselist.LoadParams
import com.gh.gamecenter.common.exposure.ExposureSource
import com.gh.gamecenter.entity.SubjectData
import com.gh.gamecenter.feature.entity.GameEntity
import com.gh.gamecenter.feature.entity.PageLocation
import com.gh.gamecenter.retrofit.RetrofitManager
import com.halo.assistant.HaloApp
import io.reactivex.Single

class AdSubjectListViewModel(
    application: Application,
    private val mSubjectData: SubjectData,
    pageLocation: PageLocation,
    exposureSourceList: List<ExposureSource>? = null
) :
    SubjectListViewModel(application, mSubjectData, pageLocation, exposureSourceList, null) {
    override fun provideDataSingle(page: Int): Single<MutableList<GameEntity>> =
        RetrofitManager.getInstance().newApi.getAdGames(
            mSubjectData.adId,
            mapOf("page" to "$page,${LoadParams.DEFAULT_PAGE_SIZE}", "code_id" to mSubjectData.codeId)
        )

    override fun mergeResultLiveData() {
        mResultLiveData.addSource(mListLiveData) {
            it.forEach { game ->
                game.isAdData = mSubjectData.isAdData
            }
            mResultLiveData.postValue(it)
        }
    }

    class Factory(
        private val mSubjectData: SubjectData,
        private val mPageLocation: PageLocation,
        private val mExposureSourceList: List<ExposureSource>? = null
    ) : ViewModelProvider.NewInstanceFactory() {
        override fun <T : ViewModel> create(modelClass: Class<T>): T {
            return AdSubjectListViewModel(HaloApp.getInstance(), mSubjectData, mPageLocation, mExposureSourceList) as T
        }
    }
}