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.MaxAppOpenAd
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 MaxOpenMgr {

    private val adState = MaxAdState().apply {
        currentAd = MaxAppOpenAd(GlobalConfig.ID_MAX_OPEN, 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 (!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 MaxAppOpenAd?

        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()
                    LimitUtils.addDisplayNum()
                    (adEvent as AdMaxEvent).showAd(ad, activity::class.simpleName)
                }

                override fun onAdHidden(p0: MaxAd) {
                    adState.onAdHidden()
                    showCallBack?.close()
                    loadAd(AdMaxEvent("openAd", "preload"))
                }

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


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

            })

            showAd()
        }
    }

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

        if (!LimitUtils.isAdShow(AdsType.OPEN, adEvent)) {
            loadCallBack?.invoke(false)
            return
        }
        val ad = adState.currentAd as MaxAppOpenAd?
        ad?.setListener(object : MaxAdListener {
            override fun onAdLoaded(ad: MaxAd) {
                adState.onAdLoaded()
                loadCallBack?.invoke(true)
                (adEvent as AdMaxEvent).pullAd(ad)
                LimitUtils.addRequestNum()
            }

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

            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
        })

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


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