package com.base.filerecoveryrecyclebin.ads

import android.app.Activity
import android.util.Log
import android.view.ViewGroup
import com.base.filerecoveryrecyclebin.R
import com.base.filerecoveryrecyclebin.ads.admob.AdmobInterstitialUtils
import com.base.filerecoveryrecyclebin.ads.admob.AdmobNativeUtils
import com.base.filerecoveryrecyclebin.ads.admob.AdmobOpenUtils
import com.base.filerecoveryrecyclebin.ads.max.AdMaxInit.initAdMax
import com.base.filerecoveryrecyclebin.ads.max.AdMaxInterstitialUtils
import com.base.filerecoveryrecyclebin.ads.max.AdMaxNativeUtils
import com.base.filerecoveryrecyclebin.ads.max.AdMaxOpenUtils
import com.base.filerecoveryrecyclebin.help.BaseApplication
import com.base.filerecoveryrecyclebin.utils.AppPreferences
import com.base.filerecoveryrecyclebin.utils.EventUtils
import com.base.filerecoveryrecyclebin.utils.LogEx
import com.google.android.gms.ads.MobileAds
import java.util.concurrent.atomic.AtomicBoolean

object AdmobMaxHelper {
    private val TAG = "AdmobMaxHelper"
    var isAdInit = AtomicBoolean(false)
    var isBlack: Boolean = false

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

    fun showLoadingNativeSp(): Boolean {
        val isShowLoadingNative = AppPreferences.getInstance().getString("isShowLoadingNative", "0").toInt()
        return isShowLoadingNative == 1
    }

    fun showExitNativeSp(): Boolean {
        val isShowExitNative = AppPreferences.getInstance().getString("isShowExitNative", "0").toInt()
        return isShowExitNative == 1
    }

    fun showGuideNativeSp(): Boolean {
        val isShowGuideNative = AppPreferences.getInstance().getString("isShowGuideNative", "1").toInt()
        return isShowGuideNative == 1
    }

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

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


    //Begin
    fun initAdmobMaxAd() {
        if (isBlack) {
            return
        }
        if (getSpAdmobTrueMaxFalse()) {
            if (!isAdInit.get()) {
                LogEx.logDebug(TAG, "init sp Admob")
                MobileAds.initialize(BaseApplication.context) { initializationStatus ->
                    isAdInit.set(true)
                }
            }
        } else {
            if (!isAdInit.get()) {
                LogEx.logDebug(TAG, "init sp AdMax")
                BaseApplication.context.initAdMax()
            }
        }
    }

    //End end
    fun isOpenAdLoaded(): Boolean {
        if (getSpAdmobTrueMaxFalse()) {
            return AdmobOpenUtils.isOpenAdLoaded()
        } else {
            return AdMaxOpenUtils.isOpenAdLoaded()
        }
    }

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

        if (getSpAdmobTrueMaxFalse()) {
            AdmobInterstitialUtils.showInterstitialAd(activity) {
                onHidden?.invoke()
            }
        } else {
            AdMaxInterstitialUtils.showInterstitialAd(activity) {
                onHidden?.invoke()
            }
        }
    }

    fun admobMaxShowOpenAd(
        activity: Activity, showBeforeAction: (flag: Boolean) -> Unit,
        onHidden: (() -> Unit)? = null,
    ) {
        if (isBlack) {
            EventUtils.event("ad_black", "openAd")
            onHidden?.invoke()
            return
        }
        if (getSpAdmobTrueMaxFalse()) {
            AdmobOpenUtils.showAppOpenAd(activity, showBefore = showBeforeAction) {
                onHidden?.invoke()
            }
        } else {
            AdMaxOpenUtils.showAppOpenAd(activity, showBefore = showBeforeAction, loadCallBack = showBeforeAction) {
                onHidden?.invoke()
            }
        }
    }

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

        if (getSpAdmobTrueMaxFalse()) {
            val layout = when (where) {
                0 -> R.layout.layout_native_custom
                1 -> R.layout.layout_nativie_custom2
                3 -> R.layout.layout_native_full
                else -> R.layout.layout_native_custom
            }
            AdmobNativeUtils.showNativeAd(activity, parent, layout)
        } else {
            val layout = when (where) {
                0 -> R.layout.layout_max_native_small_f8f8f8
                1 -> R.layout.layout_max_native_big
                2 -> R.layout.layout_max_native_big_2
                3 -> R.layout.layout_max_native_full
                else -> R.layout.layout_max_native_small_f8f8f8
            }
            AdMaxNativeUtils.showNativeAd(activity, parent, layout)
        }
    }

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

        if (getSpAdmobTrueMaxFalse()) {
//            AdmobOpenUtils.loadAppOpenAd()
            AdmobInterstitialUtils.loadInterstitialAd(activity)
        } else {
            if (isAdInit.get()) {
                AdMaxOpenUtils.loadAppOpenAd(activity)
                AdMaxInterstitialUtils.loadInterstitialAd(activity)
            } else {
//                AdMaxInit.maxInitAction = {
                Log.d(TAG, "max init suc")
                AdMaxOpenUtils.loadAppOpenAd(activity)
                AdMaxInterstitialUtils.loadInterstitialAd(activity)
            }
        }
    }

}