package com.base.appzxhy.ads.admob

import android.app.Activity
import android.content.Context
import com.base.appzxhy.GlobalConfig
import com.base.appzxhy.ads.AdCountDownDialog.createUICountdownTimer
import com.base.appzxhy.ads.AdsType
import com.base.appzxhy.ads.AdEvent
import com.base.appzxhy.ads.AdsShowCallBack
import com.base.appzxhy.ads.LimitUtils
import com.google.android.gms.ads.AdError
import com.google.android.gms.ads.AdRequest
import com.google.android.gms.ads.FullScreenContentCallback
import com.google.android.gms.ads.LoadAdError
import com.google.android.gms.ads.appopen.AppOpenAd


/**
 * 开屏广告加载显示管理类
 */
class AdOpenMgr {

    private val adState = AdmobAdState()

    fun show(activity: Activity, adEvent: AdEvent, showCallBack: AdsShowCallBack?) {
        if (activity.isFinishing || activity.isDestroyed) {
            return
        }
        if (!LimitUtils.isAdShow(AdsType.OPEN, adEvent)) {
            showCallBack?.failed()
            return
        }
        if (LimitUtils.isIntervalLimited(adState.lastShowTime, adEvent)) {
            showCallBack?.failed()
            return
        }
        adEvent.adPrepareShow()

        if (adState.currentAd == null || !adAvailable()) {

            loadAd(activity, adEvent) {
                if (adState.currentAd != null) {
                    createUICountdownTimer(null) {
                        showReadyAd(activity, adEvent, showCallBack)
                    }
                } else {
                    showCallBack?.failed()
                }
            }

        } else {
            createUICountdownTimer(null) {
                showReadyAd(activity, adEvent, showCallBack)
            }
        }

    }

    private fun showReadyAd(activity: Activity, adEvent: AdEvent, showCallBack: AdsShowCallBack?) {
        val ad = adState.currentAd as AppOpenAd?
        ad?.run {
            fullScreenContentCallback = object : FullScreenContentCallback() {
                override fun onAdShowedFullScreenContent() {

                    showCallBack?.show()

                    (adEvent as AdmobEvent).showAd(this@run.responseInfo, activity)
                    adState.onAdDisplayed()
                    //计数
                    LimitUtils.addDisplayNum()
                }

                override fun onAdFailedToShowFullScreenContent(adError: AdError) {
                    super.onAdFailedToShowFullScreenContent(adError)

                    showCallBack?.adFailed()
                    adState.onAdDisplayFailed()

                    (adEvent as AdmobEvent).adShowError(adError)

                }

                override fun onAdDismissedFullScreenContent() {
                    super.onAdDismissedFullScreenContent()
                    showCallBack?.close()
                    adState.onAdHidden()
                    loadAd(activity.applicationContext, AdmobEvent("openAd", "preload"))
                }

                override fun onAdClicked() {
                    (adEvent as AdmobEvent).clickAd(this@run.responseInfo)
                    //计数
                    LimitUtils.addClickNum()
                }
            }
            show(activity)
        }
    }

    fun loadAd(context: Context, adEvent: AdEvent, loadCallBack: (() -> Unit)? = null) {

        if (!LimitUtils.isAdShow(AdsType.OPEN, adEvent)) {
            loadCallBack?.invoke()
            adState.onAdLoadFailed()
            return
        }

        if (!adState.loadingAd) {
            adState.loadingAd = true
            adEvent.adPulStart()

            AppOpenAd.load(
                context,
                GlobalConfig.ID_ADMOB_OPEN,
                AdRequest.Builder().build(),
                object : AppOpenAd.AppOpenAdLoadCallback() {
                    override fun onAdLoaded(appOpenAd: AppOpenAd) {
                        adState.onAdLoaded(appOpenAd)
                        loadCallBack?.invoke()
                        (adEvent as AdmobEvent).pullAd(appOpenAd.responseInfo)
                        appOpenAd.onPaidEventListener = AdmobEvent.EventOnPaidEventListener(appOpenAd)
                        LimitUtils.addRequestNum()
                    }

                    override fun onAdFailedToLoad(loadAdError: LoadAdError) {
                        adState.onAdLoadFailed()
                        loadCallBack?.invoke()
                        (adEvent as AdmobEvent).pullAd(loadAdError.responseInfo, loadAdError)
                    }
                }
            )
        }
    }

    private fun adAvailable() = ((System.currentTimeMillis() - adState.lastLoadTime) / 1000 / 60).toInt() < 30
}