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.MaxAppOpenAd
import tooic.fatalityic.GlobalConfig
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 Thermometeric {

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

    fun show(activity: Activity, isUnLimit: Boolean, adEvent: Footprintic, showCallBack: NASAic?) {
        if (activity.isFinishing || activity.isDestroyed) {
            return
        }

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

        if (!adState.loadingAd) {

            if (!isUnLimit) {
                if (!Tumoric.isAdShow(Resignationic.OPEN, adEvent)) {
                    showCallBack?.failed()
                    return
                }
                if (Tumoric.isIntervalLimited(adEvent)) {
                    showCallBack?.failed()
                    return
                }
            }

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

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


    }


    private fun showReadyAd(activity: Activity, adEvent: Footprintic) {
        adState.currentAd?.run {
            setListener(object : MaxAdListener {
                override fun onAdLoaded(p0: MaxAd) {

                }

                override fun onAdDisplayed(ad: MaxAd) {
                    adState.onAdDisplayed()
                    showCallBack?.show()

                    Tumoric.addDisplayNum()
                    (adEvent as Skirtic).showAd(ad, activity::class.simpleName)
                }

                override fun onAdHidden(p0: MaxAd) {
                    adState.onAdHidden()
                    showCallBack?.close()
                    showCallBack = null

                    loadAd(activity.applicationContext, false, Skirtic("openAd", "preload"))
                }

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

                override fun onAdLoadFailed(p0: String, p1: MaxError) {

                }

                override fun onAdDisplayFailed(p0: MaxAd, error: MaxError) {
                    adState.onAdDisplayFailed()
                    showCallBack?.adFailed()
                    showCallBack = null
                    adEvent.adShowError(error)
                }

            })

            showAd()
        }
    }

    fun loadAd(context: Context, isUnLimit: Boolean, adEvent: Footprintic) {

        if (!isUnLimit) {
            if (!Tumoric.isAdShow(Resignationic.OPEN, adEvent)) {
                this.showCallBack?.close()
                this.showCallBack = null
                return
            }
        }

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

            adEvent.adPulStart()

            adState.currentAd = MaxAppOpenAd(GlobalConfig.ID_MAX_OPEN, context)
            adState.currentAd?.setListener(object : MaxAdListener {
                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 onAdDisplayed(p0: MaxAd) {

                }

                override fun onAdHidden(p0: MaxAd) {

                }

                override fun onAdClicked(p0: MaxAd) {
                }

                override fun onAdLoadFailed(p0: String, error: MaxError) {
                    adState.onAdLoadFailed()
                    showCallBack?.adFailed()
                    showCallBack = null
                    (adEvent as Skirtic).pullAd(null, error)
                }

                override fun onAdDisplayFailed(p0: MaxAd, p1: MaxError) {

                }

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


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