package com.base.filerecoveryrecyclebin.ads

import android.app.Activity
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.help.ConfigHelper
import com.google.android.gms.ads.MobileAds
import java.util.concurrent.atomic.AtomicBoolean

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

    fun initAdmobMaxAd() {
        if (ConfigHelper.admobTrueMaxFlase) {
            if (!isAdInit.get()) {
                MobileAds.initialize(BaseApplication.context) { initializationStatus ->
                    isAdInit.set(true)
                }
            }
        } else {
            if (!isAdInit.get()) {
                BaseApplication.context.initAdMax()
            }
        }
    }

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

    fun admobMaxShowInterstitialAd(activity: Activity, onHidden: (() -> Unit)? = null) {
        if (ConfigHelper.admobTrueMaxFlase) {
            AdmobInterstitialUtils.showInterstitialAd(activity) {
                onHidden?.invoke()
            }
        } else {
            AdMaxInterstitialUtils.showInterstitialAd(activity) {
                onHidden?.invoke()
            }
        }
    }

    fun admobMaxShowOpenAd(activity: Activity, showBeforeAction: () -> Unit, onHidden: (() -> Unit)? = null) {
        if (ConfigHelper.admobTrueMaxFlase) {
            AdmobOpenUtils.showAppOpenAd(activity, showBefore = showBeforeAction) {
                onHidden?.invoke()
            }
        } else {
            AdMaxOpenUtils.showAppOpenAd(activity, loaded = showBeforeAction) {
                onHidden?.invoke()
            }
        }
    }

    fun admobMaxShowNativeAd(activity: Activity, parent: ViewGroup, where: Int = 0) {
        if (ConfigHelper.admobTrueMaxFlase) {
            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
            }
            AdMaxNativeUtils.showNativeAd(activity, parent, layout)
        }
    }

}