package com.base.datarecovery.activity

import android.annotation.SuppressLint
import android.content.Intent
import android.graphics.Color
import android.net.Uri
import android.text.SpannableString
import android.text.Spanned
import android.text.style.UnderlineSpan
import android.view.View
import androidx.lifecycle.lifecycleScope
import com.base.datarecovery.ads.AdmobNativeUtils
import com.base.datarecovery.ads.AdmobOpenUtils
import com.base.datarecovery.databinding.ActivitySplashBinding
import com.base.datarecovery.help.BaseActivity
import com.base.datarecovery.help.ConfigHelper
import com.base.datarecovery.utils.BarUtils
import com.base.datarecovery.utils.LogEx
import com.base.datarecovery.utils.SPUtils
import kotlinx.coroutines.Job
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.SharedFlow
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.isActive
import kotlinx.coroutines.launch
import kotlin.random.Random

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

    private val TAG = "SplashActivity"
    private var job: Job? = null
    private val progress = MutableSharedFlow<Int>()
    private val progressFlow: SharedFlow<Int> = progress
    private var oneClickStart: Boolean = false
    var ifAgreePrivacy = false
        get() {
            return SPUtils.getInstance().getBoolean("ifAgreePrivacy", field)
        }
        set(value) {
            field = value
            SPUtils.getInstance().put("ifAgreePrivacy", value, true)
        }

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

    override fun initView() {
        BarUtils.setStatusBarLightMode(this, true)
        BarUtils.setStatusBarColor(this, Color.TRANSPARENT)

        jumpNext()

        if (ifAgreePrivacy) {
            startProgress()
            binding.llStart.visibility = View.GONE
            binding.llProgress.visibility = View.VISIBLE
        } else {
            binding.llStart.visibility = View.VISIBLE
            binding.llProgress.visibility = View.GONE
        }

        val spannableString = SpannableString("Privacy Policy")
        spannableString.setSpan(
            UnderlineSpan(),
            0,
            spannableString.length,
            Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
        )
        binding.idTvPrivacyPolicy.text = spannableString

        binding.idTvPrivacyPolicy.setOnClickListener {
            val intent = Intent(
                Intent.ACTION_VIEW,
                Uri.parse(ConfigHelper.privacyPolicy)
            )
            startActivity(intent)
        }

        AdmobNativeUtils.loadNativeAd()
        AdmobOpenUtils.loadAppOpenAd {
            LogEx.logDebug(TAG, "load where=$it")
        }
    }

    private fun jumpNext() {
        lifecycleScope.launch {
            progressFlow.collectLatest {
                if (it >= 100) {
                    job?.cancel()
                    startActivity(Intent(this@SplashActivity, MainActivity::class.java))
                    finish()
                }
            }
        }
    }

    override fun initListener() {

        binding.idTvStart.setOnClickListener {
            if (oneClickStart) {
                return@setOnClickListener
            }
            oneClickStart = true
            ifAgreePrivacy = true
            binding.llStart.visibility = View.GONE
            binding.llProgress.visibility = View.VISIBLE
            startProgress()
        }
    }

    var processTime = 0L
    private fun startProgress() = lifecycleScope.launch {
        while (isActive) {

            val value = binding.pb.progress + Random.nextInt(3, 5)
            binding.pb.setProgress(value, true)
            progress.emit(value)

            val delayTime = Random.nextLong(300, 550)
            delay(delayTime)
            processTime += delayTime
            if (processTime >= 3000) {
                AdmobOpenUtils.showAppOpenAd(this@SplashActivity) {
                    LogEx.logDebug(TAG, "where $it")
                    binding.pb.progress = 100
                    startActivity(Intent(this@SplashActivity, MainActivity::class.java))
                    finish()
                }
            }
        }
    }

    override fun onResume() {
        super.onResume()
        if (ifAgreePrivacy) {
            job = startProgress()
        }
    }

    override fun onPause() {
        super.onPause()
        job?.cancel()
    }

}