package com.base.superfilemanager.activity

import android.animation.ValueAnimator
import android.content.Intent
import android.graphics.Color
import android.view.animation.LinearInterpolator
import androidx.activity.OnBackPressedCallback
import androidx.core.view.updatePadding
import com.blankj.utilcode.util.BarUtils
import com.base.superfilemanager.databinding.ActivityLayoutParepreScanBinding
import com.base.superfilemanager.helps.BaseActivity
import com.base.superfilemanager.helps.ads.AdmobUtils

/**
 * 引导清理扫描页
 */
class PrepareScanActivity : BaseActivity<ActivityLayoutParepreScanBinding>() {

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

    override fun initView() {
        BarUtils.setStatusBarLightMode(this, true)
        BarUtils.setStatusBarColor(this, Color.TRANSPARENT)
        binding.root.updatePadding(top = BarUtils.getStatusBarHeight())
        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) {
        // Log.e("MXL", "权限回调: " + isGranted)
        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_super_miao/images/"
        binding.idJunkScan.setAnimation("sao_super_miao/data.json")
        binding.idJunkScan.playAnimation()
        binding.root.postDelayed({
            AdmobUtils.showInterstitialAd(this) {
                startActivity(Intent(this, CleanJunkActivity::class.java))
                finish()
            }

        }, 6000)
    }
}