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 android.view.KeyEvent
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
import com.base.filerecoveryrecyclebin.utils.EventUtils

@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 fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            EventUtils.event("back")
        }
        return super.onKeyDown(keyCode, event)
    }


    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()
        } 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() {
        EventUtils.event("app_start")
        if (jumpType == 0) {
//            startStayNotification()
        }
        AdmobMaxHelper.preloadAd(this)
        mTaskManager?.startProgress()
        loadAd()
    }

private var isMaxComplete=false
    override fun onProgressMax() {
        if(isMaxComplete)return
        isMaxComplete=true
        Handler().postDelayed({
            StartUtils.jumpNextPage(this)
        }, 500)
    }

    private fun loadAd() {
        AdmobMaxHelper.admobMaxShowOpenAd(this, {
            mTaskManager?.pauseProgress()
        }, {
            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
        EventUtils.event("onPause")
    }

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

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


}