package com.xm.test.myfilemaster

import android.annotation.SuppressLint
import android.content.Intent
import android.os.Bundle
import android.os.Handler
import android.widget.ProgressBar
import androidx.appcompat.app.AppCompatActivity
import com.applovin.mediation.MaxAd
import com.applovin.mediation.MaxAdListener
import com.applovin.mediation.MaxError
import com.applovin.mediation.ads.MaxInterstitialAd
import com.applovin.sdk.AppLovinMediationProvider
import com.applovin.sdk.AppLovinSdk
import com.applovin.sdk.AppLovinSdkInitializationConfiguration
import com.kongzue.dialogx.DialogX
import com.xm.test.myfilemaster.activity.HomeActivity
import com.xm.test.myfilemaster.util.UrlManager
import java.util.concurrent.TimeUnit


class MainActivity : AppCompatActivity(), MaxAdListener {
    private lateinit var mProgressBar: ProgressBar

    private lateinit var interstitialAd: MaxInterstitialAd
    private var retryAttempt = 0.0

    @SuppressLint("SuspiciousIndentation")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        DialogX.init(this)
        mProgressBar = findViewById(R.id.main_page_progress)

        initView()
    }

    private fun initView() {


        // Create the initialization configuration
        // Perform any additional configuration/setting changes
        val initConfig = AppLovinSdkInitializationConfiguration.builder(UrlManager.AD_APP_KEY, this)
            .setMediationProvider(AppLovinMediationProvider.MAX)
            .build();
        val handler = Handler()
        var count: Int = 0
        val runnable = object : Runnable {
            override fun run() {
                if (count <= 100) {
                    runOnUiThread {
                        mProgressBar.setProgress(count)
                        count += 10
                    }
                    handler.postDelayed(this, 300)
                } else {
                    startVi()
                }
            }
        }
        AppLovinSdk.getInstance(this).initialize(initConfig) {
            // Start loading ads
            handler.postDelayed(runnable, 300)
        }
    }

    fun startVi() {
        startActivity(Intent(this, HomeActivity::class.java))
        finish()
    }

    fun createInterstitialAd() {
        interstitialAd = MaxInterstitialAd(UrlManager.AD_UNIT_ID, this)
        interstitialAd.setListener(this)

        // Load the first ad
        interstitialAd.loadAd()
    }

    // MAX Ad Listener
    override fun onAdLoaded(maxAd: MaxAd) {
        // Interstitial ad is ready to be shown. interstitialAd.isReady() will now return 'true'

        // Reset retry attempt
        retryAttempt = 0.0
    }

    override fun onAdLoadFailed(adUnitId: String, error: MaxError) {
        // Interstitial ad failed to load
        // AppLovin recommends that you retry with exponentially higher delays up to a maximum delay (in this case 64 seconds)

        retryAttempt++
        val delayMillis =
            TimeUnit.SECONDS.toMillis(Math.pow(2.0, Math.min(6.0, retryAttempt)).toLong())

        Handler().postDelayed({ interstitialAd.loadAd() }, delayMillis)
    }

    override fun onAdDisplayFailed(ad: MaxAd, error: MaxError) {
        // Interstitial ad failed to display. AppLovin recommends that you load the next ad.
        interstitialAd.loadAd()
    }

    override fun onAdDisplayed(maxAd: MaxAd) {}

    override fun onAdClicked(maxAd: MaxAd) {}

    override fun onAdHidden(maxAd: MaxAd) {
        // Interstitial ad is hidden. Pre-load the next ad
        interstitialAd.loadAd()
    }

}