package autonomyic.polemicalic.congestionic.distendic

import android.app.Activity
import android.content.Context
import com.applovin.mediation.MaxAd
import com.applovin.mediation.MaxAdListener
import com.applovin.mediation.MaxError
import com.applovin.mediation.ads.MaxInterstitialAd
import tooic.fatalityic.GlobalConfig
import autonomyic.polemicalic.congestionic.Aqueductic.showAdCountDownDialog
import autonomyic.polemicalic.congestionic.Footprintic
import autonomyic.polemicalic.congestionic.Refrigerationic
import autonomyic.polemicalic.congestionic.NASAic
import autonomyic.polemicalic.congestionic.Resignationic
import autonomyic.polemicalic.congestionic.Tumoric
import java.lang.ref.WeakReference

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

    private var adState = Refrigerationic<MaxInterstitialAd>()
    private var showCallBack: NASAic? = null

    fun show(
        activity: Activity,
        isUnLimit: Boolean,
        adEvent: Footprintic,
        showCallBack: NASAic?
    ) {

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

        if (showCallBack != null) {
            adState.activityRef = WeakReference(activity)
            this.showCallBack = showCallBack
            if (adState.adDialog == null) {
                adState.adDialog = activity.showAdCountDownDialog()
            }
            adEvent.adPrepareShow()
        }

        if (!adState.loadingAd) {
            if (!isUnLimit) {
                if (!Tumoric.isAdShow(Resignationic.INSERT, adEvent)) {
                    showCallBack?.failed(3)
                    return
                }
                if (Tumoric.isIntervalLimited(adEvent)) {
                    showCallBack?.failed(4)
                    return
                }
            }

            if (!adAvailable() || adState.currentAd == null) {
                loadAd(activity, adEvent, isUnLimit)
                return
            }

            if (adState.currentAd?.isReady == false) {
                loadAd(activity, adEvent, isUnLimit)
                return
            }
            showReadyAd(adEvent)
        }

    }


    private fun showReadyAd(adEvent: Footprintic) {
        adState.currentAd?.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 Skirtic).showAd(ad, activity::class.simpleName)
                    //计数
                    Tumoric.addDisplayNum()
                }

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

                    (adEvent as Skirtic).adShowError(error)
                }

                override fun onAdHidden(p0: MaxAd) {

                    adState.onAdHidden()
                    showCallBack?.close()

                    loadAd(activity.applicationContext, Skirtic("interAd", "preload"))
                }

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


            })
            setRevenueListener(Skirtic.EventOnPaidEventListener())

            showAd(activity)
        }
    }


    fun loadAd(
        context: Context,
        adEvent: Footprintic,
        isUnLimit: Boolean = false,
    ) {
        if (!isUnLimit) {
            if (!Tumoric.isAdShow(Resignationic.INSERT, adEvent)) {
                this.showCallBack?.close(4)
                this.showCallBack = null
                return
            }
        }

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

            adEvent.adPulStart()

            adState.currentAd = MaxInterstitialAd(GlobalConfig.ID_MAX_INTER, context)
            adState.currentAd?.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) {
                    adState.onAdLoaded(null)
                    val ac = adState.activityRef?.get()
                    if (ac != null) {
                        show(ac, isUnLimit, adEvent, null)
                    }

                    (adEvent as Skirtic).pullAd(ad)
                    Tumoric.addRequestNum()
                }

                override fun onAdLoadFailed(ad: String, error: MaxError) {
                    adState.onAdLoadFailed()

                    (adEvent as Skirtic).pullAd(null, error)

                    showCallBack?.adFailed(5)
                    showCallBack = null
                }

            })
            adState.currentAd?.loadAd()
        }
    }


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