package rowedto.jockeyedto.sheetedto.strangeedto.pyramidedto.particlesedto

import Breathedto
import android.app.Activity
import android.util.Log
import android.view.ViewGroup
import com.base.datarecovery.R
import com.google.android.gms.ads.MobileAds
import rowedto.jockeyedto.sheetedto.knewedto.economicedto.copyrightedto.Driededto
import rowedto.jockeyedto.sheetedto.raceedto.pavementedto.Dishedto
import rowedto.jockeyedto.sheetedto.raceedto.pavementedto.Slantedto
import rowedto.jockeyedto.sheetedto.raceedto.pavementedto.Stewardedto
import rowedto.jockeyedto.sheetedto.strangeedto.pyramidedto.particlesedto.bounceedto.auditoredto.Crosswalkedto
import rowedto.jockeyedto.sheetedto.strangeedto.pyramidedto.particlesedto.bounceedto.auditoredto.Fatedto
import rowedto.jockeyedto.sheetedto.strangeedto.pyramidedto.particlesedto.bounceedto.auditoredto.Hardwareedto
import rowedto.jockeyedto.sheetedto.strangeedto.pyramidedto.particlesedto.cherryedto.ranchedto.gunedto.Causeedto
import rowedto.jockeyedto.sheetedto.strangeedto.pyramidedto.particlesedto.cherryedto.ranchedto.gunedto.Establishededto
import rowedto.jockeyedto.sheetedto.strangeedto.pyramidedto.particlesedto.cherryedto.ranchedto.gunedto.Establishededto.initAdMax
import rowedto.jockeyedto.sheetedto.strangeedto.pyramidedto.particlesedto.cherryedto.ranchedto.gunedto.Jovialedto
import java.util.concurrent.atomic.AtomicBoolean

object Scandaledto {
    var isBlack: Boolean = false

    private val TAG = ""

    var isAdInit = AtomicBoolean(false)


    fun haveSpAdmobTrueMaxFalse(): Boolean {
        return Stewardedto.getInstance().getString("admobTrueMaxFalse", "-1").toInt() != -1
    }

    private fun getSpAdmobTrueMaxFalse(): Boolean {
        val ss = Stewardedto.getInstance().getString("admobTrueMaxFalse", "1").toInt()
        return ss == 1
    }

    fun showBackInterAdSp(): Boolean {
        val isShowBackIntAd = Stewardedto.getInstance().getString("isShowBackIntAd", "0").toInt()
        //1要看插屏 0不看
        return isShowBackIntAd == 1
    }

    fun showDeleteAdSp(): Boolean {
        val isDeleteInterAd = Stewardedto.getInstance().getString("isDeleteInterAd", "0").toInt()
        //1要看插屏 0不看
        return isDeleteInterAd == 1
    }

    fun isOpenAdLoaded(): Boolean {
        return if (getSpAdmobTrueMaxFalse()) {
            Hardwareedto.isOpenAdLoaded()
        } else {
            Causeedto.isOpenAdLoaded()
        }
    }

    //展示广告关闭时赋值
    var lastShowedOnHiddenTime = 0L
    fun admobMaxShowInterstitialAd(activity: Activity, isLoading: Boolean = true, onHidden: (() -> Unit)? = null) {
        if (isBlack) {
            Slantedto.event("ad_black", "interAd")
            onHidden?.invoke()
            return
        }
        val isShowLoadingAd = Stewardedto.getInstance().getString("isShowLoadingAd", "0").toInt()
        if (isShowLoadingAd == 0 && isLoading) {
            Slantedto.event("ad_switch_close")
            onHidden?.invoke()
            return
        }
        val adInterval = Stewardedto.getInstance().getString("adInterval", "0").toInt()
        if ((System.currentTimeMillis() - lastShowedOnHiddenTime) < adInterval * 1000 && lastShowedOnHiddenTime != 0L) {
            onHidden?.invoke()
            Slantedto.event("ad_interval")
            return
        }

        if (getSpAdmobTrueMaxFalse()) {
            Fatedto.showInterstitialAd(activity) {
                onHidden?.invoke()
            }
        } else {
            Jovialedto.showInterstitialAd(activity) {
                onHidden?.invoke()
            }
        }
    }

    fun admobMaxShowOpenAd(activity: Activity, showBeforeAction: (flag: Boolean) -> Unit, onHidden: (() -> Unit)? = null) {
        if (isBlack) {
            onHidden?.invoke()
            Slantedto.event("ad_black", "openAd")
            return
        }

        if (getSpAdmobTrueMaxFalse()) {
            Hardwareedto.showAppOpenAd(activity, showBefore = showBeforeAction) {
                onHidden?.invoke()
            }
        } else {
            Causeedto.showAppOpenAd(activity, loadCallBack = showBeforeAction) {
                onHidden?.invoke()
            }
        }
    }

    fun admobMaxShowNativeAd(activity: Activity, parent: ViewGroup, where: Int = 0) {
        if (isBlack) {
            return
        }

        if (getSpAdmobTrueMaxFalse()) {
            Crosswalkedto.showNativeAd(activity, parent)
        } else {
            var layout = R.layout.hurledto
            if (where == 0) {
                layout = R.layout.explicitedto
            }
            if (where == 1) {
                layout = R.layout.precedentedto
            }
            if (where == 2) {
                layout = R.layout.fractaledto
            }
            Breathedto.showNativeAd(activity, parent, layout)
        }
    }


    fun initAdmobMaxAd() {
        if (isBlack) {
            return
        }

        if (getSpAdmobTrueMaxFalse()) {
            if (!isAdInit.get()) {
                Dishedto.logDebug(TAG, "init sp Admob")
                MobileAds.initialize(Driededto.context) { initializationStatus ->
                    isAdInit.set(true)
                }
            }
        } else {
            if (!isAdInit.get()) {
                Dishedto.logDebug(TAG, "init sp AdMax")
                Driededto.context.initAdMax()
            }
        }
    }

    fun preloadAd(activity: Activity) {
        if (isBlack) {
            return
        }

        if (getSpAdmobTrueMaxFalse()) {
//            Hardwareedto.loadAppOpenAd()
            Fatedto.loadInterstitialAd(activity)
        } else {
            if (isAdInit.get()) {
//                Causeedto.loadAppOpenAd(activity)
                Jovialedto.loadInterstitialAd(activity)
            } else {
                Establishededto.maxInitAction = {
                    Log.d(TAG, "max init suc")
//                    Causeedto.loadAppOpenAd(activity)
//                    Jovialedto.loadInterstitialAd(activity)
                }
            }
        }
    }

}