package com.base.scanqr.ui.start

import android.content.Intent
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.lifecycleScope
import com.base.scanqr.ads.AdsMgr
import com.base.scanqr.ads.AdsShowCallBack
import com.base.scanqr.base.BaseActivity
import com.base.scanqr.bean.ConstObject
import com.base.scanqr.bean.ConstObject.isFirstLauncher
import com.base.scanqr.bean.NotificationSendBean.Companion.ACTION_ID_EMAIL
import com.base.scanqr.bean.NotificationSendBean.Companion.ACTION_ID_SCAN
import com.base.scanqr.bean.NotificationSendBean.Companion.ACTION_ID_STAY_SCAN
import com.base.scanqr.bean.NotificationSendBean.Companion.ACTION_ID_TEXT
import com.base.scanqr.bean.NotificationSendBean.Companion.ACTION_ID_WEBSITE
import com.base.scanqr.bean.NotificationSendBean.Companion.ACTION_ID_WIFI
import com.base.scanqr.databinding.ActivityStartBinding
import com.base.scanqr.fcm.notification.NotificationHoverUtils
import com.base.scanqr.service.StayJobService.Companion.startJob
import com.base.scanqr.ui.email.EmailActivity
import com.base.scanqr.ui.language.LanguageActivity
import com.base.scanqr.ui.main.MainActivity
import com.base.scanqr.ui.text.TextActivity
import com.base.scanqr.ui.website.WebsiteActivity
import com.base.scanqr.ui.wifi.WifiActivity
import com.base.scanqr.utils.LogEx
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()

        NotificationHoverUtils.stopNotificationHandler()

        if (ConstObject.ifAgreePrivacy) {
            agreePrivacy()
        }

        startJob()
    }


    private fun agreePrivacy() {

        AdsMgr.showOpen(this, false, object : AdsShowCallBack() {
            override fun show() {
                super.show()
                viewModel.cancelJumpJob()
                cancelProgressJob()
            }

            override fun close(where: Int) {
                jumpNext()
            }

            override fun failed(where: Int) {
                jumpNext()
            }

            override fun googleFailed(where: Int) {
                jumpNext()
            }
        })
        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() {
        val actionId = intent.extras?.getString("actionId")
        LogEx.logDebug(TAG, "actionId=$actionId")
        when (actionId) {
            ACTION_ID_STAY_SCAN, ACTION_ID_SCAN -> {
                LogEx.logDebug(TAG, "actionId MainActivity")
                startActivity(Intent(this, MainActivity::class.java).apply {
                    putExtra("actionId", actionId)
                    addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT)
                })
            }

            ACTION_ID_WIFI -> {
                startActivity(Intent(this, WifiActivity::class.java).apply {
//                    putExtra("actionId", actionId)
//                    addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT)
                })
            }

            ACTION_ID_TEXT -> {
                startActivity(Intent(this, TextActivity::class.java).apply {
//                    putExtra("actionId", actionId)
//                    addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT)
                })
            }

            ACTION_ID_EMAIL -> {
                startActivity(Intent(this, EmailActivity::class.java).apply {
//                    putExtra("actionId", actionId)
//                    addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT)
                })
            }

            ACTION_ID_WEBSITE -> {
                startActivity(Intent(this, WebsiteActivity::class.java).apply {
//                    putExtra("actionId", actionId)
//                    addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT)
                })
            }

            else -> {
                startActivity(Intent(this, MainActivity::class.java).apply {
                    putExtra("actionId", actionId)
                    addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT)
                })
            }
        }
        finish()
    }

    private fun firstLauncherJump() {
        startActivity(Intent(this, LanguageActivity::class.java).apply {
            putExtra("isGuide", true)
        })
        finish()
    }

    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()
    }

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