package com.gh.gamecenter.gamecollection.choose

import android.annotation.SuppressLint
import android.app.Application
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import com.gh.gamecenter.common.utils.singleToMain
import com.gh.gamecenter.feature.entity.GameEntity
import com.gh.gamecenter.mygame.PlayedGameViewModel
import com.gh.gamecenter.retrofit.RetrofitManager
import com.halo.assistant.HaloApp
import com.lightgame.utils.Utils
import io.reactivex.Single

class AddUserPlayedGameViewModel(application: Application, userId: String, isKeepTagStyle: Boolean = false) :
    PlayedGameViewModel(application, userId, isKeepTagStyle) {
    private val mApi = RetrofitManager.getInstance().api
    val addAllPlayedGamesLiveData = MutableLiveData<List<GameEntity>?>()

    override fun provideDataSingle(page: Int): Single<MutableList<GameEntity>> {
        val map = mapOf("view" to "game_list")
        return mApi.getPlayedGames(userId, page, Utils.getTime(getApplication()), map)
    }

    @SuppressLint("CheckResult")
    fun addAllPlayedGames() {
        val map = mapOf("view" to "game_list", "page_size" to ADD_GAMES_MAX_SIZE)
        mApi.getPlayedGames(userId, 1, Utils.getTime(getApplication()), map)
            .compose(singleToMain())
            .subscribe({
                addAllPlayedGamesLiveData.postValue(it)
            }, {
                Utils.toast(getApplication(), "添加失败")
            })
    }

    class Factory(private val mUserId: String, val isKeepTagStyle: Boolean = false) :
        ViewModelProvider.NewInstanceFactory() {
        override fun <T : ViewModel> create(modelClass: Class<T>): T {
            return AddUserPlayedGameViewModel(HaloApp.getInstance().application, mUserId, isKeepTagStyle) as T
        }
    }

    companion object {
        const val ADD_GAMES_MAX_SIZE = 100
    }

}