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.AdCountDownDialog.showAdCountDownDialog
import com.base.appzxhy.ads.AdEvent
import com.base.appzxhy.ads.AdsShowCallBack
import com.base.appzxhy.ads.AdsType
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.interstitial.InterstitialAd
import com.google.android.gms.ads.interstitial.InterstitialAdLoadCallback


/**
 *插屏广告加载显示管理类
 */
class AdInterMgr {

    private var adState = AdmobAdState()


    fun show(activity: Activity, adEvent: AdEvent, showCallBack: AdsShowCallBack?) {

        if (activity.isFinishing || activity.isDestroyed) {
            showCallBack?.failed(0)
            return
        }

        if (!LimitUtils.isAdShow(AdsType.INSERT, adEvent)) {
            showCallBack?.failed(1)
            return
        }
        if (LimitUtils.isIntervalLimited(adState.lastShowTime, adEvent)) {
            showCallBack?.failed(2)
            return
        }

        val loadingDialog = activity.showAdCountDownDialog()

        if (adState.currentAd == null || !adAvailable()) {
            loadAd(activity, adEvent) {
                if (adState.currentAd != null) {
                    createUICountdownTimer(loadingDialog) {
                        showReadyAd(activity, adEvent, showCallBack)
                    }
                }
            }
        } else {
            createUICountdownTimer(loadingDialog) {
                showReadyAd(activity, adEvent, showCallBack)
            }
        }
    }

    private fun showReadyAd(activity: Activity, adEvent: AdEvent, showCallBack: AdsShowCallBack?) {
        val ad = adState.currentAd as InterstitialAd?
        ad?.run {
            fullScreenContentCallback = object : FullScreenContentCallback() {
                override fun onAdShowedFullScreenContent() {
                    super.onAdShowedFullScreenContent()
                    adState.onAdDisplayed()
                    showCallBack?.show()
                    (adEvent as AdmobEvent).showAd(responseInfo, activity)
                    LimitUtils.addDisplayNum()
                }

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

                    adState.onAdDisplayFailed()
                    showCallBack?.adFailed()
                    adEvent.adShowError(adError)
                }

                override fun onAdDismissedFullScreenContent() {
                    super.onAdDismissedFullScreenContent()

                    adState.onAdHidden()

                    showCallBack?.close()
                    loadAd(activity.applicationContext, AdmobEvent("interAd", "preload"))
                }

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

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

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

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

            adEvent.adPulStart()

            InterstitialAd.load(context, GlobalConfig.ID_ADMOB_INTER, AdRequest.Builder().build(),
                object : InterstitialAdLoadCallback() {
                    override fun onAdLoaded(ad: InterstitialAd) {
                        adState.onAdLoaded(ad)
                        loadCallBack?.invoke()
                        (adEvent as AdmobEvent).pullAd(ad.responseInfo)
                        LimitUtils.addRequestNum()
                        ad.onPaidEventListener = AdmobEvent.EventOnPaidEventListener(ad)
                    }

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

    }


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