package com.gh.gamecenter.minigame

import android.annotation.SuppressLint
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import com.gh.gamecenter.common.constant.Constants
import com.gh.gamecenter.common.retrofit.BiResponse
import com.gh.gamecenter.feature.entity.GameEntity
import com.gh.gamecenter.minigame.qq.QGameViewModel
import com.gh.gamecenter.retrofit.RetrofitManager
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers
import okhttp3.ResponseBody

/**
 * 小游戏-最近在玩-网域层
 */
object MiniGameRecentlyPlayUseCase {

    private val repository = MiniGameRecentlyPlayRepository(RetrofitManager.getInstance().newApi)

    /**
     * 微信小游戏-最近在玩
     */
    private val _wechatRecentGamesItemLiveData = MutableLiveData<List<GameEntity>>()
    val wechatRecentGamesItemLiveData: LiveData<List<GameEntity>> = _wechatRecentGamesItemLiveData

    /**
     * QQ小游戏-最近在玩
     */
    private val _qqRecentGamesItemLiveData = MutableLiveData<List<GameEntity>>()
    val qqRecentGamesItemLiveData: LiveData<List<GameEntity>> = _qqRecentGamesItemLiveData

    @SuppressLint("CheckResult")
    fun loadRecentlyPlayedMiniGameList(gameType: String) {
        repository.getRecentPlayedMiniGameList(gameType)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(object : BiResponse<List<GameEntity>>() {
                override fun onSuccess(data: List<GameEntity>) {
                    if (gameType == Constants.QQ_MINI_GAME) {
                        _qqRecentGamesItemLiveData.value = data
                    } else {
                        _wechatRecentGamesItemLiveData.value = data
                    }
                }

                override fun onFailure(exception: Exception) {
                    super.onFailure(exception)
                    if (gameType == Constants.QQ_MINI_GAME) {
                        _qqRecentGamesItemLiveData.value = emptyList()
                    } else {
                        _wechatRecentGamesItemLiveData.value = emptyList()
                    }
                }
            })
    }

    @SuppressLint("CheckResult")
    fun submitRecentPlayedWGame(wechatAppId: String, gid: String) {
        repository.postRecentPlayedWGame(wechatAppId, gid)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(object : BiResponse<ResponseBody>() {
                override fun onSuccess(data: ResponseBody) {
                    loadRecentlyPlayedMiniGameList(Constants.WECHAT_MINI_GAME)
                }
            })
    }

    @SuppressLint("CheckResult")
    fun submitRecentPlayedQGame(qqAppId: String, userId: String) {
        repository.postRecentPlayedQGame(qqAppId, userId)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(object : BiResponse<ResponseBody>() {
                override fun onSuccess(data: ResponseBody) {
                    loadRecentlyPlayedMiniGameList(Constants.QQ_MINI_GAME)
                    QGameViewModel.notifyQGameSubjectUpdate() // 通知QQ小游戏首页列表刷新
                }
            })
    }

    /**
     * 获取小游戏-最近在玩游戏列表缓存
     * @param gameType 游戏类型（qq/wechat）
     */
    fun getRecentlyPlayedMiniGameList(gameType: String): List<GameEntity> =
        if (gameType == Constants.QQ_MINI_GAME) {
            _qqRecentGamesItemLiveData.value
        } else {
            _wechatRecentGamesItemLiveData.value
        } ?: let {
            loadRecentlyPlayedMiniGameList(gameType)// 最近在玩数据为空时，则调用接口获取数据
            emptyList()
        }

    fun clearRecentlyPlayedMiniGameList(gameType: String = "") {
        when (gameType) {
            Constants.QQ_MINI_GAME -> {
                _qqRecentGamesItemLiveData.value = emptyList()
            }

            Constants.WECHAT_MINI_GAME ->
                _wechatRecentGamesItemLiveData.value = emptyList()

            else -> {
                _qqRecentGamesItemLiveData.value = emptyList()
                _wechatRecentGamesItemLiveData.value = emptyList()
            }
        }
    }
}