package com.simplecleaner.app.business.ads

import android.app.Activity
import android.content.Context
import android.view.View
import android.view.ViewGroup
import androidx.annotation.LayoutRes
import com.simplecleaner.app.bean.config.AdConfigBean.Companion.adsConfigBean
import com.simplecleaner.app.bean.config.ConfigBean.Companion.configBean
import com.simplecleaner.app.business.ads.admob.AdBannerMgr
import com.simplecleaner.app.business.ads.admob.AdInterMgr
import com.simplecleaner.app.business.ads.admob.AdNativeMgr
import com.simplecleaner.app.business.ads.admob.AdOpenMgr
import com.simplecleaner.app.business.ads.admob.AdmobEvent
import com.simplecleaner.app.business.helper.EventUtils
import com.google.android.gms.ads.MobileAds
import com.google.android.gms.ads.initialization.AdapterStatus
import com.simplecleaner.app.MyApplication

/**
 * 广告管理类
 */
object AdsMgr {

    private val adOpenMgr by lazy {
        AdOpenMgr()
    }
    private val adInterMgr by lazy {
        AdInterMgr()
    }
    private val adNativeMgr by lazy {
        AdNativeMgr()
    }
    private val adBannerMgr by lazy {
        AdBannerMgr()
    }

    /**
     * 是否初始化
     */
    var isAdmobInit = false
        private set


    var isAdUtEvent = false

    fun eventAdUt() {
        if (!isAdUtEvent) {
            isAdUtEvent = true
            EventUtils.event("ad_ut_${configBean.ut}")
        }
    }

    /**
     * Init 初始化
     *
     * @param context 这里最好是appContext，因为是耗时操作，等它初始化完毕马上加载开屏和插屏广告
     */
    fun init(context: Context) {
        initAdmob(context)
    }

    private fun initAdmob(context: Context) {
        if (isAdmobInit) return
        MobileAds.initialize(context) {
            val readyAdapter = it.adapterStatusMap.entries.find { entry ->
                entry.value.initializationState == AdapterStatus.State.READY
            }
            isAdmobInit = readyAdapter != null
            EventUtils.event("AdmobInit", "AdmobInit=$isAdmobInit")
//            context.toast("admob init")

            if (true) {
                admobInitCallBack?.invoke()
                admobInitCallBack = null
                //处理下app在前台再去预加载
                if (MyApplication.PAUSED_VALUE == 1) {
                    adNativeMgr.loadAd(context, AdmobEvent("nativeAd", context::class.java.simpleName))
                    adOpenMgr.loadAd(context, AdmobEvent("openAd", context::class.java.simpleName))
                    adInterMgr.loadAd(context, AdmobEvent("interAd", context::class.java.simpleName))
                }
            }
        }
    }

    var admobInitCallBack: (() -> Unit)? = null


    /**
     * 展示开屏广告
     *
     * @param activity 当前页面
     * @param showCallBack  展示回调
     */
    fun showOpen(
        activity: Activity,
        isUnLimit: Boolean = false,
        showCallBack: AdsShowCallBack? = null,
    ) {
        eventAdUt()

        if (!adsConfigBean.isAdShow) {
            showCallBack?.failed()
            return
        }


        val from = activity::class.java.simpleName
        if (true) {
            val admobEvent = AdmobEvent("openAd", from).apply { this.isUnLimit = isUnLimit }
            if (isAdmobInit) {
                adOpenMgr.show(activity, admobEvent, showCallBack)
            } else {
                admobInitCallBack = {
                }
                adOpenMgr.show(activity, admobEvent, showCallBack)
            }
        } else {
        }

    }

    /**
     * 展示插屏广告
     *
     * @param activity 当前页面
     * @param showCallBack  展示回调
     * @param isUnLimit 是否不受限制 默认false 代表受到限制
     */
    fun showInsert(
        activity: Activity,
        isUnLimit: Boolean = false,
        showCallBack: AdsShowCallBack? = null,
    ) {
        eventAdUt()

        if (!adsConfigBean.isAdShow) {
            showCallBack?.failed()
            return
        }
        val from = activity::class.java.simpleName
        if (true) {
            adInterMgr.show(activity, AdmobEvent("interAd", from).apply { this.isUnLimit = isUnLimit }, showCallBack)
        } else {
        }
    }

    /**
     * 展示原生广告
     *
     * @param nativeView 需要展示广告的布局容器
     * @param layout 原生广告布局
     */
    fun showNative(
        nativeView: NativeParentView,
        @LayoutRes layout: Int,
        nativeCallBack: ((Any?) -> Unit)? = null
    ) {
        if (!adsConfigBean.isAdShow || !LimitUtils.isShowNative(AdsType.NATIVE, null)) {
            nativeView.visibility = View.GONE
            nativeCallBack?.invoke(null)
            return
        }
        nativeView.visibility = View.VISIBLE

        val showNative = {
            if (true) {
                adNativeMgr.show(AdmobEvent("nativeAd", "nativeAd"), nativeView, layout, nativeCallBack)
            } else {
            }
        }

        if (isAdmobInit) {
            showNative.invoke()
        } else {
            admobInitCallBack = { showNative.invoke() }
        }

    }

    /**
     * 展示banner广告
     *
     * @param parent 展示广告的父布局容器
     */
    fun showBanner(parent: ViewGroup, collapsible: Boolean = true, adClose: (() -> Unit)? = null) {
        if (!adsConfigBean.isAdShow) {
            parent.visibility = View.GONE
            return
        }
        parent.visibility = View.VISIBLE

        if (true) {
            adBannerMgr.show(parent, collapsible, adClose)
        }
    }

}
