package rowedto.jockeyedto.sheetedto.atticedto.directionedto.noxiousedto.batedto

import android.animation.ValueAnimator
import android.annotation.SuppressLint
import android.content.Intent
import android.graphics.Color
import android.view.View
import android.view.animation.LinearInterpolator
import android.widget.Toast
import androidx.activity.addCallback
import androidx.core.view.updatePadding
import androidx.lifecycle.lifecycleScope
import com.base.datarecovery.databinding.MeatedtoBinding
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import rowedto.jockeyedto.sheetedto.balsamedto.formaledto.orientationedto.Patedto.showGerPermission
import rowedto.jockeyedto.sheetedto.knewedto.economicedto.copyrightedto.Crossingedto
import rowedto.jockeyedto.sheetedto.knewedto.economicedto.copyrightedto.Studiededto.checkStorePermission
import rowedto.jockeyedto.sheetedto.knewedto.economicedto.copyrightedto.Studiededto.requestStorePermission
import rowedto.jockeyedto.sheetedto.raceedto.pavementedto.Dishedto
import rowedto.jockeyedto.sheetedto.raceedto.pavementedto.Spareedto
import rowedto.jockeyedto.sheetedto.strangeedto.pyramidedto.particlesedto.Scandaledto
import rowedto.jockeyedto.sheetedto.strangeedto.pyramidedto.particlesedto.Scandaledto.admobMaxShowInterstitialAd

/**
 * 清理动画页面
 */
class Celebrationedto : Crossingedto<MeatedtoBinding>() {

    private val TAG = "ScanJunkActivity"
    override val binding: MeatedtoBinding by lazy {
        MeatedtoBinding.inflate(layoutInflater)
    }

    private var job: Job? = null
    override fun initView() {
        Dishedto.logDebug(TAG,"ScanJunkActivity initView")
        Spareedto.setStatusBarColor(this, Color.TRANSPARENT)
        binding.root.updatePadding(top = Spareedto.getStatusBarHeight())
        Scandaledto.admobMaxShowNativeAd(this,binding.tooledto,1)
        if (!checkStorePermission()) {
            showGerPermission(tittle = "Storage Permission Required",
                desc = "This feature requires access to your storage to scan your files and clean up junk files and unused APK files. We will not transmit your data to any third-party service. Please grant permission so that we can provide you with better service.",
                deny = { finishToMain() },
                allow = {
                    requestStorePermission(launcher, result = {
                        if (it) {
                        } else {
                            finishToMain()
                        }
                    })
                })
        }
    }


    override fun initListener() {

        onBackPressedDispatcher.addCallback {
            Toast.makeText(this@Celebrationedto, "wait a moment", Toast.LENGTH_SHORT).show()
        }
    }


    override fun onResume() {
        super.onResume()
        if (checkStorePermission()) {
            resumeLottie()
            job = waitJob()
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        animator1?.cancel()
        animator2?.cancel()
        animator3?.cancel()
        binding.giftedto.cancelAnimation()
    }

    override fun onPause() {
        super.onPause()
        animator1?.pause()
        animator2?.pause()
        animator3?.pause()
        binding.giftedto.pauseAnimation()
        job?.cancel()
    }

    @SuppressLint("SetTextI18n")
    private fun resumeLottie() {
        if (animator1 == null) {
            animator1 = ValueAnimator.ofFloat(0f, 360f)
            animator1?.run {
                duration = 1000
                repeatMode = ValueAnimator.RESTART
                repeatCount = ValueAnimator.INFINITE
                interpolator = LinearInterpolator()
                addUpdateListener {
                    binding.continuationedto.rotation = it.animatedValue as Float
                }
            }
        }
        animator1?.start()
        if (animator2 == null) {
            animator2 = ValueAnimator.ofFloat(0f, 360f)
            animator2?.run {
                duration = 1000
                repeatMode = ValueAnimator.RESTART
                repeatCount = ValueAnimator.INFINITE
                interpolator = LinearInterpolator()
                addUpdateListener {
                    binding.sphereedto.rotation = it.animatedValue as Float
                }
            }
        }
        animator2?.start()
        if (animator3 == null) {
            animator3 = ValueAnimator.ofFloat(0f, 360f)
            animator3?.run {
                duration = 1000
                repeatMode = ValueAnimator.RESTART
                repeatCount = ValueAnimator.INFINITE
                interpolator = LinearInterpolator()
                addUpdateListener {
                    binding.apartmentedto.rotation = it.animatedValue as Float
                }
            }
        }
        animator3?.start()
        binding.giftedto.imageAssetsFolder = "junk_scan/images/"
        binding.giftedto.setAnimation("junk_scan/data.json")
        binding.giftedto.playAnimation()
    }

    private fun waitJob() = lifecycleScope.launch(Dispatchers.Main) {
        delay(5000)
        binding.giftedto.visibility = View.GONE
        binding.innovationedto.visibility = View.VISIBLE
        binding.innovationedto.playAnimation()
        binding.lightedto.text = "Completed!"
        animator1?.cancel()
        animator2?.cancel()
        animator3?.cancel()
        delay(1000)
        admobMaxShowInterstitialAd(this@Celebrationedto) {
            startActivity(Intent(this@Celebrationedto, Childishedto::class.java))
            finish()
        }
    }


    private var animator1: ValueAnimator? = null
    private var animator2: ValueAnimator? = null
    private var animator3: ValueAnimator? = null
}