package com.gh.gamecenter.gamedetail.history

import android.app.Application
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import com.gh.gamecenter.common.constant.Constants
import com.gh.gamecenter.common.baselist.ListViewModel
import com.gh.gamecenter.common.baselist.LoadType
import com.gh.gamecenter.entity.ApkEntity
import com.gh.gamecenter.entity.GameEntity
import com.gh.gamecenter.common.retrofit.Response
import com.gh.gamecenter.retrofit.RetrofitManager
import com.lightgame.download.DownloadEntity
import io.reactivex.Single
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers
import retrofit2.HttpException

class HistoryApkListViewModel(application: Application, var gameId: String, var game: GameEntity?) :
    ListViewModel<ApkEntity, GameEntity>(application) {

    val showDowngradeHint = MutableLiveData<Boolean>()

    init {
        if (game == null) {
            getGameDigest()
        }
    }

    override fun provideDataObservable(page: Int) = null

    override fun provideDataSingle(page: Int): Single<MutableList<ApkEntity>> {
        return RetrofitManager.getInstance().api.getHistoryApks(gameId, page)
    }

    fun getGameDigest() {
        RetrofitManager.getInstance().api
            .getGameDigest(gameId)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(object : Response<GameEntity>() {
                override fun onResponse(response: GameEntity?) {
                    game = response
                    load(LoadType.NORMAL)
                }

                override fun onFailure(e: HttpException?) {
                    load(LoadType.NORMAL)
                }
            })
    }

    override fun mergeResultLiveData() {
        mResultLiveData.addSource(mListLiveData) {
            val gameList = arrayListOf<GameEntity>()
            for (apk in it) {
                gameList.add(game!!.clone().apply {
                    // 改变 id 避免其它地方根据原有 game id 变更状态
                    this.isReservable = false
                    this.id = game?.id + DownloadEntity.GAME_ID_DIVIDER + apk.version
                    this.name = game?.name + Constants.GAME_NAME_DECORATOR // 历史版本加个空格避免其他用游戏名来判定是否待安装的地方错误判断
                    this.setApk(arrayListOf(apk))
                })
            }
            if (gameList.isNotEmpty()
                && gameList.first().getApk().isNotEmpty()
                && gameList.first().getApk().first().downloadStatus != "off"
            ) {
                showDowngradeHint.postValue(true)
            }
            mResultLiveData.postValue(gameList)
        }
    }

    class Factory(
        private val mApplication: Application,
        private val gameId: String,
        private val game: GameEntity?
    ) : ViewModelProvider.NewInstanceFactory() {
        override fun <T : ViewModel> create(modelClass: Class<T>): T {
            return HistoryApkListViewModel(mApplication, gameId, game) as T
        }
    }

}