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

@SuppressLint("CustomSplashScreen")
class Splash2Activity : BaseActivity<ActivitySplashBinding>(),
    PrivacyManager.OnUserPrivacyAgreementListener, 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()
        }
        mTaskManager?.startProgress()
        loadAd()
    }


    override fun onProgressMax() {
        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
    }


}