package com.base.filerecoveryrecyclebin.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 androidx.activity.result.contract.ActivityResultContracts
import com.base.filerecoveryrecyclebin.MyApplication
import com.base.filerecoveryrecyclebin.ads.AdmobMaxHelper
import com.base.filerecoveryrecyclebin.bean.ConstObject.ifAgreePrivacy
import com.base.filerecoveryrecyclebin.databinding.ActivitySplashBinding
import com.base.filerecoveryrecyclebin.fcm.CloseNotificationReceiver
import com.base.filerecoveryrecyclebin.fcm.NotificationUtil
import com.base.filerecoveryrecyclebin.help.BaseActivity
import com.base.filerecoveryrecyclebin.service.StayNotificationService.Companion.startStayNotification
import com.base.filerecoveryrecyclebin.utils.BarUtils

@SuppressLint("CustomSplashScreen")
class Splash2Activity : BaseActivity<ActivitySplashBinding>(),
    PrivacyManager.onUserPrivacyAggreementListener, TaskManager.ProgressListener {
    private val TAG = "NewStartActivity"
    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: ActivitySplashBinding by lazy {
        ActivitySplashBinding.inflate(layoutInflater)
    }

    private var mTaskManager: TaskManager? = null
    var jumpType = 0

    override fun initView() {
        initStatusBar()
        if (isDestroyed) {
            return
        }
        if (MyApplication.isInterOpenShowing) {
            finish()
            return
        }

        NotificationUtil.stopNotificationHandler()
        jumpType = intent.getIntExtra("actionId", 0)
        closeNotification()
        mTaskManager = TaskManager(binding, this)
        if (ifAgreePrivacy) {
            onAgreePrivacy()
            //  EventUtils.event("app_start")
        } else {
            PrivacyManager(binding, this, this)
        }

    }
    private fun closeNotification() {
        sendBroadcast(Intent(this, CloseNotificationReceiver::class.java).apply {
            this.action = CloseNotificationReceiver.Action
            this.putExtra(CloseNotificationReceiver.NotificationId, jumpType)
        })
    }

    override fun onAgreePrivacy() {
        if (jumpType == 0) {
            startStayNotification()
        }
        mTaskManager?.startProgress()
        loadAd()
//        if (!ConfigHelper.ifGuest) {
//            mTaskManager?.pauseProgress()
//            onProgressMax()
//            return
//        } else {
//            loadAd()
//        }

    }


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

    private fun loadAd() {
//        AdmobMaxHelper.admobMaxShowOpenAd(this){
//
//        }
            AdmobMaxHelper.admobMaxShowOpenAd(this,{
                mTaskManager?.pauseProgress()
            },{
                if (!isPause) {
                    mTaskManager?.pauseProgress()
                    runOnUiThread {
                        mTaskManager?.maxProgress()
                    }
                }
            })
    }

    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
    }


}