package com.base.superfilemanager.activity.speed

import android.animation.Animator
import android.content.Intent
import android.graphics.Color
import androidx.core.view.isVisible
import androidx.core.view.updatePadding
import com.blankj.utilcode.util.AppUtils
import com.blankj.utilcode.util.BarUtils
import com.base.superfilemanager.databinding.ActivityLayoutSpeedBinding
import com.base.superfilemanager.helps.BaseActivity
import com.base.superfilemanager.helps.ConfigHelper
import com.base.superfilemanager.view.AFunOb.APP_SPEED
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.MainScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import kotlin.random.Random

class SpeedActivity : BaseActivity<ActivityLayoutSpeedBinding>() {
    override val binding: ActivityLayoutSpeedBinding by lazy {
        ActivityLayoutSpeedBinding.inflate(layoutInflater)
    }

    override fun initView() {
        BarUtils.setStatusBarColor(this, Color.TRANSPARENT)
        binding.root.updatePadding(top = BarUtils.getStatusBarHeight())
        binding.idOneLottie.imageAssetsFolder = "guan_super_li/images/"
        binding.idOneLottie.setAnimation("guan_super_li/data.json")
        binding.idOneLottie.playAnimation()
        binding.root.postDelayed({
            binding.idOneLottie.isVisible = false
            binding.idScaning.text = "Optimizing"

            playRocket()
        }, 3000)
    }


    private fun playRocket() {
        binding.idRocket.imageAssetsFolder = "fast_fast/images/"
        binding.idRocket.setAnimation("fast_fast/data.json")
        binding.idRocket.playAnimation()
        MainScope().launch(Dispatchers.Main) {
            launch {
                if (ConfigHelper.appList.isNullOrEmpty()) {
                    withContext(Dispatchers.IO) {
                        ConfigHelper.appList = AppUtils.getAppsInfo().shuffled()
                    }
                }
                val icons = ConfigHelper.appList?.filter { !it.isSystem }?.map { it.icon }
                var index = 0
                binding.idSpeedIcon.isVisible = true
                while (binding.idRocket.isVisible) {
                    binding.idSpeedIcon.setImageDrawable(icons?.getOrNull(index++ % icons.size))
                    delay(Random.nextLong(100, 200))
                }
            }
        }
        binding.idRocket.addAnimatorListener(object : Animator.AnimatorListener {
            override fun onAnimationStart(p0: Animator) {

            }

            override fun onAnimationEnd(p0: Animator) {
                startActivity(
                    Intent(
                        this@SpeedActivity,
                        SpeedFinishActivity::class.java
                    ).putExtra("type", APP_SPEED)
                )
                finish()
            }

            override fun onAnimationCancel(p0: Animator) {

            }

            override fun onAnimationRepeat(p0: Animator) {

            }

        })
    }
}