package com.test.mydemo3

import android.app.Application
import android.content.Context
import android.util.Log
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleObserver
import androidx.lifecycle.OnLifecycleEvent
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.sdk.AppLovinSdk
import com.applovin.sdk.AppLovinSdkConfiguration
import com.cloud.cleanjunksdk.task.CheckSdkCallback
import com.cloud.cleanjunksdk.task.Clean
import com.cloud.cleanjunksdk.task.CleanSDK
import com.cloud.cleanjunksdk.tools.Region
import com.test.mydemo3.util.UrlManager
import com.trustlook.sdk.cloudscan.CloudScanClient

class MyApplication : Application() {
    private lateinit var appOpenManager: ExampleAppOpenManager

    companion object{
        var mCleanSdk: Clean? = null
        var mCloudScan: CloudScanClient? = null
    }

    override fun onCreate() {
        super.onCreate()

//        AppLovinSdk.getInstance( this ).initializeSdk({ configuration: AppLovinSdkConfiguration ->
//            {
//                appOpenManager = ExampleAppOpenManager(applicationContext)
//            }
//            )
//        }
        CleanSDK.init(MyApplication(), this, Region.INTL, object : CheckSdkCallback {
            override fun onSuccess(p0: Clean?) {
                mCleanSdk = p0!!
                Log.e("yanglin", "onSuccess: clean INIT", )
            }

            override fun onError(p0: Int) {
                Log.e("yanglin", "onError: INIT", )
            }
        })
        mCloudScan = CloudScanClient.Builder(this)
            .setRegion(com.trustlook.sdk.data.Region.INTL)
            .setConnectionTimeout(30000)
            .setSocketTimeout(30000)
            .build()
        AppLovinSdk.getInstance(this).initializeSdk {
            appOpenManager = ExampleAppOpenManager(applicationContext)
        }
    }

    class ExampleAppOpenManager(applicationContext: Context?) : LifecycleObserver, MaxAdListener {
        private lateinit var appOpenAd: MaxAppOpenAd
        private lateinit var context: Context

        init {
            ProcessLifecycleOwner.get().lifecycle.addObserver(this)

            context = applicationContext!!

            appOpenAd = MaxAppOpenAd(UrlManager.AD_UNIT_ID, applicationContext!!)
            appOpenAd.setListener(this)
            appOpenAd.loadAd()
        }

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

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

        @OnLifecycleEvent(Lifecycle.Event.ON_START)
        fun onStart() {
            showAdIfReady()
        }

        override fun onAdLoaded(ad: MaxAd) {}
        override fun onAdLoadFailed(adUnitId: String, error: MaxError) {}
        override fun onAdDisplayed(ad: MaxAd) {}
        override fun onAdClicked(ad: MaxAd) {}

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

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