package com.base.filerecoveryrecyclebin.ads.max

import android.app.Activity
import android.content.Context
import com.adjust.sdk.Adjust
import com.adjust.sdk.AdjustAdRevenue
import com.adjust.sdk.AdjustConfig
import com.applovin.mediation.MaxAd
import com.applovin.mediation.MaxAdListener
import com.applovin.mediation.MaxError
import com.applovin.mediation.ads.MaxAppOpenAd
import com.base.filerecoveryrecyclebin.ads.AdDisplayUtils
import com.base.filerecoveryrecyclebin.ads.AdmobCommonUtils
import com.base.filerecoveryrecyclebin.help.ConfigHelper
import com.base.filerecoveryrecyclebin.utils.EventUtils
import com.base.filerecoveryrecyclebin.utils.LogEx
import org.json.JSONObject

object AdMaxOpenUtils {
    private val TAG = "AdMaxOpenUtils"
    private var appOpenAd: MaxAppOpenAd? = null
    private var openLoadTime = Long.MAX_VALUE
    private var onHidden: (() -> Unit)? = null
    private var loadingListener: (() -> Unit)? = null

    private fun setListener(context: Context) {
        if (appOpenAd == null) {
            appOpenAd = MaxAppOpenAd(ConfigHelper.openAdMaxId, context)

            appOpenAd?.setListener(object : MaxAdListener {
                override fun onAdLoaded(p0: MaxAd) {
                    openLoadTime = System.currentTimeMillis()
                    loadingListener?.invoke()
                }

                override fun onAdDisplayed(p0: MaxAd) {
                }

                override fun onAdHidden(p0: MaxAd) {
                    onHidden?.invoke()
                    appOpenAd?.loadAd()
                }

                override fun onAdClicked(p0: MaxAd) {
//                    AdMaxEvent.clickAd(appOpenAd, "openAd")
                    AdmobCommonUtils.isMultiClick(appOpenAd)
                }

                override fun onAdLoadFailed(p0: String, p1: MaxError) {
                    appOpenAd?.loadAd()
                }

                override fun onAdDisplayFailed(p0: MaxAd, p1: MaxError) {
                    onHidden?.invoke()
                    appOpenAd?.loadAd()
                    val obj = JSONObject()
                    obj.put("reason", p1.message)
                    obj.put("ad_unit", "openAd")
                    EventUtils.event("ad_show_error", ext = obj)
                }
            })

            appOpenAd?.setRevenueListener { ad ->
                val adjustAdRevenue = AdjustAdRevenue(AdjustConfig.AD_REVENUE_APPLOVIN_MAX)
                adjustAdRevenue.setRevenue(ad.revenue, "USD")
                adjustAdRevenue.setAdRevenueNetwork(ad.networkName)
                adjustAdRevenue.setAdRevenueUnit(ad.adUnitId)
                adjustAdRevenue.setAdRevenuePlacement(ad.placement)
                Adjust.trackAdRevenue(adjustAdRevenue)
            }
        }
    }

    fun showAppOpenAd(activity: Activity, loaded: (() -> Unit)? = null, onHidden: (() -> Unit)? = null) {

        setListener(activity)

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

        if (!AdDisplayUtils.getInstance().shouldShowAd()) {
            LogEx.logDebug(TAG, "!shouldShowAd")
            onHidden?.invoke()
            return
        }

        if (System.currentTimeMillis() - openLoadTime > 1000 * 60 * 60) {
            LogEx.logDebug(TAG, "openLoadTime > ")
            onHidden?.invoke()
            val obj2 = JSONObject()
            obj2.put("ad_unit", "openAd")
            EventUtils.event("ad_expire", ext = obj2)
            appOpenAd?.loadAd()
        }

        LogEx.logDebug(TAG, "showAppOpenAd continue")

        appOpenAd?.loadAd()

        loadingListener = {
            loaded?.invoke()
            LogEx.logDebug(TAG, "loadingListener callback")
            if (appOpenAd?.isReady == true) {
                val obj1 = JSONObject()
                obj1.put("ad_unit", "openAd")
                EventUtils.event("ad_prepare_show", ext = obj1)
                this.onHidden = onHidden
                appOpenAd?.showAd()
                loadingListener = null
                appOpenAd?.loadAd()
            } else {
                onHidden?.invoke()
                appOpenAd?.loadAd()
                val obj = JSONObject()
                obj.put("reason", "no_ad")
                obj.put("ad_unit", "openAd")
                EventUtils.event("ad_show_error", ext = obj)
            }
        }

    }

}