package com.base.superfilemanager.activity.splash

import android.Manifest
import android.annotation.SuppressLint
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.base.superfilemanager.databinding.ActivityLayoutSplashBinding
import com.base.superfilemanager.display.CloseNotificationReceiver
import com.base.superfilemanager.display.NotificationHelper
import com.base.superfilemanager.helps.BaseActivity
import com.base.superfilemanager.helps.ConfigHelper
import com.base.superfilemanager.helps.LogEx
import com.base.superfilemanager.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 val TAG = "NewSplashActivity"
    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() {
        cancelNotification()
        initStatusBar()
        if (isDestroyed) {
            return
        }

        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
    }

    private fun cancelNotification() {
        var jumpType = intent.getIntExtra("type", 0)
        if (jumpType == 0) {
            val uri = intent.data
            val str = (uri?.getQueryParameter("type") ?: "0")
            jumpType = str.toIntOrNull() ?: 0
        }
        LogEx.logDebug(TAG, "jumpType=$jumpType")
        if (jumpType != 0) {
            val cancelIntent = Intent(this, CloseNotificationReceiver::class.java)
            cancelIntent.action = CloseNotificationReceiver.action
            cancelIntent.putExtra("notificationId", NotificationHelper.notificationId)
            sendBroadcast(cancelIntent)
        }
    }
}