package com.gh.gamecenter.savegame

import android.app.Application
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import com.gh.gamecenter.common.baselist.ListViewModel
import com.gh.gamecenter.common.retrofit.Response
import com.gh.gamecenter.common.utils.toRequestBody
import com.gh.gamecenter.entity.ArchiveEntity
import com.gh.gamecenter.feature.entity.GameEntity
import com.gh.gamecenter.retrofit.RetrofitManager
import com.gh.vspace.VHelper
import io.reactivex.Observable
import io.reactivex.Single
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers
import retrofit2.HttpException

class GameArchiveListViewModel(application: Application, val path: String) :
    ListViewModel<GameEntity, GameEntity>(application) {
    override fun mergeResultLiveData() {
        mResultLiveData.addSource(mListLiveData) {
            mResultLiveData.postValue(it)
        }
    }

    private fun getGameConfigUrl(game: GameEntity, archiveConfigs: List<ArchiveEntity>) {
        archiveConfigs.forEach { archiveEntity ->
            //获取对应游戏的config_url
            if (game.id == archiveEntity.gameId) {
                game.configUrl = archiveEntity.configUrl
            }
        }
    }

    override fun provideDataSingle(page: Int): Single<MutableList<GameEntity>> {
        return if (path == "recommend") RetrofitManager.getInstance().api.gameSaveRecommendGameList(page) else Single.create { emitter ->
            val vGameList = VHelper.getAllVGame()
            val gameList = arrayListOf<GameEntity>()
            val gameIds = arrayListOf<String>()
            // 为空直接返回
            if (vGameList.isEmpty()) {
                emitter.onSuccess(gameList)
                return@create
            }
            for (vGame in vGameList) {
                val gameEntity = VHelper.toGameEntity(vGame.downloadEntity)
                gameList.add(0, gameEntity)
                gameIds.add(gameEntity.id)
            }
            val map = mapOf(
                "game_ids" to gameIds
            )
            RetrofitManager.getInstance().newApi.getGamesArchiveConfigs(map.toRequestBody())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(object : Response<List<ArchiveEntity>>() {
                    override fun onResponse(response: List<ArchiveEntity>?) {
                        val itemDataList = arrayListOf<GameEntity>()
                        response.let {
                            gameList.forEach { game ->
                                getGameConfigUrl(game, it!!)
                                if (game.configUrl.isNotEmpty()) {
                                    itemDataList.add(game)
                                }
                            }
                        }
                        emitter.onSuccess(itemDataList)
                    }

                    override fun onFailure(e: HttpException?) {
                        emitter.onSuccess(arrayListOf<GameEntity>())
                        super.onFailure(e)
                    }
                })
        }
    }

    override fun provideDataObservable(page: Int): Observable<MutableList<GameEntity>>? {
        return null
    }

    class Factory(
        private val mApplication: Application,
        private val path: String,
    ) : ViewModelProvider.NewInstanceFactory() {
        override fun <T : ViewModel> create(modelClass: Class<T>): T {
            return GameArchiveListViewModel(mApplication, path) as T
        }
    }
}
