package com.gh.gamecenter.manager

import android.text.TextUtils
import com.gh.common.constant.Config
import com.gh.common.util.PackageUtils
import com.gh.gamecenter.entity.GameInstall
import com.gh.gamecenter.entity.GameUpdateEntity
import java.util.*
import kotlin.collections.ArrayList

/**
 * todo 整理部分与[PackageUtils]冲突的方法
 */
object PackagesManager {

    // 存在网络延迟
    private var mUpdateAndPluginList = ArrayList<GameUpdateEntity>()
    private val mInstalledList = ArrayList<GameInstall>()

    // 实时更新(已安装到本地的包名列表，不包括畅玩游戏)
    private val mInstalledPkgList = Collections.synchronizedList(ArrayList<String>())

    fun initInstallPkgList(list: List<String>) {
        mInstalledPkgList.clear()
        mInstalledPkgList.addAll(list)
    }

    /**
     * 初始化更新列表
     *
     * @param list 更新列表
     */
    fun initGameUpdate(list: ArrayList<GameUpdateEntity>) {
        mUpdateAndPluginList = list
    }

    /**
     *
     * 初始化已安装列表
     *
     * @param list 已安装列表数据
     */
    fun initGameInstall(list: ArrayList<GameInstall>) {
        mInstalledList.clear()
        mInstalledList.addAll(list)
    }

    /**
     * 获取已安装游戏的包名list
     *
     * @return
     */
    @JvmStatic
    fun getInstalledList(): ArrayList<GameInstall> {
        return mInstalledList
    }

    /**
     * 获取过滤同包名已安装游戏的包名list
     *
     * @return
     */
    @JvmStatic
    fun getFilterSameApkInstalledList(): ArrayList<GameInstall> {
        return filterSameApk(mInstalledList)
    }

    /**
     * 获取可更新游戏列表
     *
     * @return 可更新游戏列表
     */
    fun getUpdateList(): ArrayList<GameUpdateEntity> {
        return mUpdateAndPluginList
    }

    /**
     * 根据包名 判断是否安装该游戏 (与 PackageUtils 的是否已安装判断不一样，这里仅在该包名应用被后台收录且安装是返回 true)
     *
     * @param packageName 包名
     * @return true 已安装 false 未安装
     */
    @JvmStatic
    fun isInstalled(packageName: String?): Boolean {
        if (TextUtils.isEmpty(packageName)) {
            return false
        }

        return mInstalledPkgList.contains(packageName)
    }

    /**
     * 根据包名版本号判断是否已安装相应版本的应用
     *
     * @param packageName 包名
     * @param versionName 版本号
     * @return true 已安装 false 未安装
     */
    @JvmStatic
    fun isInstalledWithSpecificVersion(packageName: String?, versionName: String): Boolean {
        if (TextUtils.isEmpty(packageName)) {
            return false
        }

        return if (isInstalled(packageName)) {
            val installData = getInstalledData(packageName)
            installData?.version == versionName
        } else {
            false
        }
    }

    /**
     * 根据包名 获取已安装的信息
     *
     * @param packageName 包名
     * @return 如果为空：未安装
     */
    @JvmStatic
    fun getInstalledData(packageName: String?): GameInstall? {
        if (TextUtils.isEmpty(packageName)) {
            return null
        }
        for (gameInstall in mInstalledList) {
            if (gameInstall.packageName == packageName) {
                return gameInstall
            }
        }
        return null
    }


    /**
     * 判断包名是否可以更新
     *
     * @param packageName 包名
     * @return true 可以更新 false 不可以更新
     */
    fun isCanUpdate(gameId: String?, packageName: String?): Boolean {
        for (entity in mUpdateAndPluginList) {
            if (!entity.isPluggable && entity.packageName == packageName && entity.id == gameId) {
                return true
            }
        }
        return false
    }

    /**
     * 判断包名是否可以插件化
     */
    fun isCanPluggable(gameId: String?, packageName: String?): Boolean {
        if (gameId.isNullOrEmpty() || packageName.isNullOrEmpty()) return false
        for (entity in mUpdateAndPluginList) {
            if (entity.isPluggable && gameId == entity.id && packageName == entity.packageName) {
                return true
            }
        }
        return false
    }

    /**
     * 过滤同包名游戏,取最后一个
     * @return
     */
    @JvmStatic
    fun filterSameApk(installedList: List<GameInstall>?): ArrayList<GameInstall> {
        val map = HashMap<String, GameInstall>()
        if (installedList != null) {
            for (gameInstall in installedList) {
                map[gameInstall.packageName] = gameInstall
            }
        }
        return ArrayList(map.values)
    }

    @JvmStatic
    fun filterDownloadBlackPackage(installedList: MutableList<GameInstall>?): List<GameInstall>? {
        if (installedList == null) return ArrayList()
        val blackList = Config.getSettings()?.gameDownloadBlackList ?: return installedList

        var i = 0
        while (i < installedList.size) {
            val data = installedList[i]
            if (blackList.contains(data.packageName)) {
                installedList.remove(data)
                i--
            }
            i++
        }

        return ArrayList(installedList)
    }
}
