package com.gh.vspace

import android.app.Application
import androidx.annotation.WorkerThread
import com.gh.download.DownloadManager
import com.gh.download.PackageObserver
import com.gh.gamecenter.common.baselist.ListViewModel
import com.gh.gamecenter.common.utils.toProperReadableSize
import com.gh.gamecenter.core.runOnUiThread
import com.gh.gamecenter.core.utils.NumberUtils
import com.gh.gamecenter.core.utils.ToastUtils
import com.gh.gamecenter.entity.GameEntity
import com.gh.gamecenter.eventbus.EBPackage
import com.lightgame.download.DownloadStatus
import io.reactivex.Observable
import io.reactivex.Single
import org.greenrobot.eventbus.EventBus

class VDownloadManagerViewModel(application: Application) :
    ListViewModel<GameEntity, GameEntity>(application) {

    var type = ""
    var isFromHomeRecent = false

    override fun mergeResultLiveData() {
        mResultLiveData.addSource(mListLiveData) {
            mResultLiveData.postValue(it)
        }
    }

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

    override fun provideDataSingle(page: Int): Single<MutableList<GameEntity>> {
        return if (type == TYPE_DOWNLOADED) {
            provideDownloadedSingle()
        } else {
            provideDownloadingSingle()
        }
    }

    private fun provideDownloadedSingle(): Single<MutableList<GameEntity>> {
        return Single.create { emitter ->
            val vGameList = VHelper.getAllVGame().sortedByDescending { it.downloadEntity.start }
            val gameList = arrayListOf<GameEntity>()

            // 为空直接返回
            if (vGameList.isEmpty()) {
                emitter.onSuccess(gameList)
                return@create
            }

            for (vGame in vGameList) {
                // 过滤部分不需要的
                if (DownloadManager.getInstance().allVDownloadTaskSnapshots.any {
                        it.gameId == vGame.downloadEntity.gameId && it.status != DownloadStatus.done
                    }) {
                    continue
                }

                val gameEntity = VHelper.toGameEntity(vGame.downloadEntity)
                if (gameEntity.playedTime != 0L) {
                    gameEntity.des =
                        "已畅玩${NumberUtils.transSimpleUsageTime(gameEntity.playedTime / 1000)}"
                } else {
                    var occupiedSpace =
                        VHelper.getAppOccupiedSpace(vGame.downloadEntity.packageName)
                    if (occupiedSpace == 0L) {
                        occupiedSpace = vGame.downloadEntity.size
                    }

                    gameEntity.des = "已占用 ${occupiedSpace.toProperReadableSize()}"
                }

                gameList.add(gameEntity)
            }

            emitter.onSuccess(gameList)
        }
    }

    private fun provideDownloadingSingle(): Single<MutableList<GameEntity>> {
        return Single.create { emitter ->
            val downloadList = DownloadManager.getInstance().allVDownloadTaskSnapshots

            val gameList = arrayListOf<GameEntity>()

            for (downloadEntity in downloadList) {
                // 过滤下载完成的部分
                if (downloadEntity.status == DownloadStatus.done && VHelper.isInstalled(downloadEntity.packageName)) {
                    continue
                }
                gameList.add(VHelper.toGameEntity(downloadEntity))
            }

            emitter.onSuccess(gameList)
        }
    }

    @WorkerThread
    fun removeItem(url: String?, packageName: String?) {
        DownloadManager.getInstance().pause(url)
        DownloadManager.getInstance().cancel(url)

        VHelper.uninstall(packageName ?: "")

        runOnUiThread {
            val event = EBPackage("卸载", packageName ?: "", "unknown")
            EventBus.getDefault().post(event)
            PackageObserver.onPackageChanged(event)

            ToastUtils.toast("已删除 1 款游戏")
        }
    }

    @WorkerThread
    fun removeItems(idList: ArrayList<String>) {
        val size = idList.size.coerceAtMost(99)
        val packageList = arrayListOf<String>()

        for (id in idList) {
            val apkEntity =
                mResultLiveData.value?.firstOrNull { id == it.id }?.getApk()?.firstOrNull()
            DownloadManager.getInstance().pause(apkEntity?.url)
            DownloadManager.getInstance().cancel(apkEntity?.url)
            packageList.add(apkEntity?.packageName ?: "")

            VHelper.uninstall(apkEntity?.packageName)
        }

        runOnUiThread {
            for (packageName in packageList) {
                val event = EBPackage("卸载", packageName, "unknown")
                EventBus.getDefault().post(event)
                PackageObserver.onPackageChanged(event)
            }

            ToastUtils.toast("已删除 $size 款游戏")
        }
    }

    fun pauseItems(idList: ArrayList<String>) {
        for (id in idList) {
            val apkEntity =
                mResultLiveData.value?.firstOrNull { id == it.id }?.getApk()?.firstOrNull()
            DownloadManager.getInstance().pause(apkEntity?.url)
        }
    }

    fun isTypeDownloaded(): Boolean {
        return type == TYPE_DOWNLOADED
    }

    companion object {
        const val TYPE = "type"
        const val TYPE_DOWNLOADED = "type_downloaded"
        const val TYPE_DOWNLOADING = "type_downloading"
    }

}