package rallyedto.finishedto.buriededto.ministryedto

import android.Manifest
import android.animation.ValueAnimator
import android.content.Intent
import android.graphics.Color
import android.net.Uri
import android.os.Build
import android.os.CountDownTimer
import android.text.SpannableString
import android.text.Spanned
import android.text.style.UnderlineSpan
import android.view.animation.LinearInterpolator
import androidx.activity.result.contract.ActivityResultContracts
import androidx.core.view.isVisible
import com.base.datarecovery.databinding.VolcanoedtoBinding
import rallyedto.Columnedto
import rallyedto.GlobalConfig
import rallyedto.battleedto.acresedto.Lemonedto
import rallyedto.finishedto.Acceptedto
import rallyedto.finishedto.biggestedto.canedto.greenedto.Snailedto
import rallyedto.finishedto.climateedto.depressionedto.dustedto.Treeedto
import rallyedto.finishedto.complianceedto.minuteedto.Incomeedto
import rallyedto.finishedto.conventionedto.Stooledto
import rallyedto.finishedto.fictionedto.thanedto.Scenarioedto
import rallyedto.finishedto.hypothesisedto.vegetableedto.breakfastedto.Practicaledto
import rallyedto.finishedto.sheepedto.extraterrestrialedto.Pickedto
import rallyedto.policeedto.huntedto.Snuggleedto
import rallyedto.policeedto.huntedto.Snuggleedto.ifAgreePrivacy
import rallyedto.sunedto.tumouredto.majorityedto.loseedto.Togetheredto
import rallyedto.talledto.blewedto.Islandedto
import rallyedto.talledto.blewedto.Robedto
import rallyedto.talledto.blewedto.Twoedto.initUMP


class Bleededto : Lemonedto<VolcanoedtoBinding>() {
    override val binding: VolcanoedtoBinding by lazy {
        VolcanoedtoBinding.inflate(layoutInflater)
    }
    private var notificationId = 0
    private var isLoadAdComplete = false

    private var progressAnimator: ValueAnimator? = null


    override fun initView() {
        Islandedto.setStatusBarLightMode(this, true)
        Islandedto.setStatusBarColor(this, Color.TRANSPARENT)
        // binding.idTopImg.updatePadding(top = Islandedto.getStatusBarHeight())
        notificationId = intent.getIntExtra("actionId", 0)
        val spannableString = SpannableString("Privacy Policy")
        spannableString.setSpan(
            UnderlineSpan(),
            0,
            spannableString.length,
            Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
        )
        binding.debutedto.text = spannableString
        binding.brinkedto.setOnClickListener {
            binding.brinkedto.isEnabled = false
            ifAgreePrivacy = true
            showLoading()
            Robedto.event("app_start")
            Columnedto.initApp()
        }
        notificationId = intent.getIntExtra("actionId", 0)
        initUMP()
        showLoading()
        binding.debutedto.setOnClickListener {
            val intent = Intent(
                Intent.ACTION_VIEW,
                Uri.parse(GlobalConfig.URL_PRIVACY)
            )
            startActivity(intent)
        }
        // jumpMain()
        if (Build.VERSION.SDK_INT >= 33) {
            registerForActivityResult(ActivityResultContracts.RequestPermission()) {}.launch(
                Manifest.permission.POST_NOTIFICATIONS
            )
        }

    }

    private fun showLoading() {
        binding.todayedto.isVisible = !ifAgreePrivacy
        binding.engineeredto.isVisible = !ifAgreePrivacy
        binding.weaknessedto.isVisible = ifAgreePrivacy
        if (ifAgreePrivacy) {
            initAd()
            showProgress()
        }
    }

    private fun initAd() {
        Togetheredto.init(
            this,
            {
                if (isFinishing || isDestroyed) return@init
                isLoadAdComplete = true
                showProgress(binding.tolerateedto.progress, 10)
                when (it) {
                    0 -> {
                        countdownTimer.start()
                    }

                    1 -> {
                        Togetheredto.showInterstitialAd(
                            this,
                            dismissed = { delayedToHome() },
                            failed = { delayedToHome() }
                        )
                    }

                    else -> {
                        toHome()
                    }
                }
            }, {
                delayedToHome()
            }
        )
    }

    private val countdownTimer = object : CountDownTimer(3000, 1000) {
        override fun onTick(millisUntilFinished: Long) {
            val seconds = (millisUntilFinished / 1000).toInt()
            binding.distantedto.text = "This process may contain ads (${seconds})"
        }

        override fun onFinish() {
            Togetheredto.showAppOpenAd(
                this@Bleededto,
                dismissed = { delayedToHome() },
                failed = { delayedToHome() }
            )
        }
    }

    private fun showProgress(start: Int = 1, timeout: Long = 15000) {
        progressAnimator?.cancel()
        progressAnimator = ValueAnimator.ofInt(start, 100).apply {
            duration = timeout
            interpolator = LinearInterpolator()
            addUpdateListener { animation ->
                val value = animation.animatedValue as Int
                binding.tolerateedto.progress = value
                if (value == 100 && !isLoadAdComplete) {
                    toHome()
                }
            }
        }
        progressAnimator?.start()
    }

    private fun delayedToHome() {
        binding.brinkedto.postDelayed({ toHome() }, 300)
    }

    private fun toHome() {
        if (isFinishing || isDestroyed) return
        when (notificationId) {
            Snuggleedto.ID_JUNK_CLEAN_PUSH -> {
                startActivity(Intent(this, Practicaledto::class.java))
            }

            Snuggleedto.ID_SIMILAR_IMAGE -> {
                startActivity(Intent(this, Stooledto::class.java))
            }

            Snuggleedto.ID_SCREENSHOT_CLEAN -> {
                startActivity(Intent(this, Stooledto::class.java))
            }

            Snuggleedto.ID_RECOVERY_PHOTOS -> {
                startActivity(
                    Intent(this, Snailedto::class.java)
                        .putExtra("ScanType", Snuggleedto.SCAN_PHOTOS)
                )
            }

            Snuggleedto.ID_RECOVERY_VIDEOS -> {
                startActivity(
                    Intent(this, Snailedto::class.java)
                        .putExtra("ScanType", Snuggleedto.SCAN_VIDEOS)
                )
            }

            Snuggleedto.ID_RECOVERY_DOCUMENTS -> {
                startActivity(
                    Intent(this, Snailedto::class.java)
                        .putExtra("ScanType", Snuggleedto.SCAN_DOCUMENTS)
                )
            }

            Snuggleedto.ID_APP_MANAGER -> {
                startActivity(Intent(this, Pickedto::class.java))
            }

            Snuggleedto.ID_BATTERY_INFO -> {
                startActivity(Intent(this, Treeedto::class.java))
            }

            Snuggleedto.ID_WEATHER -> {
                startActivity(Intent(this, Incomeedto::class.java))
            }


            else -> {
                val isHotLaunch = intent?.extras?.getBoolean("isHotLaunch", false) ?: false
                if (!isHotLaunch) {
                    if (!Snuggleedto.isGuide) {
                        startActivity(Intent(this, Scenarioedto::class.java))
                    } else {
                        startActivity(Intent(this, Acceptedto::class.java))
                    }
                }
            }
        }
        finish()
    }
}