package com.gh.gamecenter.gamedetail.libao

import android.annotation.SuppressLint
import android.app.Application
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import com.gh.common.util.LibaoUtils
import com.gh.gamecenter.common.baselist.ListViewModel
import com.gh.gamecenter.common.utils.observableToMain
import com.gh.gamecenter.common.utils.singleToMain
import com.gh.gamecenter.core.utils.UrlFilterUtils
import com.gh.gamecenter.feature.entity.GameEntity
import com.gh.gamecenter.feature.entity.LibaoEntity
import com.gh.gamecenter.gamedetail.GameDetailViewModel
import com.gh.gamecenter.retrofit.RetrofitManager
import com.halo.assistant.HaloApp
import com.lightgame.utils.Utils
import io.reactivex.Observable
import io.reactivex.Single

class LibaoListViewModel(application: Application, private val game: GameEntity?) :
    ListViewModel<LibaoEntity, LibaoEntity>(application) {
    private val api = RetrofitManager.getInstance().api
    override fun provideDataObservable(page: Int): Observable<MutableList<LibaoEntity>>? = null

    @SuppressLint("CheckResult")
    override fun provideDataSingle(page: Int): Single<MutableList<LibaoEntity>> {
        return Single.create { emitter ->
            if (page == 1) {
                api.getGameLibaoList(game?.id)
                    .compose(singleToMain())
                    .subscribe({ libaoList ->
                        if (libaoList.isEmpty()) {
                            emitter.onSuccess(libaoList)
                        } else {
                            val simpleGame = game?.toSimpleGame()
                            libaoList.forEach {
                                if (it.game == null) {
                                    it.game = simpleGame
                                }
                            }

                            val builder = StringBuilder()
                            var i = 0
                            val size = libaoList.size
                            while (i < size) {
                                builder.append(libaoList[i].id)
                                builder.append("-")
                                i++
                            }
                            builder.deleteCharAt(builder.length - 1)
                            val ids = builder.toString()
                            api.getLibaoStatus(UrlFilterUtils.getFilterQuery("libao_ids", ids))
                                .compose(observableToMain())
                                .subscribe({
                                    LibaoUtils.initLiBaoEntity(it, libaoList)
                                    GameDetailViewModel.sortLibaoList(libaoList)
                                    emitter.onSuccess(libaoList)
                                }, {
                                    Utils.toast(getApplication(), "获取礼包状态失败")
                                    emitter.onSuccess(libaoList)
                                })
                        }
                    }, {
                        emitter.onError(it)
                    })
            } else {
                emitter.onSuccess(arrayListOf())
            }
        }
    }

    override fun mergeResultLiveData() {
        mResultLiveData.addSource(mListLiveData) { list ->
            mResultLiveData.postValue(list)
        }
    }

    class Factory(private val game: GameEntity?) : ViewModelProvider.NewInstanceFactory() {
        override fun <T : ViewModel> create(modelClass: Class<T>): T {
            return LibaoListViewModel(HaloApp.getInstance().application, game) as T
        }
    }
}