package com.base.filerecoveryrecyclebin.activity.junkclean

import android.animation.ValueAnimator
import android.content.Intent
import android.graphics.Color
import android.view.animation.LinearInterpolator
import android.widget.Toast
import androidx.activity.addCallback
import androidx.core.view.updatePadding
import com.base.filerecoveryrecyclebin.ads.admob.AdmobInterstitialUtils
import com.base.filerecoveryrecyclebin.databinding.ActivityLayoutScanJunkBinding
import com.base.filerecoveryrecyclebin.help.BaseActivity
import com.base.filerecoveryrecyclebin.help.PermissionHelp.checkStorePermission
import com.base.filerecoveryrecyclebin.help.PermissionHelp.requestStorePermission
import com.base.filerecoveryrecyclebin.utils.BarUtils
import com.base.filerecoveryrecyclebin.view.DialogViews.showGerPermission

/**
 * 清理动画页面
 */
class ScanJunkActivity : BaseActivity<ActivityLayoutScanJunkBinding>() {

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

    override fun initView() {
        BarUtils.setStatusBarColor(this, Color.TRANSPARENT)
        binding.root.updatePadding(top = BarUtils.getStatusBarHeight())

        if (checkStorePermission()) {
            playLottie()
        } else {
            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) {
                            playLottie(false)
                        } else {
                            finishToMain()
                        }
                    })
                })

        }
    }

    override fun initListener() {

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

    private var isPause = false
    override fun onResume() {
        super.onResume()
        if (isPause) {
            binding.idJunkScan.resumeAnimation()
        }
        isPause = false
    }

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

    override fun onPause() {
        super.onPause()
        isPause = true
        animator1?.pause()
        animator2?.pause()
        animator3?.pause()
        binding.idJunkScan.pauseAnimation()
    }

    private fun playLottie(showAd: Boolean = true) {
        val a1 = ValueAnimator.ofFloat(0f, 360f)
        a1.run {
            duration = 1000
            repeatMode = ValueAnimator.RESTART
            repeatCount = ValueAnimator.INFINITE
            interpolator = LinearInterpolator()
            addUpdateListener {
                binding.idView1.rotation = it.animatedValue as Float
            }
            start()
        }
        val a2 = ValueAnimator.ofFloat(0f, 360f)
        a2.run {
            duration = 1000
            repeatMode = ValueAnimator.RESTART
            repeatCount = ValueAnimator.INFINITE
            interpolator = LinearInterpolator()
            addUpdateListener {
                binding.idView2.rotation = it.animatedValue as Float
            }
            start()
        }
        val a3 = ValueAnimator.ofFloat(0f, 360f)
        a3.run {
            duration = 1000
            repeatMode = ValueAnimator.RESTART
            repeatCount = ValueAnimator.INFINITE
            interpolator = LinearInterpolator()
            addUpdateListener {
                binding.idView3.rotation = it.animatedValue as Float
            }
            start()
        }

        binding.idJunkScan.imageAssetsFolder = "junk_scan/images/"
        binding.idJunkScan.setAnimation("junk_scan/data.json")
        binding.idJunkScan.playAnimation()
        binding.root.postDelayed({

            if (showAd) {
                AdmobInterstitialUtils.showInterstitialAd(this) {
                    a1.cancel()
                    a2.cancel()
                    a3.cancel()
                    startActivity(Intent(this, CleanJunkActivity::class.java))
                    finish()
                }
            } else {
                a1.cancel()
                a2.cancel()
                a3.cancel()
                startActivity(Intent(this, CleanJunkActivity::class.java))
                finish()
            }
        }, 5000)
    }

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