package com.zxhyis.toolss.ui.page

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.zxhyis.toolss.ad.AdmobUtils
import com.zxhyis.toolss.base.TopActivity
import com.zxhyis.toolss.databinding.ActivityLayoutParepreScanBinding
import com.zxhyis.toolss.logic.setOnClick

class PrepareScanActivity : TopActivity<ActivityLayoutParepreScanBinding>() {
    override fun getViewBinding1(): ActivityLayoutParepreScanBinding {
        return ActivityLayoutParepreScanBinding.inflate(layoutInflater)
    }

    override fun init() {
        BarUtils.setStatusBarLightMode(this, true)
        BarUtils.setStatusBarColor(this, Color.TRANSPARENT)
        vb.root.updatePadding(top = BarUtils.getStatusBarHeight())
        vb.idBackJunkScan.setOnClick(javaClass.simpleName) {
            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 {
                vb.idView1.rotation = it.animatedValue as Float
            }
            start()
        }
        ValueAnimator.ofFloat(0f, 360f).run {
            duration = 1000
            repeatMode = ValueAnimator.RESTART
            repeatCount = ValueAnimator.INFINITE
            interpolator = LinearInterpolator()
            addUpdateListener {
                vb.idView2.rotation = it.animatedValue as Float
            }
            start()
        }
        ValueAnimator.ofFloat(0f, 360f).run {
            duration = 1000
            repeatMode = ValueAnimator.RESTART
            repeatCount = ValueAnimator.INFINITE
            interpolator = LinearInterpolator()
            addUpdateListener {
                vb.idView3.rotation = it.animatedValue as Float
            }
            start()
        }
        vb.idJunkScan.imageAssetsFolder = "scan/images/"
        vb.idJunkScan.setAnimation("scan/data.json")
        vb.idJunkScan.playAnimation()
        vb.root.postDelayed({
            AdmobUtils.showInterstitialAd(this) {
                startActivity(Intent(this, JunksActivity::class.java))
                finish()
            }

        }, 6000)
    }
}