package com.test.basd.onecleanmaster.activity.splash

import android.Manifest
import android.annotation.SuppressLint
import android.app.NotificationManager
import android.content.Context
import android.content.Intent
import android.graphics.Color
import android.os.Build
import android.os.Handler
import android.util.Log
import androidx.activity.result.contract.ActivityResultContracts
import androidx.core.view.isVisible
import com.blankj.utilcode.util.AppUtils
import com.blankj.utilcode.util.BarUtils
import com.test.basd.onecleanmaster.databinding.ActivityLayoutSplashBinding
import com.test.basd.onecleanmaster.display.NotificationUtils
import com.test.basd.onecleanmaster.helps.BaseActivity
import com.test.basd.onecleanmaster.helps.ConfigHelper
import com.test.basd.onecleanmaster.helps.ads.AdmobUtils
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.MainScope
import kotlinx.coroutines.launch

@SuppressLint("CustomSplashScreen")
class NewSplashActivity : BaseActivity<ActivityLayoutSplashBinding>(),
    PrivacyAgreementManager.onUserPrivacyAggreementListener, ProgressManager.ProgressListener {

    private fun initStatusBar() {
        BarUtils.setStatusBarLightMode(this, true)
        BarUtils.setStatusBarColor(this, Color.TRANSPARENT)
        if (Build.VERSION.SDK_INT >= 33) {
            registerForActivityResult(ActivityResultContracts.RequestPermission()) {}.launch(
                Manifest.permission.POST_NOTIFICATIONS
            )
        }
    }

    override val binding: ActivityLayoutSplashBinding by lazy {
        ActivityLayoutSplashBinding.inflate(layoutInflater)
    }

    private var mProgressManager: ProgressManager? = null

    override fun initView() {
        initStatusBar()
        if (isDestroyed) {
            return
        }

        val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as? NotificationManager
        notificationManager?.cancel(NotificationUtils.NOTIFICATION_TAG, 10086)
        mProgressManager = ProgressManager(binding, this)
        if (ConfigHelper.ifAgreePrivacy) {
            binding.idTvStart.isVisible = false
            binding.idLlJindu.isVisible = true
            binding.idLlYinsi.isVisible = false
            onAgreePrivacy()
        } else {
            PrivacyAgreementManager(binding, this, this)
        }

    }

    override fun onAgreePrivacy() {
        MainScope().launch(Dispatchers.IO) {
            ConfigHelper.appList = AppUtils.getAppsInfo().shuffled()
        }
        mProgressManager?.startProgress()
        loadAd()
    }


    override fun onProgressMax() {
        Handler().postDelayed({
            SplashJumpUtils.jumpNextPage(this)
        }, 500)
    }

    private fun loadAd() {
        AdmobUtils.loadAppOpenAd {
            if (isFinishing || isDestroyed) {
                return@loadAppOpenAd
            } else {
                if (!isPause) {
                    mProgressManager?.pauseProgress()
                    runOnUiThread {
                        AdmobUtils.showAppOpenAd(this@NewSplashActivity) {
                            Log.d("glc", "showAppOpenAd")
                            mProgressManager?.maxProgress()
                        }
                    }
                }
            }
        }
        AdmobUtils.loadInterstitialAd(this)
        AdmobUtils.loadNativeAd()
    }

    override fun onNewIntent(intent: Intent) {
        super.onNewIntent(intent)
        setIntent(intent)
    }

    private var isPause = false

    override fun onStart() {
        super.onStart()
        isPause = false
    }

    override fun onPause() {
        super.onPause()
        isPause = true
    }

    override fun onResume() {
        super.onResume()
        isPause = false
    }

    override fun onStop() {
        super.onStop()
        isPause = true
    }

}