package com.gh.gamecenter.simulatorgame

import android.annotation.SuppressLint
import android.app.Application
import android.content.pm.PackageManager
import com.gh.common.constant.Config
import com.gh.common.simulator.SimulatorGameManager
import com.gh.common.util.PackageHelper
import com.gh.gamecenter.common.utils.NetworkUtils
import com.gh.gamecenter.common.baselist.ListViewModel
import com.gh.gamecenter.feature.entity.ApkEntity
import com.gh.gamecenter.feature.entity.SimulatorEntity
import com.gh.gamecenter.retrofit.RetrofitManager
import com.halo.assistant.HaloApp
import io.reactivex.Observable
import io.reactivex.Single

class SimulatorManagementViewModel(application: Application) :
    ListViewModel<SimulatorEntity, SimulatorEntity>(application) {

    private val mApi = RetrofitManager.getInstance().api

    override fun mergeResultLiveData() {
        mResultLiveData.addSource<List<SimulatorEntity>>(mListLiveData) {
            val newList = ArrayList<SimulatorEntity>()
            it.forEach { simulator ->
                if (simulator.active) newList.add(simulator)
            }
            mResultLiveData.postValue(newList)
        }
    }

    override fun provideDataObservable(page: Int): Observable<MutableList<SimulatorEntity>>? = null

    override fun provideDataSingle(page: Int): Single<List<SimulatorEntity>> {
        val isNetworkConnected = NetworkUtils.isNetworkConnected(HaloApp.getInstance())
        val isInstalledOldSimulator = SimulatorGameManager.isOldSimulatorInstalled(HaloApp.getInstance())//有没有安装旧版本模拟器
        val newSimulator = Config.getNewSimulatorEntitySetting()
        var newSimulatorActive = false
        if (newSimulator != null) {
            newSimulatorActive = newSimulator.active
        }
        return if (isNetworkConnected) {
            if (isInstalledOldSimulator || !newSimulatorActive) {
                mApi.simulators
            } else {
                getNewsSimulator()
            }
        } else {
            getAllSimulator()
        }
    }

    @SuppressLint("CheckResult")
    fun getAllSimulator(): Single<List<SimulatorEntity>> {
        return Single.create<List<SimulatorEntity>> { emitter ->
            val simulatorEntityList = ArrayList<SimulatorEntity>()
            val allInstalledPackages =
                PackageHelper.getInstalledPackageInfoList(HaloApp.getInstance().application, PackageManager.GET_ACTIVITIES)
            allInstalledPackages.forEach {
                if (it.packageName.contains("com.gh")) {
                    val activityInfo =
                        it.activities.find { activity -> activity.name.contains("RequestPermissionActivity") }
                    if (activityInfo != null) {
                        val appName =
                            it.applicationInfo.loadLabel(HaloApp.getInstance().application.packageManager).toString()
                        val apk = ApkEntity(packageName = it.packageName, version = it.versionName)
                        simulatorEntityList.add(SimulatorEntity(name = appName, apk = apk, active = true))
                    }
                }
            }
            emitter.onSuccess(simulatorEntityList)
        }

    }

    //获取新模拟器
    private fun getNewsSimulator(): Single<List<SimulatorEntity>> {
        return Single.create { emitter ->
            val newSimulatorList = ArrayList<SimulatorEntity>()
            val simulator = Config.getNewSimulatorEntitySetting()
            if (simulator != null) {
                newSimulatorList.add(simulator)
            }
            emitter.onSuccess(newSimulatorList)
        }
    }

}