package importantedto.admireedto.sweptedto

import Funeraledto
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 importantedto.admireedto.sweptedto.joinedto.Parkingedto
import importantedto.admireedto.sweptedto.joinedto.Situationedto
import importantedto.admireedto.sweptedto.joinedto.Situationedto.initAdMax
import importantedto.admireedto.sweptedto.joinedto.Suntanedto
import importantedto.admireedto.sweptedto.testedto.huntedto.storageedto.Complainedto
import importantedto.admireedto.sweptedto.testedto.huntedto.storageedto.Leavingedto
import importantedto.admireedto.sweptedto.testedto.huntedto.storageedto.Orbitedto
import importantedto.afteredto.constitutionaledto.Interferenceedto
import importantedto.gallonedto.Rideredto
import importantedto.gallonedto.Tumbleedto
import importantedto.gallonedto.Waitedto
import java.util.concurrent.atomic.AtomicBoolean

object Differenceedto {
    var isBlack: Boolean = false

    private val TAG = ""

    var isAdInit = AtomicBoolean(false)


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

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

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

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

    fun isOpenAdLoaded(): Boolean {
        return if (getSpAdmobTrueMaxFalse()) {
            Complainedto.isOpenAdLoaded()
        } else {
            Parkingedto.isOpenAdLoaded()
        }
    }

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

        if (getSpAdmobTrueMaxFalse()) {
            Leavingedto.showInterstitialAd(activity) {
                onHidden?.invoke()
            }
        } else {
            Suntanedto.showInterstitialAd(activity) {
                onHidden?.invoke()
            }
        }
    }

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

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

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

        if (getSpAdmobTrueMaxFalse()) {
            Orbitedto.showNativeAd(activity, parent)
        } else {
            var layout = R.layout.huntingedto
            if (where == 0) {
                layout = R.layout.circulateedto
            }
            if (where == 1) {
                layout = R.layout.hospitaledto
            }
            if (where == 2) {
                layout = R.layout.everyedto
            }
            Funeraledto.showNativeAd(activity, parent, layout)
        }
    }


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

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

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

        if (getSpAdmobTrueMaxFalse()) {
//            Complainedto.loadAppOpenAd()
            Leavingedto.loadInterstitialAd(activity)
        } else {
            if (isAdInit.get()) {
//                Parkingedto.loadAppOpenAd(activity)
                Suntanedto.loadInterstitialAd(activity)
            } else {
                Situationedto.maxInitAction = {
                    Log.d(TAG, "max init suc")
//                    Parkingedto.loadAppOpenAd(activity)
//                    Suntanedto.loadInterstitialAd(activity)
                }
            }
        }
    }

}