package com.gh.gamecenter.gamedetail.accelerator.chain

import android.content.Context
import com.gh.common.util.PackageUtils
import com.gh.gamecenter.gamedetail.accelerator.dialog.AcceleratorDialogFragment
import com.gh.gamecenter.gamedetail.accelerator.dialog.AcceleratorDialogFragment.Companion.SPEED_NOT_INSTALLED

class AcceleratorCheckInstallInterceptor : AcceleratorValidator.Interceptor {
    override fun intercept(
        context: Context,
        chain: AcceleratorValidator.Chain,
        listener: AcceleratorValidator.ValidateListener?
    ) {
        val request = chain.request
        if (PackageUtils.isInstalled(context, request.game.getUniquePackageName() ?: "")) {
            chain.proceed(context, request, listener)
        } else {
            val game = request.game
            AcceleratorDialogFragment.show(
                SPEED_NOT_INSTALLED,
                game.getUniquePackageName() ?: "",
                game.id,
                game.name ?: "",
                request.sourceEntrance, context
            )
        }
    }
}