package com.dumpster.cleaner.ui.guide

import android.animation.Animator
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.core.view.isVisible
import androidx.core.view.updatePadding
import com.dumpster.cleaner.R
import com.dumpster.cleaner.SpConstObject.isGuided
import com.dumpster.cleaner.base.BaseActivity
import com.dumpster.cleaner.base.jumpAction
import com.dumpster.cleaner.bean.FeatureBean.Companion.JUNK_CLEAN
import com.dumpster.cleaner.business.ads.AdEvent
import com.dumpster.cleaner.business.ads.AdsMgr
import com.dumpster.cleaner.business.ads.AdsShowCallBack
import com.dumpster.cleaner.business.ads.LimitUtils
import com.dumpster.cleaner.business.ads.admob.AdmobEvent
import com.dumpster.cleaner.databinding.ActivityLayoutCleanGuideBinding
import com.dumpster.cleaner.ui.dialog.StoragePermissionDialog
import com.dumpster.cleaner.ui.main.MainActivity
import com.dumpster.cleaner.utils.BarUtils
import com.dumpster.cleaner.utils.PermissionUtils.checkStorePermission
import com.dumpster.cleaner.utils.PermissionUtils.requestStoragePermission


class CleanGuideActivity : BaseActivity<ActivityLayoutCleanGuideBinding>(ActivityLayoutCleanGuideBinding::inflate) {


    override fun useDefaultImmersive() {
        ViewCompat.setOnApplyWindowInsetsListener(binding.root) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, 0, systemBars.right, systemBars.bottom)
            insets
        }
    }


    override fun initView() {
        super.initView()
        binding.root.updatePadding(top = BarUtils.getStatusBarHeight())
        play1()
    }

    override fun initListener() {
        binding.idSkip.setOnClickListener {
            AdsMgr.showInsert(this, true, object : AdsShowCallBack() {
                override fun next() {
                    goToAc(MainActivity::class.java)
                    finish()
                }
            })
        }
        binding.idClean02.setOnClickListener {
            val jump = {
                jumpAction(JUNK_CLEAN)
                finish()
            }
            if (this.checkStorePermission()) {
                jump.invoke()
            } else {
                val dialog = StoragePermissionDialog(this)
                dialog.action = {
                    requestStoragePermission(launcher) {
                        if (it) {
                            jump.invoke()
                        } else {
                            binding.idSkip.callOnClick()
                        }
                    }
                }
                dialog.showDialog()
            }
        }
    }

    private fun play1() {
        binding.idLottieview.setAnimation(R.raw.firstscan)
        binding.idLottieview.playAnimation()
        binding.idLottieview.addAnimatorListener(createAnimatorListener())
        binding.idLottieview.addAnimatorUpdateListener { animation ->
            // 获取动画的当前进度（0.0f 到 1.0f）
            val progress = animation.animatedFraction
            // 将进度条的进度设置为动画进度的百分比
            binding.idProg01.progress = (progress * 100).toInt()
            if (binding.idProg01.progress == 50) {
                play2()
            }
        }
        binding.idProg01.progress = 0
        binding.idProg01.max = 50
    }

    private fun play2() {
        binding.idLottieview.setAnimation(R.raw.firstscan_finish)
        binding.idLottieview.playAnimation()
        binding.idLl01.isVisible = false
        binding.idLl02.isVisible = true
        binding.idSkip.isVisible = true
    }

    private fun createAnimatorListener(): Animator.AnimatorListener {
        return object : Animator.AnimatorListener {
            override fun onAnimationStart(p0: Animator) {
                // 动画开始时调用
            }

            override fun onAnimationEnd(p0: Animator) {
                // 动画结束时调用

            }

            override fun onAnimationCancel(p0: Animator) {
                // 动画被取消时调用
            }

            override fun onAnimationRepeat(p0: Animator) {
                // 动画重复时调用
            }
        }
    }

    override fun handleBackCallBack() {
        binding.idSkip.callOnClick()
    }
}