package com.base.pdfviewerscannerwhite.ui.splash

import android.Manifest
import android.annotation.SuppressLint
import android.content.Intent
import android.graphics.Color
import android.os.Build
import androidx.activity.result.contract.ActivityResultContracts
import androidx.lifecycle.lifecycleScope
import com.base.pdfviewerscannerwhite.ads.AdmobHelper.initAdmobAd
import com.base.pdfviewerscannerwhite.ads.admob.AdmobOpenUtils
import com.base.pdfviewerscannerwhite.bean.ConstObject.ifAgreePrivacy
import com.base.pdfviewerscannerwhite.databinding.ActivitySplashBinding
import com.base.pdfviewerscannerwhite.helper.BaseActivity
import com.base.pdfviewerscannerwhite.ui.main.MainActivity
import com.base.pdfviewerscannerwhite.utils.BarUtils
import com.base.pdfviewerscannerwhite.utils.LogEx
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.cancel
import kotlinx.coroutines.delay
import kotlinx.coroutines.isActive
import kotlinx.coroutines.launch
import java.util.concurrent.atomic.AtomicBoolean

@SuppressLint("CustomSplashScreen")
class SplashActivity : BaseActivity<ActivitySplashBinding>(), SplashView {

    private val TAG = "SplashActivity"
    private var job: Job? = null

    private lateinit var splashPresenter: SplashPresenter

    override val binding: ActivitySplashBinding by lazy {
        ActivitySplashBinding.inflate(layoutInflater)
    }


    override fun initView() {
        BarUtils.setStatusBarLightMode(this, true)
        BarUtils.setStatusBarColor(this, Color.TRANSPARENT)
//        binding.root.updatePadding(top = BarUtils.getStatusBarHeight())
        if (Build.VERSION.SDK_INT >= 33) {
            registerForActivityResult(ActivityResultContracts.RequestPermission()) {}.launch(
                Manifest.permission.POST_NOTIFICATIONS
            )
        }
        splashPresenter = SplashPresenter(this)

        ifAgreePrivacy = true
        if (ifAgreePrivacy) {
            agreePrivacy()
        } else {

        }
    }

    override fun showAd() {
        AdmobOpenUtils.loadAppOpenAd {
            AdmobOpenUtils.showAppOpenAd(this, showBefore = {
                if (it) {
                    splashPresenter.pauseJumpJob()
                }
            }, onHidden = {
                jumpNext()
            })

        }
    }

    override fun agreePrivacy() {
        initAdmobAd()
        showAd()
        startProgressJob()
        splashPresenter.startJumpJob(lifecycleScope)
    }


    /**
     * 跑进度条,进度条不关联跳转逻辑
     */
    private fun startProgressJob() {
        if (job == null && binding.progressBar.progress != 100) {
            job = lifecycleScope.launch(Dispatchers.Main) {
                while (isActive) {
                    binding.progressBar.progress += 1
                    delay(150)
                    if (binding.progressBar.progress == 100) {
                        cancel()
                    }
                }
            }
        }
    }

    /**
     * 暂停进度条
     */
    private fun pauseProgressJob() {
        job?.cancel()
        job = null
    }

    override fun onPause() {
        super.onPause()
        pauseProgressJob()
        splashPresenter.pauseJumpJob()
    }

    override fun onResume() {
        super.onResume()
        startProgressJob()
        splashPresenter.startJumpJob(lifecycleScope)
    }


    private var jump: AtomicBoolean = AtomicBoolean(false)
    override fun jumpNext() {
        if (jump.get()) {
            return
        }
        jump.set(true)

        LogEx.logDebug(TAG, "jumpNext")
        binding.progressBar.progress = 100
        binding.root.postDelayed({
            startActivity(Intent(this, MainActivity::class.java))
            finish()
        }, 200)
    }


}