package com.zxhyis.tools.ad

import android.app.Activity
import android.content.Context
import androidx.lifecycle.DefaultLifecycleObserver
import androidx.lifecycle.LifecycleObserver
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.ProcessLifecycleOwner
import com.applovin.mediation.MaxAd
import com.applovin.mediation.MaxAdListener
import com.applovin.mediation.MaxError
import com.applovin.mediation.ads.MaxAppOpenAd
import com.applovin.mediation.ads.MaxInterstitialAd
import com.applovin.sdk.AppLovinSdk
import com.zxhyis.tools.logic.testLog
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import java.util.concurrent.TimeUnit


class CommonOpenManager(adId:String,appContext: Context?) :DefaultLifecycleObserver{
    private lateinit var appOpenAd: MaxAppOpenAd
    private lateinit var context: Context

    init {
        ProcessLifecycleOwner.get().lifecycle.addObserver(this)
        context = appContext!!
        appOpenAd = MaxAppOpenAd(adId, appContext)
        appOpenAd.setListener(object : MaxAdListener{
            override fun onAdLoaded(ad: MaxAd?) {

            }

            override fun onAdDisplayed(ad: MaxAd?) {

            }

            override fun onAdHidden(ad: MaxAd?) {
                appOpenAd.loadAd()
            }

            override fun onAdClicked(ad: MaxAd?) {

            }

            override fun onAdLoadFailed(adUnitId: String?, error: MaxError?) {

            }

            override fun onAdDisplayFailed(ad: MaxAd?, error: MaxError?) {
                appOpenAd.loadAd()
            }

        })
        appOpenAd.loadAd()
    }

    private fun showAdIfReady() {
        if (appOpenAd == null || !AppLovinSdk.getInstance(context).isInitialized) return

        if (appOpenAd.isReady)
        {
            appOpenAd.showAd()
        }
        else
        {
            appOpenAd.loadAd()
        }
    }

    override fun onStart(owner: LifecycleOwner) {
        super.onStart(owner)
        showAdIfReady()
    }



}


fun preLoadOpenAd(
) {


}


fun preLoadInterAd(
    maxId: String,
    activity: Activity,
    isShow: Boolean = false,
    isLimitCount: Boolean = false,
    isMoment: Boolean = false,
    loaded: (ad:MaxInterstitialAd?) -> Unit,
    loadFailed: () -> Unit,
    displayFailed: () -> Unit,
    clicked: () -> Unit,
    closed: () -> Unit,
    scope: CoroutineScope
) {
    val currentTime = System.currentTimeMillis()
    var retryAttempt = 0.0
    val maxAd = MaxInterstitialAd(maxId, activity)
    maxAd.setListener(object : MaxAdListener {
        override fun onAdLoaded(ad: MaxAd?) {
            testLog("ad loaded")
            retryAttempt = 0.0
            loaded(maxAd)
        }

        override fun onAdDisplayed(ad: MaxAd?) {
            testLog("ad displayed")
        }

        override fun onAdHidden(ad: MaxAd?) {
            testLog("ad closed")
            closed()
        }

        override fun onAdClicked(ad: MaxAd?) {
            testLog("ad clicked")
            clicked()
        }

        override fun onAdLoadFailed(adUnitId: String?, error: MaxError?) {
            testLog("ad failed")
            loadFailed()
            retryAttempt++
            scope.launch {
                val delayMillis =
                    TimeUnit.SECONDS.toMillis(Math.pow(2.0, Math.min(6.0, retryAttempt)).toLong())
                delay(delayMillis)
                maxAd.loadAd()
            }

        }

        override fun onAdDisplayFailed(ad: MaxAd?, error: MaxError?) {
            testLog("ad displayed")
            displayFailed()
        }

    })
    maxAd.loadAd()

}





