package com.gh.gamecenter.forum.home

import android.app.Application
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.MediatorLiveData
import com.gh.gamecenter.common.utils.observableToMain
import com.gh.gamecenter.common.utils.toRequestBody
import com.gh.gamecenter.entity.ForumEntity
import com.gh.gamecenter.common.entity.LinkEntity
import com.gh.gamecenter.manager.PackagesManager
import com.gh.gamecenter.login.user.UserManager
import com.gh.gamecenter.common.retrofit.Response
import com.gh.gamecenter.retrofit.RetrofitManager
import io.reactivex.Observable
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.functions.BiFunction
import io.reactivex.schedulers.Schedulers
import retrofit2.HttpException

class ForumHomeViewModel(application: Application) : AndroidViewModel(application) {
    val slideLiveData = MediatorLiveData<List<LinkEntity>>()
    val forumData = MediatorLiveData<List<ForumEntity>>()

    fun loadSlide() {
        RetrofitManager.getInstance().api
            .forumSlide
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(object : Response<List<LinkEntity>>() {
                override fun onResponse(response: List<LinkEntity>?) {
                    super.onResponse(response)
                    slideLiveData.postValue(response)
                }

                override fun onFailure(e: HttpException?) {
                    super.onFailure(e)
                    slideLiveData.postValue(null)
                }
            })
    }

    fun loadForums() {
        Observable.zip(
            loadFollowsForum(),
            getRecommendForum(),
            BiFunction<List<ForumEntity>, List<ForumEntity>, ArrayList<ForumEntity>> { t1, t2 ->
                val list = arrayListOf<ForumEntity>()
                list.addAll(t1)
                t2.forEach {
                    it.isRecommend = true
                    list.add(it)
                }
                list
            }).compose(observableToMain())
            .subscribe(object : Response<List<ForumEntity>>() {
                override fun onResponse(response: List<ForumEntity>?) {
                    super.onResponse(response)
                    forumData.postValue(response)
                }

                override fun onFailure(e: HttpException?) {
                    super.onFailure(e)
                    forumData.postValue(null)
                }
            })

    }


    private fun loadFollowsForum(): Observable<List<ForumEntity>> {
        return if (UserManager.getInstance().isLoggedIn) {
            RetrofitManager.getInstance().api
                .getFollowsForum(UserManager.getInstance().userId)
        } else {
            Observable.create<List<ForumEntity>> {
                it.onNext(listOf())
            }
        }
    }

    private fun getRecommendForum(): Observable<List<ForumEntity>> {
        val installedList =
            PackagesManager.filterSameApk(PackagesManager.filterDownloadBlackPackage(PackagesManager.getInstalledList()))
        val gameIds = installedList.map { it.id }.toList()
        val requestMap = hashMapOf<String, Any>()
        requestMap["game_ids"] = gameIds
        return if (UserManager.getInstance().isLoggedIn) {
            RetrofitManager.getInstance().api
                .getRecommendForum(UserManager.getInstance().userId, requestMap.toRequestBody())
        } else {
            RetrofitManager.getInstance().api.getRecommendForumNotLoggedIn(requestMap.toRequestBody())
        }

    }
}
