package com.base.appzxhy.ads.applovin

import android.app.Activity
import com.applovin.mediation.MaxAd
import com.applovin.mediation.MaxAdListener
import com.applovin.mediation.MaxError
import com.applovin.mediation.ads.MaxInterstitialAd
import com.base.appzxhy.GlobalConfig
import com.base.appzxhy.MyApplication
import com.base.appzxhy.ads.AdCountDownDialog.createUICountdownTimer
import com.base.appzxhy.ads.AdCountDownDialog.showAdCountDownDialog
import com.base.appzxhy.ads.AdsType
import com.base.appzxhy.ads.AdEvent
import com.base.appzxhy.ads.AdsShowCallBack
import com.base.appzxhy.ads.LimitUtils

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

    private var adState = MaxAdState().apply {
        currentAd = MaxInterstitialAd(GlobalConfig.ID_MAX_INTER, MyApplication.appContext)
    }

    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
        }

        adEvent.adPrepareShow()

        val loadingDialog = activity.showAdCountDownDialog()

        if (adState.currentAd == null || !adAvailable()) {
            loadAd(adEvent) { flag: Boolean ->
                if (flag) {
                    createUICountdownTimer(loadingDialog) {
                        showReadyAd(activity, adEvent, showCallBack)
                    }
                } else {
                    showCallBack?.failed()
                }
            }
        } else {
            showReadyAd(activity, adEvent, showCallBack)
        }
    }


    private fun showReadyAd(activity: Activity, adEvent: AdEvent, showCallBack: AdsShowCallBack?) {
        val ad = adState.currentAd as MaxInterstitialAd?

        ad?.run {
            setListener(object : MaxAdListener {
                override fun onAdLoaded(p0: MaxAd) = Unit
                override fun onAdLoadFailed(p0: String, p1: MaxError) = Unit

                override fun onAdDisplayed(ad: MaxAd) {

                    adState.onAdDisplayed()
                    showCallBack?.show()

                    (adEvent as AdMaxEvent).showAd(ad, activity::class.simpleName)
                    //计数
                    LimitUtils.addDisplayNum()
                }

                override fun onAdDisplayFailed(ad: MaxAd, error: MaxError) {
                    adState.onAdDisplayFailed()
                    showCallBack?.adFailed()

                    (adEvent as AdMaxEvent).adShowError(error)
                }

                override fun onAdHidden(p0: MaxAd) {

                    adState.onAdHidden()
                    showCallBack?.close()

                    loadAd(AdMaxEvent("interAd", "preload"))
                }

                override fun onAdClicked(ad: MaxAd) {
                    (adEvent as AdMaxEvent).clickAd(ad)
                    //计数
                    LimitUtils.addClickNum()
                }


            })
            setRevenueListener(AdMaxEvent.EventOnPaidEventListener())

            showAd(activity)
        }
    }


    fun loadAd(
        adEvent: AdEvent,
        loadCallBack: ((flag: Boolean) -> Unit)? = null
    ) {
        if (!LimitUtils.isAdShow(AdsType.INSERT, adEvent)) {
            loadCallBack?.invoke(false)
            return
        }

        val ad = adState.currentAd as MaxInterstitialAd?

        ad?.setListener(object : MaxAdListener {

            override fun onAdDisplayed(p0: MaxAd) = Unit
            override fun onAdHidden(p0: MaxAd) = Unit
            override fun onAdClicked(p0: MaxAd) = Unit
            override fun onAdDisplayFailed(p0: MaxAd, p1: MaxError) = Unit

            override fun onAdLoaded(ad: MaxAd) {
                loadCallBack?.invoke(true)
                adState.onAdLoaded()
                (adEvent as AdMaxEvent).pullAd(ad)
                LimitUtils.addRequestNum()
            }

            override fun onAdLoadFailed(ad: String, error: MaxError) {
                loadCallBack?.invoke(false)
                adState.onAdLoadFailed()
                (adEvent as AdMaxEvent).pullAd(null, error)

            }

        })

        if (!adState.loadingAd) {
            adState.loadingAd = true
            adEvent.adPulStart()
            ad?.loadAd()
        }
    }


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