package com.gh.common.util

import android.content.Context
import android.os.Build
import com.gh.gamecenter.common.utils.DialogHelper
import com.gh.gamecenter.core.utils.EmptyCallback
import com.gh.gamecenter.entity.ApkEntity
import com.gh.gamecenter.entity.GameEntity

object DownloadDialogHelper {

    /**
     * [callback] 执行点击关闭以后会执行的代码块，没有弹窗也会执行这个代码块
     */
    @JvmStatic
    fun findAvailableDialogAndShow(
        context: Context,
        gameEntity: GameEntity,
        apkEntity: ApkEntity,
        callback: EmptyCallback
    ) {
        val dialog = retrieveAvailableDialog(gameEntity, apkEntity)
        if (dialog != null) {
            showDownloadDialog(context, dialog, callback)
        } else {
            callback.onCallback()
        }
    }

    private fun retrieveAvailableDialog(gameEntity: GameEntity, apkEntity: ApkEntity): GameEntity.Dialog? {
        val downloadDialog = if (gameEntity.shouldUseMirrorInfo()) {
            gameEntity.mirrorData?.downloadDialog
        } else {
            gameEntity.downloadDialog
        }

        if (downloadDialog.isNullOrEmpty()) return null
        for (dialog in downloadDialog) {
            val versionName = PackageUtils.getGhVersionName()
            val noticeVersions = dialog.rule.noticeVersions
            if (noticeVersions.isEmpty() || noticeVersions.contains(versionName)) {
                // 共有 8 种可能
                // 1. 不指定包名、机型和系统版本
                if (dialog.rule.packageName.isEmpty()
                    && dialog.rule.models.isEmpty()
                    && dialog.rule.systemVersions.isEmpty()
                ) {
                    return dialog
                }

                // 2. 指定包名不管机型和系统
                if (dialog.rule.packageName == apkEntity.packageName
                    && dialog.rule.models.isEmpty()
                    && dialog.rule.systemVersions.isEmpty()
                ) {
                    return dialog
                }

                // 3. 指定机型不管包名和系统
                if (dialog.rule.models.contains(Build.MODEL)
                    && dialog.rule.packageName.isEmpty()
                    && dialog.rule.systemVersions.isEmpty()
                ) {
                    return dialog
                }

                // 4. 指定系统不管包名和机型
                if (dialog.rule.systemVersions.contains(Build.VERSION.RELEASE)
                    && dialog.rule.packageName.isEmpty()
                    && dialog.rule.models.isEmpty()
                ) {
                    return dialog
                }

                // 5. 指定包名和机型不管系统
                if (dialog.rule.packageName == apkEntity.packageName
                    && dialog.rule.models.contains(Build.MODEL)
                    && dialog.rule.systemVersions.isEmpty()
                ) {
                    return dialog
                }

                // 6. 指定包名和系统不管机型
                if (dialog.rule.packageName == apkEntity.packageName
                    && dialog.rule.systemVersions.contains(Build.VERSION.RELEASE)
                    && dialog.rule.models.isEmpty()
                ) {
                    return dialog
                }

                // 7. 指定机型和系统不管包名
                if (dialog.rule.systemVersions.contains(Build.VERSION.RELEASE)
                    && dialog.rule.models.contains(Build.MODEL)
                    && dialog.rule.packageName.isEmpty()
                ) {
                    return dialog
                }

                // 8.指定包名、机型和系统版本
                if (dialog.rule.packageName == apkEntity.packageName
                    && dialog.rule.models.contains(Build.MODEL)
                    && dialog.rule.systemVersions.contains(Build.VERSION.RELEASE)
                ) {
                    return dialog
                }
            } else return null
        }

        return null
    }

    private fun showDownloadDialog(context: Context, dialog: GameEntity.Dialog, callback: EmptyCallback) {
        DialogHelper.showDialogWithHtmlContent(
            context,
            dialog.title,
            dialog.content,
            "继续下载",
            "取消",
            { callback.onCallback() })
    }

}