package com.gh.common.chain

import android.app.Activity
import android.content.Context
import com.gh.common.util.DialogUtils
import com.gh.common.util.TempCertificationUtils
import com.gh.gamecenter.feature.entity.GameEntity

class OverseaDownloadHandler : DownloadChainHandler() {

    override fun handleRequest(context: Context, gameEntity: GameEntity, asVGame: Boolean) {
        if (gameEntity.isOverseaAddressDialog()) {
            TempCertificationUtils.checkCertificationBeforeAction(gameEntity) {
                if (context is Activity && context.isFinishing) {
                    // 当前 context 已经无效，不需要再传递执行下去了
                    return@checkCertificationBeforeAction
                }

                DialogUtils.showOverseaDownloadDialog(context, gameEntity) {// 跳转海外下载地址弹窗
                    if (hasNext()) {
                        getNext()?.handleRequest(context, gameEntity, asVGame)
                    } else {
                        processEndCallback?.invoke(asVGame, null)
                    }
                }
            }
        } else {
            if (hasNext()) {
                getNext()?.handleRequest(context, gameEntity, asVGame)
            } else {
                processEndCallback?.invoke(asVGame, null)
            }
        }
    }
}