package rallyedto.finishedto.hypothesisedto.vegetableedto.breakfastedto

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.QuitedtoBinding
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import rallyedto.battleedto.acresedto.Lemonedto
import rallyedto.battleedto.acresedto.Napedto.checkStorePermission
import rallyedto.battleedto.acresedto.Napedto.requestStorePermission
import rallyedto.farewelledto.Technologyedto.showGerPermission
import rallyedto.sunedto.tumouredto.majorityedto.loseedto.Togetheredto
import rallyedto.talledto.blewedto.Islandedto
import rallyedto.talledto.blewedto.Musicaledto

/**
 * 清理动画页面
 */
class Practicaledto : Lemonedto<QuitedtoBinding>() {

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

    private var job: Job? = null
    override fun initView() {
        Musicaledto.logDebug(TAG,"ScanJunkActivity initView")
        Islandedto.setStatusBarColor(this, Color.TRANSPARENT)
        binding.root.updatePadding(top = Islandedto.getStatusBarHeight())
        Togetheredto.showNativeAd(this,binding.jeweledto, isShowAdNative = Togetheredto.isShowAdNativeBottom())
        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@Practicaledto, "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.correctedto.cancelAnimation()
    }

    override fun onPause() {
        super.onPause()
        animator1?.pause()
        animator2?.pause()
        animator3?.pause()
        binding.correctedto.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.bridgeedto.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.soapedto.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.anechoicedto.rotation = it.animatedValue as Float
                }
            }
        }
        animator3?.start()
        binding.correctedto.imageAssetsFolder = "junk_scan/images/"
        binding.correctedto.setAnimation("junk_scan/data.json")
        binding.correctedto.playAnimation()
    }

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


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