package com.gh.common.chain

import android.content.Context
import com.gh.gamecenter.R
import com.gh.gamecenter.common.utils.DialogHelper
import com.gh.gamecenter.common.utils.SensorsBridge
import com.gh.gamecenter.common.utils.toResString
import com.gh.gamecenter.feature.entity.GameEntity

class VersionNumberHandler : DownloadChainHandler() {

    override fun handleRequest(context: Context, gameEntity: GameEntity, asVGame: Boolean) {
        val confirmCallback = {
            if (hasNext()) {
                getNext()?.handleRequest(context, gameEntity, asVGame)
            } else {
                processEndCallback?.invoke(asVGame, null)
            }
        }
        if (!gameEntity.isShowVersionNumber()) {
            confirmCallback.invoke()
        } else {
            SensorsBridge.trackGameDemoDialogShow(
                gameId = gameEntity.id,
                gameName = gameEntity.name ?: "",
                gameType = gameEntity.categoryChinese
            )
            DialogHelper.showGuideDialog(
                context,
                "温馨提示",
                gameEntity.getVersionNumberString(),
                "继续下载",
                com.gh.gamecenter.common.R.string.cancel.toResString(),
                {
                    confirmCallback.invoke()
                    SensorsBridge.trackGameDemoDialogClick(
                        buttonName = "继续下载",
                        gameId = gameEntity.id,
                        gameName = gameEntity.name ?: "",
                        gameType = gameEntity.categoryChinese
                    )
                },
                cancelClickCallback = {
                    SensorsBridge.trackGameDemoDialogClick(
                        buttonName = com.gh.gamecenter.common.R.string.cancel.toResString(),
                        gameId = gameEntity.id,
                        gameName = gameEntity.name ?: "",
                        gameType = gameEntity.categoryChinese
                    )
                },
                touchOutsideCallback = {
                    SensorsBridge.trackGameDemoDialogClick(
                        buttonName = "关闭弹窗",
                        gameId = gameEntity.id,
                        gameName = gameEntity.name ?: "",
                        gameType = gameEntity.categoryChinese
                    )
                },
                extraConfig = DialogHelper.Config(titleIcon = R.drawable.ic_dialog_tips)
            )
        }
    }
}