package com.base.scanqr.ads.applovin

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 com.base.scanqr.GlobalConfig
import com.base.scanqr.ads.AdEvent
import com.base.scanqr.ads.AdsShowCallBack
import com.base.scanqr.ads.AdsType
import com.base.scanqr.ads.LimitUtils
import com.base.scanqr.ads.AdState
import java.lang.ref.WeakReference

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

    private val adState = AdState<MaxAppOpenAd>()
    private var showCallBack: AdsShowCallBack? = null

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

        if (adState.showingAd) {
            showCallBack?.failed()
            adEvent.adShowError("showingAd")
            return
        }

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

        if (!adState.loadingAd) {

            if (!isUnLimit) {
                if (!LimitUtils.isAdShow(AdsType.OPEN, adEvent)) {
                    showCallBack?.failed()
                    return
                }
                if (LimitUtils.isIntervalLimited(adState.lastShowTime, 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: AdEvent) {
        adState.currentAd?.run {
            setListener(object : MaxAdListener {
                override fun onAdLoaded(p0: MaxAd) {

                }

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

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

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

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

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

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

                }

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

            })

            showAd()
        }
    }

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

        if (!isUnLimit) {
            if (!LimitUtils.isAdShow(AdsType.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 AdMaxEvent).pullAd(ad)
                    LimitUtils.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?.googleFailed()
                    showCallBack = null
                    (adEvent as AdMaxEvent).pullAd(null, error)
                }

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

                }

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


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