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.ads.AdsMgr
import com.base.locationsharewhite.ads.AdsShowCallBack
import com.base.locationsharewhite.bean.ConstObject.ifAgreePrivacy
import com.base.locationsharewhite.bean.ConstObject.isFirstLauncher
import com.base.locationsharewhite.databinding.ActivitySplashBinding
import com.base.locationsharewhite.helper.BaseActivity
import com.base.locationsharewhite.helper.config.ConstConfig
import com.base.locationsharewhite.notification.NotificationHoverUtils
import com.base.locationsharewhite.service.StayNotificationService.Companion.startStayNotification
import com.base.locationsharewhite.ui.howuse.HowUseActivity
import com.base.locationsharewhite.ui.main.LocationCodeActivity
import com.base.locationsharewhite.ui.main.LocationShareActivity
import com.base.locationsharewhite.ui.main.MainActivity
import com.base.locationsharewhite.ui.set.SettingActivity
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: Int? = null

    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?.getInt("actionId")
        LogEx.logDebug(TAG, "actionId=$actionId")
        if (actionId != null) {
            NotificationHoverUtils.stopNotificationHandler()
        }

        if (ifAgreePrivacy) {
            agreePrivacy()
        }
    }

    override fun showAd() {

    }

    override fun agreePrivacy() {

        AdsMgr.showOpen(this, object : AdsShowCallBack() {
            override fun show() {
                LogEx.logDebug(TAG, "AdsShowCallBack show")
                splashPresenter.cancelJumpJob()
                cancelProgressJob()
            }

            override fun close() {
                LogEx.logDebug(TAG, "AdsShowCallBack close")
                jumpNext()
            }

            override fun failed() {
                LogEx.logDebug(TAG, "AdsShowCallBack failed")
                jumpNext()
            }

            override fun googleFailed() {
                LogEx.logDebug(TAG, "AdsShowCallBack googleFailed")
                jumpNext()
            }

        })

        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 {
                handleActionIdJump()
            }
        }, 100)
    }

    override fun onNewIntent(intent: Intent?) {
        super.onNewIntent(intent)
        handleActionIdJump()
    }

    private fun handleActionIdJump() {
        actionId = intent.extras?.getInt("actionId")
        LogEx.logDebug(TAG, "handleActionIdJump actionId=$actionId")

        when (actionId) {
            ConstConfig.ACTION_STAY_HOME -> {
                startActivity(Intent(this, MainActivity::class.java).apply {
                    putExtra("actionId", actionId)
                })
            }

            ConstConfig.ACTION_STAY_MY_CODE -> {
                startActivity(Intent(this, LocationCodeActivity::class.java).apply {
//                    putExtra("actionId", actionId)
                })
            }

            ConstConfig.ACTION_STAY_SHARE -> {
                startActivity(Intent(this, LocationShareActivity::class.java).apply {
//                    putExtra("actionId", actionId)
                })
            }

            ConstConfig.ACTION_STAY_SETTING -> {
                startActivity(Intent(this, SettingActivity::class.java).apply {
//                    putExtra("actionId", actionId)
                })
            }

            ConstConfig.ACTION_SHARE_LOCATION -> {
                startActivity(Intent(this, LocationShareActivity::class.java).apply {})
            }

            ConstConfig.ACTION_ENABLE_LOCATION -> {
                startActivity(Intent(this, MainActivity::class.java).apply {
                    putExtra("actionId", actionId)
                })
            }

            ConstConfig.ACTION_COPY_CODE -> {
                startActivity(Intent(this, LocationCodeActivity::class.java).apply {
                    putExtra("actionId", actionId)
                })
            }

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

        }

        finish()
        intent.extras?.clear()
    }

    private fun firstLauncherJump() {
        if (Locale.getDefault().language + "_" + Locale.getDefault().country != Locale.ENGLISH.language + "_" + Locale.ENGLISH.country) {
            startActivity(Intent(this, HowUseActivity::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
    }

}