package com.base.scanqr.ui.start

import android.content.Intent
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.lifecycleScope
import com.base.scanqr.base.BaseActivity
import com.base.scanqr.bean.ConstObject
import com.base.scanqr.bean.ConstObject.isFirstLauncher
import com.base.scanqr.databinding.ActivityStartBinding
import com.base.scanqr.ui.main.MainActivity
import com.gyf.immersionbar.ktx.immersionBar
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.delay
import kotlinx.coroutines.isActive
import kotlinx.coroutines.launch
import java.util.concurrent.atomic.AtomicBoolean

class StartActivity : BaseActivity<ActivityStartBinding>(ActivityStartBinding::inflate) {

    private val viewModel by lazy(LazyThreadSafetyMode.NONE) {
        ViewModelProvider(this)[StartViewModel::class.java]
    }

    override fun initView() {
        super.initView()

        if (ConstObject.ifAgreePrivacy) {
            agreePrivacy()
        }
    }


    private fun agreePrivacy() {
        //todo 显示广告
        viewModel.startJumpJob = true
        viewModel.startJumpJob(lifecycleScope, ::jumpNext)

        startProgressJob = true
        startProgressJob()
    }

    private var startProgressJob: Boolean = false
    private var progressJob: Job? = null

    private fun startProgressJob() {
        if (ConstObject.ifAgreePrivacy && startProgressJob) {
            if (progressJob == null) {
                progressJob = lifecycleScope.launch(Dispatchers.Main) {
                    while (isActive) {
                        binding.progressBar.progress += 1
                        delay(200)
                    }
                }
            }
        }
    }

    private fun cancelProgressJob() {
        progressJob?.cancel()
        progressJob = null
    }

    private var jump: AtomicBoolean = AtomicBoolean(false)
    private fun jumpNext() {
        if (jump.get()) {
            return
        }
        jump.set(true)
        binding.progressBar.progress = 100

        binding.root.postDelayed({
            if (isFirstLauncher) {
                isFirstLauncher = false
                firstLauncherJump()
            } else {
                handleActionIdJump()
            }
        }, 100)
    }

    private fun handleActionIdJump() {
        startActivity(Intent(this, MainActivity::class.java).apply {
        })
    }

    private fun firstLauncherJump() {
        startActivity(Intent(this, MainActivity::class.java).apply {
        })
    }


    override fun initListener() {
        super.initListener()
    }

    override fun configSystemBar() {
        immersionBar {
            statusBarColor("#00FFFFFF")
            statusBarDarkFont(false)
//            navigationBarDarkIcon(true)
//            fitsSystemWindows(true)
//            navigationBarColor("#FFFFFFFF")
        }
    }

    override fun onResume() {
        super.onResume()
        viewModel.startJumpJob(lifecycleScope, ::jumpNext)
        startProgressJob()
    }

    override fun onPause() {
        super.onPause()
        viewModel.cancelJumpJob()
        cancelProgressJob()
    }
}