package com.ritoq.quickphone.business.pangle

import android.app.Activity
import com.ritoq.quickphone.GlobalConfig
import com.ritoq.quickphone.business.ads.AdsShowCallBack
import com.ritoq.quickphone.utils.LogEx
import com.bytedance.sdk.openadsdk.api.interstitial.PAGInterstitialAd
import com.bytedance.sdk.openadsdk.api.interstitial.PAGInterstitialAdInteractionListener
import com.bytedance.sdk.openadsdk.api.interstitial.PAGInterstitialAdLoadListener
import com.bytedance.sdk.openadsdk.api.interstitial.PAGInterstitialRequest
import com.ritoq.quickphone.business.pangle.PangleEvent.eventAdClick
import com.ritoq.quickphone.business.pangle.PangleEvent.eventAdPull
import com.ritoq.quickphone.business.pangle.PangleEvent.eventAdPullStart
import com.ritoq.quickphone.business.pangle.PangleEvent.eventAdShow
import com.ritoq.quickphone.business.pangle.PangleEvent.eventPangleAd

class PangleInterMgr {

    private val TAG = "PangleInterMgr"
    private var interstitialAd: PAGInterstitialAd? = null
    private var showCallBack: AdsShowCallBack? = null

    fun load(callback: ((flag: Boolean) -> Unit)? = null) {
        eventAdPullStart()
        PAGInterstitialAd.loadAd(
            GlobalConfig.ID_PANGLE_INTER,
            PAGInterstitialRequest(),
            object : PAGInterstitialAdLoadListener {
                override fun onError(code: Int, message: String?) {
                    eventAdPull("code:$code message:$message")
                    LogEx.logDebug(TAG, "loadAd onError code=$code message=$message")
                    callback?.invoke(false)
                }

                override fun onAdLoaded(pagInterstitialAd: PAGInterstitialAd?) {
                    eventAdPull()
                    LogEx.logDebug(TAG, "onAdLoaded")
                    interstitialAd = pagInterstitialAd
                    callback?.invoke(true)
                }
            })
    }

    fun show(activity: Activity, showCallBack: AdsShowCallBack?) {

        if (activity.isFinishing || activity.isDestroyed) {
            return
        }

        this.showCallBack = showCallBack

        if (interstitialAd == null) {
            LogEx.logDebug(TAG, "interstitialAd load")
            load {
                if (it) {
                    showReadyAd(activity)
                } else {
                    showCallBack?.adFailed()
                }
            }
        } else {
            LogEx.logDebug(TAG, "interstitialAd ready")
            showReadyAd(activity)
        }
    }

    private fun showReadyAd(ac: Activity) {

        if (ac.isFinishing || ac.isDestroyed) {
            return
        }
        if (interstitialAd == null) {
            showCallBack?.failed()
            return
        }
        LogEx.logDebug(TAG, "interstitialAd show")

        interstitialAd?.setAdInteractionListener(object : PAGInterstitialAdInteractionListener {
            override fun onAdShowed() {
                showCallBack?.show()

                interstitialAd?.let {
                    eventPangleAd(ac, it)
                }

                interstitialAd = null
                load()
                eventAdShow()
            }

            override fun onAdClicked() {
                eventAdClick()
            }

            override fun onAdDismissed() {
                showCallBack?.close()
            }
        })
        interstitialAd?.show(ac)
    }

}