package com.base.locationsharewhite.ui.splash

import android.annotation.SuppressLint
import android.content.Intent
import android.graphics.Color
import androidx.lifecycle.lifecycleScope
import com.base.locationsharewhite.R
import com.base.locationsharewhite.bean.ConstObject.ifAgreePrivacy
import com.base.locationsharewhite.bean.ConstObject.isFirstLauncher
import com.base.locationsharewhite.databinding.ActivitySplashBinding
import com.base.locationsharewhite.fcm.NotificationHoverUtils
import com.base.locationsharewhite.helper.BaseActivity
import com.base.locationsharewhite.service.StayNotificationService.Companion.startStayNotification
import com.base.locationsharewhite.ui.howuse.HowUseActivity
import com.base.locationsharewhite.ui.main.MainActivity
import com.base.locationsharewhite.ui.set.LanguageActivity
import com.base.locationsharewhite.utils.BarUtils
import com.base.locationsharewhite.utils.LogEx
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.delay
import kotlinx.coroutines.isActive
import kotlinx.coroutines.launch
import java.util.Locale
import java.util.concurrent.atomic.AtomicBoolean

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

    private val TAG = "SplashActivity"

    private lateinit var splashPresenter: SplashPresenter
    private var progressJob: Job? = null

    override val binding: ActivitySplashBinding by lazy {
        ActivitySplashBinding.inflate(layoutInflater)
    }
    private var actionId = ""

    override fun initView() {
        BarUtils.setStatusBarLightMode(this, true)
        BarUtils.setStatusBarColor(this, Color.TRANSPARENT)
        //        binding.root.updatePadding(top = BarUtils.getStatusBarHeight())
        splashPresenter = SplashPresenter(this)
        startStayNotification()

        actionId = intent.extras?.getString("actionId") ?: ""
        LogEx.logDebug(TAG, "actionId=$actionId")
        if (actionId.isNotEmpty()) {
            NotificationHoverUtils.stopNotificationHandler()
        }


        if (ifAgreePrivacy) {
            agreePrivacy()
        }
    }

    override fun showAd() {

    }

    override fun agreePrivacy() {
        splashPresenter.startJumpJob = true
        splashPresenter.startJumpJob(lifecycleScope)

        startProgressJob = true
        startProgressJob()
    }

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

        binding.progressbar.progress = 100

        binding.root.postDelayed({
            if (isFirstLauncher) {
                isFirstLauncher = false
                firstLauncherJump()
            } else {
                startActivity(Intent(this, MainActivity::class.java).apply {
                    putExtra("actionId", actionId)
                })
                finish()
            }


        }, 100)
    }

    private fun firstLauncherJump() {
        if (Locale.getDefault().language + "_" + Locale.getDefault().country != Locale.ENGLISH.language + "_" + Locale.ENGLISH.country) {
            startActivity(Intent(this, LanguageActivity::class.java).apply {
                putExtra("isGuide", true)
            })
        } else {
            startActivity(Intent(this, HowUseActivity::class.java).apply {
                putExtra("isGuide", true)
            })
        }
        finish()
    }

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

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

    private var startProgressJob: Boolean = false

    private fun startProgressJob() {
        if (ifAgreePrivacy && startProgressJob) {
            if (progressJob == null) {
                progressJob = lifecycleScope.launch(Dispatchers.Main) {
                    while (isActive) {
                        binding.progressbar.progress += 1
                        binding.tvTip.text = resources.getString(R.string.moment_please_s, binding.progressbar.progress)
                        delay(200)
                    }
                }
            }
        }
    }

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

}