package com.gh.common.util

import com.gh.gamecenter.common.constant.Constants
import com.gh.gamecenter.common.tracker.Tracker
import com.gh.gamecenter.common.utils.tryCatchInRelease
import com.gh.gamecenter.core.runOnIoThread
import com.gh.gamecenter.core.utils.MD5Utils
import com.gh.gamecenter.core.utils.SPUtils
import com.gh.gamecenter.entity.HomePluggableFilterEntity
import com.gh.gamecenter.feature.entity.GameEntity
import com.gh.gamecenter.home.PluginDisplayStatus
import com.gh.gamecenter.room.AppDatabase

/**
 * 插件化区域辅助类
 */
object HomePluggableHelper {

    private val mHomePluggableFilterDao by lazy { AppDatabase.getInstance().homePluggableFilterDao() }

    @JvmStatic
    fun setHomePluggableFilterData(gameEntity: GameEntity, isNever: Boolean) {
        val apkList = gameEntity.getApk()
        if (apkList.isNotEmpty()) {
            val apk = apkList.first()
            val apkHash = MD5Utils.getContentMD5(gameEntity.id + apk.version)
            val tag = if (isNever) "never" else apkHash
            runOnIoThread {
                tryCatchInRelease {
                    val filterData = mHomePluggableFilterDao.getDataByPkgName(apk.packageName)
                    if (filterData != null && !filterData.tag.contains(tag)) {
                        filterData.tag = filterData.tag + "," + tag
                        mHomePluggableFilterDao.addData(filterData)
                    } else {
                        mHomePluggableFilterDao.addData(
                            HomePluggableFilterEntity(
                                pkgName = apk.packageName,
                                tag = tag,
                                active = true
                            )
                        )
                    }
                }
            }
        }
    }

    /**
     * 获取插件化区域显示样式
     */
    fun getPluginDisplayedStyle(pluginList: List<Any>?): PluginDisplayStatus {
        if (pluginList.isNullOrEmpty()) {
            return PluginDisplayStatus.DEFAULT
        }

        val shouldShowCollapsedStyle = shouldShowCollapsedStyle()
        return if (shouldShowCollapsedStyle) {
            PluginDisplayStatus.CLOSE
        } else {
            setPluginAreaShowed()

            if (pluginList.size > 2) {
                PluginDisplayStatus.OPEN_TWO_AND_BUTTON
            } else {
                PluginDisplayStatus.OPEN
            }
        }
    }

    /**
     * 是否需要把插件化区域显示为收起样式
     * https://jira.shanqu.cc/browse/GHZS-4725
     */
    private fun shouldShowCollapsedStyle(): Boolean {
        val launchId = SPUtils.getString(Constants.SP_PLUGIN_AREA_SHOWED_LAUNCH_ID)
        return if (launchId.isEmpty()) {
            false
        } else {
            Tracker.launchId != launchId
        }
    }

    private fun setPluginAreaShowed() {
        SPUtils.setString(Constants.SP_PLUGIN_AREA_SHOWED_LAUNCH_ID, Tracker.launchId)
    }

    @JvmStatic
    fun showHomePluggable(gameEntity: GameEntity): Boolean {
        val apkList = gameEntity.getApk()
        if (apkList.isNotEmpty()) {
            val apk = apkList.first()
            tryCatchInRelease {
                val filterData = mHomePluggableFilterDao.getDataByPkgName(apk.packageName)
                if (filterData != null) {
                    val filterTag = filterData.tag
                    return !filterTag.contains("never") && !filterTag.contains(MD5Utils.getContentMD5(gameEntity.id + apk.version))
                }
            }
        }
        return true
    }

    @JvmStatic
    fun getPermanentInactivePluggablePackage(): List<HomePluggableFilterEntity>? {
        tryCatchInRelease {
            return mHomePluggableFilterDao.getDataByTag("never")
        }
        return emptyList()
    }

}