package com.base.locationsharewhite.ads.applovin

import android.content.Context
import androidx.annotation.LayoutRes
import com.applovin.mediation.MaxAd
import com.applovin.mediation.MaxError
import com.applovin.mediation.nativeAds.MaxNativeAdListener
import com.applovin.mediation.nativeAds.MaxNativeAdLoader
import com.applovin.mediation.nativeAds.MaxNativeAdView
import com.base.locationsharewhite.ads.AdsType
import com.base.locationsharewhite.ads.ConstConfig
import com.base.locationsharewhite.ads.CustomParentNativeView
import com.base.locationsharewhite.ads.admob.LimitUtils
import com.base.locationsharewhite.helper.EventUtils
import org.json.JSONObject
import java.util.UUID

/**
 *原生广告加载显示管理类
 */
class MaxNativeMgr {

    /**
     * 上一次的缓存成功时间
     */
    protected var lastTime: Long = 0

    /**
     * 原生广告
     */
    private var currentAd: MaxAd? = null
    private var currentLoader: MaxNativeAdLoader? = null

    fun loadAd(context: Context, parent: CustomParentNativeView? = null, @LayoutRes layout: Int? = null) {
        if (!LimitUtils.isAdShow(AdsType.NATIVE)) return

        val reqId = UUID.randomUUID().toString()
        val obj = JSONObject()
        obj.put("req_id", reqId)
        obj.put("ad_type", "nativeAd")

        val nativeAdLoader = MaxNativeAdLoader(ConstConfig.MAX_NATIVE_UNIT_ID, context)
        nativeAdLoader.setNativeAdListener(object : MaxNativeAdListener() {

            override fun onNativeAdLoaded(nativeAdView: MaxNativeAdView?, ad: MaxAd) {
                currentLoader = nativeAdLoader
                currentAd = ad
                lastTime = System.currentTimeMillis()
//                nativeAd.setOnPaidEventListener(AdmobEvent.EventOnPaidEventListener(nativeAd))
//                AdmobEvent.pullAd(nativeAd.responseInfo, "nativeAd", reqId = reqId)
                if (parent != null && layout != null) show(parent, layout)
            }

            override fun onNativeAdLoadFailed(adUnitId: String, error: MaxError) {

            }

            override fun onNativeAdClicked(ad: MaxAd) {

            }
        })
        nativeAdLoader.loadAd()
    }

    fun show(parent: CustomParentNativeView, @LayoutRes layout: Int? = null) {
        if (!LimitUtils.isAdShow(AdsType.NATIVE)) {
            currentLoader = null
            currentAd = null
            return
        }
        val nativeAd = currentAd
        val nativeLoader = currentLoader
        if ((nativeAd == null || nativeLoader == null).also {
                if (it) {
                    val obj2 = JSONObject()
                    obj2.put("reason", "no_ad")
                    obj2.put("ad_unit", "nativeAd")
                    EventUtils.event("ad_show_error", ext = obj2)
                }
            } || (!adAvailable()).also {
                if (it) {
                    val obj2 = JSONObject()
                    obj2.put("ad_unit", "nativeAd")
                    EventUtils.event("ad_expire", ext = obj2)
                }
            }) {
            //缓存过期了就清空
            currentLoader = null
            currentAd = null
            loadAd(parent.context.applicationContext, parent, layout)
            return
        }
        val obj = JSONObject()
        obj.put("ad_unit", "nativeAd")
        EventUtils.event("ad_prepare_show_native", ext = obj)
        parent.setNativeAd(nativeLoader!!, nativeAd!!, layout)
        // loadAd(parent.context.applicationContext)
    }

    fun adAvailable(): Boolean {
        return ((System.currentTimeMillis() - lastTime) / 1000 / 60).toInt() < 30
    }
}