package com.zxhy.fastfilemanagerpro

import android.annotation.SuppressLint
import android.app.ActivityManager
import android.content.Intent
import android.os.Bundle
import android.util.Log
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.lifecycle.lifecycleScope
import com.google.android.gms.ads.MobileAds
import com.google.android.ump.FormError
import com.zxhy.fastfilemanagerpro.databinding.ActivityKokoLaunchBinding
import com.zxhy.fastfilemanagerpro.kokoads.GoogleMobileAdsConsentManager
import com.zxhy.fastfilemanagerpro.kokoads.KokoOpenAdManager
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import java.util.concurrent.atomic.AtomicBoolean


@SuppressLint("CustomSplashScreen")
class KokoLaunchActivity : AppCompatActivity() {

    private lateinit var binding: ActivityKokoLaunchBinding
    private val isAdsInitializeCalled = AtomicBoolean(false)
    private lateinit var googleMobileAdsConsentManager: GoogleMobileAdsConsentManager
    private lateinit var kokoOpenAdManager: KokoOpenAdManager

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        binding = ActivityKokoLaunchBinding.inflate(layoutInflater)
        setContentView(binding.root)
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            insets
        }
        kokoOpenAdManager = (application as MyApplication).kokoOpenAdManager

        val job = lifecycleScope.launch(Dispatchers.Main) {
            while (binding.ProgressBar.progress != 100) {
                binding.ProgressBar.progress += 2
                delay(50)
            }
            startActivity(Intent(this@KokoLaunchActivity, MainActivity::class.java))
            finish()
        }


        kokoOpenAdManager.adLoadComplete = { loaded ->
            if (loaded) {
                job.cancel()
                kokoOpenAdManager.showAdIfAvailable(this@KokoLaunchActivity)
            }
        }

        kokoOpenAdManager.adShowing = {
            job.cancel()
        }

        kokoOpenAdManager.adShowComplete = {
            job.cancel()
            kokoOpenAdManager.clearAllCallBack()
            startActivity(Intent(this@KokoLaunchActivity, MainActivity::class.java))
            finish()
        }

        setDebugAdsConsent {}
    }

    /**
     * 设置Debug广告同意
     */
    private fun setDebugAdsConsent(gatherConsentAction: (consentError: FormError?) -> Unit) {
        googleMobileAdsConsentManager =
            GoogleMobileAdsConsentManager.getInstance(applicationContext)

        //获取广告同意设置
        googleMobileAdsConsentManager.gatherConsent(this) { consentError ->
            gatherConsentAction.invoke(consentError)

            if (googleMobileAdsConsentManager.canRequestAds) {
                initializeMobileAdsSdk()
            }
        }

        if (googleMobileAdsConsentManager.canRequestAds) {
            initializeMobileAdsSdk()
        }

    }

    private fun initializeMobileAdsSdk() {
        if (isAdsInitializeCalled.getAndSet(true)) {
            return
        }
        MobileAds.initialize(this) {
            (application as MyApplication).kokoOpenAdManager.isInit = true
        }

        (application as MyApplication).loadAd(this)
    }

    fun printTaskActivity() {
        val activityManager = getSystemService(ACTIVITY_SERVICE) as ActivityManager
        val runningTasks = activityManager.getRunningTasks(10)
        for (taskInfo in runningTasks) {
            val componentName = taskInfo.topActivity
            val activityName = componentName!!.className
            Log.e("Activity", "Task Activity Name: $activityName")
        }
    }
}