package com.base.easyfilemanager.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.base.easyfilemanager.databinding.ActivityLayoutSplashBinding
import com.base.easyfilemanager.display.CloseNotificationReceiver
import com.base.easyfilemanager.display.NotificationHelper
import com.base.easyfilemanager.display.NotificationService
import com.base.easyfilemanager.helps.BaseActivity
import com.base.easyfilemanager.helps.ConfigHelper
import com.base.easyfilemanager.helps.LogEx
import com.base.easyfilemanager.helps.ads.AdmobUtils
import com.base.easyfilemanager.utils.BarUtils
import com.base.easyfilemanager.utils.SPUtils

@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() {
        val jumpType = intent.getIntExtra("type", 0)
        if (jumpType == 0) {
            startNotification()
        }
        mProgressManager?.startProgress()
        if (!ConfigHelper.ifGuest) {
            mProgressManager?.pauseProgress()
            onProgressMax()
            return
        } else {
            loadAd()
        }
    }

    private fun startNotification() {
        val intent = Intent(this, NotificationService::class.java)
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            startForegroundService(intent)
        } else {
            startService(intent)
        }
    }

    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
            val actionS = SPUtils.getInstance().getInt("notification_actionS", 0)
            if(actionS==1){
                cancelIntent.putExtra("notificationId", jumpType)
            }else{
                cancelIntent.putExtra("notificationId", NotificationHelper.notificationId)
            }
            sendBroadcast(cancelIntent)
        }
    }
}