package com.test.basd.fastcleanerjunk.activity

import android.animation.ValueAnimator
import android.content.Intent
import android.graphics.Color
import android.net.Uri
import android.provider.Settings
import android.view.animation.LinearInterpolator
import androidx.activity.OnBackPressedCallback
import androidx.core.view.updatePadding
import com.blankj.utilcode.util.BarUtils
import com.test.basd.fastcleanerjunk.databinding.ActivityLayoutParepreScanBinding
import com.test.basd.fastcleanerjunk.helps.BaseActivity
import com.test.basd.fastcleanerjunk.helps.PermissionHelp.checkStorePermission
import com.test.basd.fastcleanerjunk.helps.PermissionHelp.requestStorePermission
import com.test.basd.fastcleanerjunk.helps.ads.AdmobUtils
import com.test.basd.fastcleanerjunk.view.DialogViews

/**
 * 引导清理扫描页
 */
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()
            }
        })
        if (checkStorePermission()) {
            playLottie()
        } else {
            DialogViews.showGerPermission(this, {
                val intent = Intent(Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION)
                intent.addCategory("android.intent.category.DEFAULT")
                intent.data = Uri.parse("package:${packageName}")

                if (intent.resolveActivity(packageManager) != null) {
                    requestStorePermission(launcher, jumpAction = {}, result = { flag ->
                        if (flag) {
                            playLottie()
                        } else {
                            finishToMain()
                        }
                    })
                    PermissionTripActivity.launch(this)
                } else {
                    finishToMain()
                }
            }, {
                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_smax_miao/images/"
        binding.idJunkScan.setAnimation("sao_smax_miao/data.json")
        binding.idJunkScan.playAnimation()
        binding.root.postDelayed({
            AdmobUtils.showInterstitialAd(this) {
                startActivity(Intent(this, CleanJunkActivity::class.java))
                finish()
            }

        }, 6000)
    }
}