package com.test.basd.onecleanmaster.activity

import android.animation.ValueAnimator
import android.content.Intent
import android.view.animation.LinearInterpolator
import androidx.activity.OnBackPressedCallback
import com.test.basd.onecleanmaster.databinding.ActivityLayoutScanJunkBinding
import com.test.basd.onecleanmaster.helps.BaseActivity
import com.test.basd.onecleanmaster.helps.ads.AdmobUtils

class ScanJunkActivity : BaseActivity<ActivityLayoutScanJunkBinding>() {
    override val isLightMode = true
    override val binding: ActivityLayoutScanJunkBinding by lazy {
        ActivityLayoutScanJunkBinding.inflate(layoutInflater)
    }

    override fun initView() {
        binding.idBackJunkScan.setOnClickListener {
            onBackPressedDispatcher.onBackPressed()
        }
        onBackPressedDispatcher.addCallback(object : OnBackPressedCallback(true) {
            override fun handleOnBackPressed() {
                finishToMain()
            }

        })
    }

    override fun onStart() {
        super.onStart()
        checkPermission()
    }

    var isplay = 0
    override fun onPermissionsResult(isGranted: Boolean) {
        if (isGranted) {
            isplay += 1
            if (isplay == 1) {
                playLottie()
            }
        } else {
            finishToMain()
        }
    }

    private fun playLottie() {
        ValueAnimator.ofFloat(0f, 360f).run {
            duration = 1000
            repeatMode = ValueAnimator.RESTART
            repeatCount = ValueAnimator.INFINITE
            interpolator = LinearInterpolator()
            addUpdateListener {
                binding.idView1.rotation = it.animatedValue as Float
            }
            start()
        }
        ValueAnimator.ofFloat(0f, 360f).run {
            duration = 1000
            repeatMode = ValueAnimator.RESTART
            repeatCount = ValueAnimator.INFINITE
            interpolator = LinearInterpolator()
            addUpdateListener {
                binding.idView2.rotation = it.animatedValue as Float
            }
            start()
        }
        ValueAnimator.ofFloat(0f, 360f).run {
            duration = 1000
            repeatMode = ValueAnimator.RESTART
            repeatCount = ValueAnimator.INFINITE
            interpolator = LinearInterpolator()
            addUpdateListener {
                binding.idView3.rotation = it.animatedValue as Float
            }
            start()
        }
        binding.idJunkScan.imageAssetsFolder = "onecle_sao_miao/images/"
        binding.idJunkScan.setAnimation("onecle_sao_miao/data.json")
        binding.idJunkScan.playAnimation()
        binding.root.postDelayed({
            AdmobUtils.showInterstitialAd(this) {
                startActivity(Intent(this, CleanJunkActivity::class.java))
                finish()
            }

        }, 5000)
    }
}