package com.gh.common.util

import com.gh.gamecenter.core.utils.CurrentActivityHolder
import com.gh.download.DownloadManager
import com.gh.gamecenter.ShellActivity
import com.gh.gamecenter.common.constant.EntranceConsts
import com.gh.gamecenter.common.utils.DialogHelper
import com.gh.gamecenter.core.utils.ToastUtils
import com.gh.gamecenter.common.utils.getMetaExtra
import com.lightgame.download.DownloadEntity
import com.lightgame.download.DownloadStatus

object RealNameHelper {

    var pendingInstallPkgPath = ""

    /**
     * 弹未成年人不能下载游戏弹窗
     */
    fun showRealNameUnqualifiedDialog(downloadEntity: DownloadEntity) {
        val contentText = if (downloadEntity.status == DownloadStatus.done) {
            "为保护未成年身心健康成长\n" + "根据相关政策要求，该游戏不对未成年人开放"
        } else {
            "为保护未成年身心健康成长\n" + "根据相关政策要求，该游戏不对未成年人开放下载"
        }

        val currentActivity = CurrentActivityHolder.getCurrentActivity()
        if (currentActivity == null) {
            ToastUtils.toast("为保护未成年身心健康成长，根据相关政策要求，该游戏不对未成年人开放")
        } else {
            NewLogUtils.logCertificationHintDialogTrigger(true)
            DialogHelper.showDialog(
                currentActivity,
                title = "实名提示",
                content = contentText,
                confirmText = "知道了",
                cancelText = "",
                confirmClickCallback = {
//                                NewLogUtils.logCertificationHintDialogTrigger(true)
                }
            )
        }
        if (downloadEntity.status != DownloadStatus.done) {
            DownloadManager.getInstance()
                .addInvisiblePendingTask(downloadEntity)
        }
    }

    /**
     * 弹需要实名弹窗
     */
    fun showRealNameUncertificatedDialog(downloadEntity: DownloadEntity) {
        // 是否强制实名
        val isForced = downloadEntity.getMetaExtra("force_real_name") != "false"

        NewLogUtils.logCertificationTrigger(downloadEntity.gameId, downloadEntity.name)

        val contentText = if (downloadEntity.status == DownloadStatus.done) {
            "为保护未成年身心健康成长\n" + "根据相关政策要求，该游戏不对未成年人开放\n" + "如需完成安装，请前往实名认证"
        } else {
            "为保护未成年身心健康成长\n" + "根据相关政策要求，该游戏不对未成年人开放下载\n" + "如需下载，请前往实名认证"
        }

        val currentActivity = CurrentActivityHolder.getCurrentActivity()
        if (currentActivity == null
            || currentActivity.isFinishing
        ) {
            ToastUtils.toast("为保护未成年身心健康成长，根据相关政策要求，该游戏不对未成年人开放")
        } else {
            NewLogUtils.logCertificationHintDialogAppearance()
            val dialog = DialogHelper.showDialog(
                currentActivity,
                title = "实名提示",
                content = contentText,
                confirmText = "前往实名认证",
                cancelText = "取消",
                confirmClickCallback = {
                    currentActivity.startActivity(
                        ShellActivity.getIntent(
                            currentActivity,
                            ShellActivity.Type.REAL_NAME_INFO
                        ).apply {
                            putExtra(EntranceConsts.KEY_IS_FORCED_TO_CERTIFICATE, true)
                            putExtra(EntranceConsts.KEY_IS_FORCED_TO_CERTIFICATE_BUT_WITH_BACKDOOR, !isForced)
                        }
                    )
                    if (downloadEntity.status == DownloadStatus.done) {
                        pendingInstallPkgPath = downloadEntity.path
                    }
                    NewLogUtils.logCertificationHintDialogOptionsClicked("前往实名认证")
                },
                cancelClickCallback = {
                    NewLogUtils.logCertificationHintDialogOptionsClicked("取消")
                    if (!isForced) {
                        DownloadManager.getInstance()
                            .resumeAllInvisiblePendingTask()
                    }
                }
            )

            dialog?.setOnCancelListener {
                NewLogUtils.logCertificationHintDialogOptionsClicked("取消")
                if (!isForced) {
                    DownloadManager.getInstance()
                        .resumeAllInvisiblePendingTask()
                }
            }
        }

        if (downloadEntity.status != DownloadStatus.done) {
            DownloadManager.getInstance()
                .addInvisiblePendingTask(downloadEntity)
        }

    }

}