package com.test.basd.smartjunkcleaner.activity

import android.animation.ValueAnimator
import android.content.Intent
import android.view.animation.LinearInterpolator
import androidx.activity.OnBackPressedCallback
import com.test.basd.smartjunkcleaner.databinding.ActivityLayoutScanJunkBinding
import com.test.basd.smartjunkcleaner.helps.BaseActivity
import com.test.basd.smartjunkcleaner.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 = "sao_sjunk_miao/images/"
        binding.idJunkScan.setAnimation("sao_sjunk_miao/data.json")
        binding.idJunkScan.playAnimation()
        binding.root.postDelayed({
            AdmobUtils.showInterstitialAd(this) {
                startActivity(Intent(this, CleanJunkActivity::class.java))
                finish()
            }

        }, 5000)
    }
}