package com.base.datarecovery.ads

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

object AdmobMaxHelper {
    var isBlack: Boolean = false

    private val TAG = ""

    var isAdInit = AtomicBoolean(false)

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

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

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

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

        if (getSpAdmobTrueMaxFalse()) {
            AdmobNativeUtils.showNativeAd(activity, parent)
        } else {
            var layout = R.layout.layout_max_native_small
            if (where == 0) {
                layout = R.layout.layout_max_native_small_f8f8f8
            }
            if (where == 1) {
                layout = R.layout.layout_max_native_big
            }
            if (where == 2) {
                layout = R.layout.layout_max_native_big_2
            }
            AdMaxNativeUtils.showNativeAd(activity, parent, layout)
        }
    }

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

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

    fun initAdmobMaxAd(activity: Activity? = null) {
        if (isBlack) {
            return
        }

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

    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)
                }
            }
        }
    }

}