package com.gh.gamecenter.amway.search

import android.annotation.SuppressLint
import android.app.Application
import android.text.TextUtils
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.MutableLiveData
import com.gh.common.constant.Config
import com.gh.common.util.PackageHelper
import com.gh.gamecenter.BuildConfig
import com.gh.gamecenter.common.baselist.LoadStatus
import com.gh.gamecenter.common.retrofit.BiResponse
import com.gh.gamecenter.common.retrofit.Response
import com.gh.gamecenter.feature.entity.GameEntity
import com.gh.gamecenter.login.user.UserManager
import com.gh.gamecenter.retrofit.RetrofitManager
import com.halo.assistant.HaloApp
import com.lightgame.utils.Utils
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers
import retrofit2.HttpException

class AmwaySearchViewModel(application: Application) : AndroidViewModel(application) {

    private var mTempSearchKey = ""
    var currentSearchKey = ""

    var loadStatus = MutableLiveData<LoadStatus>()

    var playedGames = MutableLiveData<List<GameEntity>>()
    var searchGames = MutableLiveData<List<GameEntity>>()

    init {
        getPlayedGames()
    }

    fun retrySearch() {
        if (mTempSearchKey.isNotEmpty()) {
            getSearchResult(mTempSearchKey)
        }
    }

    fun getSearchResult(searchKey: String) {
        mTempSearchKey = searchKey
        currentSearchKey = searchKey
        loadStatus.postValue(LoadStatus.INIT_LOADING)
        searchGames.postValue(emptyList())
        RetrofitManager
            .getInstance().api
            .getSearchGame(Config.API_HOST + "games:search?keyword=" + searchKey + "&view=anliwall" + "&channel=" + HaloApp.getInstance().channel + "&version=" + BuildConfig.VERSION_NAME)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(object : Response<List<GameEntity>>() {
                override fun onResponse(response: List<GameEntity>?) {
                    super.onResponse(response)
                    mTempSearchKey = ""
                    if (response != null && response.isNotEmpty()) {
                        loadStatus.postValue(LoadStatus.INIT_LOADED)
                    } else {
                        loadStatus.postValue(LoadStatus.INIT_EMPTY)
                    }
                    searchGames.postValue(response)
                }

                override fun onFailure(e: HttpException?) {
                    super.onFailure(e)
                    loadStatus.postValue(LoadStatus.INIT_FAILED)
                }
            })
    }

    @SuppressLint("CheckResult")
    fun getPlayedGames() {
        RetrofitManager.getInstance()
            .api
            .getPlayedGames(UserManager.getInstance().userId, 1, Utils.getTime(getApplication()), mapOf())
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(object : BiResponse<List<GameEntity>>() {
                override fun onSuccess(data: List<GameEntity>) {
                    val sortedList = data.sortedBy { !it.showComment }
                    playedGames.postValue(sortedList.take(5))
                }
            })
    }

    fun canUserCommentThisGame(gameEntity: GameEntity): String? {
        val packageNameList = ArrayList<String>()

        for (apk in gameEntity.getApk()) {
            val packageName = apk.packageName
            if (!TextUtils.isEmpty(packageName)
                // 过滤黑名单包名
                && !PackageHelper.commentPackageNameBlackList.contains(packageName)
                && !PackageHelper.downloadPackageNameBlackList.contains(packageName)
            ) {
                packageNameList.add(packageName)
            }
        }

        for (packageName in packageNameList) {
            if (PackageHelper.localPackageNameSet.contains(packageName)) {
                return packageName
            }
        }

        return null
    }

}